how to covert an irregular shape into an equivalent filled square?
3 views (last 30 days)
Show older comments
MatlabUser
on 13 Mar 2022
Commented: Image Analyst
on 15 Mar 2022
I tried to describe my question in the attached image. If we have irregular region of an image (has texture background not single color), how can we have the equivalent square of that region using the padding for all borders but not scaling since we need to preserve the original texture of that region...
we can find the square by finding the min and max x and y of that region, but how to fill all the empty white pixels inside the square by padding the irregular region ?? ( I mean the texture of the original irregular region should not be changed but the padding outide can be just a padding values of the irregular shape)
any help will be appreciated.
2 Comments
DGM
on 13 Mar 2022
Edited: DGM
on 13 Mar 2022
Is the region texture known? That is, do you have a copy of the texture or a means to sample it other than the texture as presented in the region itself?
If not, the question is about texture replication. I'm not sure of a good approach to that.
Is the texture repeating? The example does not appear to be. That would complicate the problem dramatically.
Accepted Answer
Image Analyst
on 13 Mar 2022
Edited: Image Analyst
on 14 Mar 2022
If you have some irregular region as a binary image that was somehow created, and you have an original grayscale or color image, and you want to mask/blacken the original image outside the mask you can do this:
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
% Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
I don't see any reason at all to create a subimage that is the bounding box cropped out of the original image. Whatever you want to do after that will probably work just as well without cropping out the bounding box. But if you want to, you can. Just do:
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
props = regionprops(mask, 'BoundingBox');
% Crop out bounding box, masked.
% Assumes one blob per image, otherwise you'd need a loop.
croppedImage = imcrop(maskedRgbImage, props.BoundingBox)
2 Comments
Image Analyst
on 14 Mar 2022
I don't think you understand. The code I gave for bsxfun() does not FIND the region. It assumes the region has already been found and identified by a binary image mask.
The code I gave blackens the region outside the mask. If you don't want that, but instead want to basically smear perimeter values in towards the center of the mask ("inpainting") then you want to use the regionfill() function.
repairedImage = regionfill(originalImage, mask);
More Answers (1)
Matt J
on 13 Mar 2022
Edited: Matt J
on 13 Mar 2022
Like in this example, perhaps.
load Image
imshow(Image)
reg=regionprops(Image>0,'PixelIdxList','BoundingBox');
I=ceil(reg.BoundingBox(2))+(0:reg.BoundingBox(4)-1);
J=ceil(reg.BoundingBox(1))+(0:reg.BoundingBox(3)-1);
[in,out]=deal(false(size(Image)));
in(reg.PixelIdxList)=1;
out(I,J)=1;
Image( out & ~in)=3;
imshow(Image,[])
6 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!