Clear Filters
Clear Filters

how to covert an irregular shape into an equivalent filled square?

5 views (last 30 days)
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
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.
MatlabUser
MatlabUser on 14 Mar 2022
Thanks for replying,
actually when I said texture ( I mean it is any rgb color image just not one solid color), it is like hole filling (the region texture is not known but we don't care to have true values of the region outside the irregular shape I just need it for internal processing (the visual appearance is not important) @DGM

Sign in to comment.

Accepted Answer

Image Analyst
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
MatlabUser
MatlabUser on 14 Mar 2022
Edited: MatlabUser on 15 Mar 2022
@Image Analyst , thank you very much for your replying, actually yes I am using (bsxfun) to get that region of the image using its mask.
Now similar to the previous replies my problem is not how to find this region, but how to padding the pixels values to fill the empty pixels in the bounding box that are outside the irregular shape but inside the bounding pixels (padding by same values of the irregular shape boundaries not a solid color).
Image Analyst
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);

Sign in to comment.

More Answers (1)

Matt J
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
MatlabUser
MatlabUser on 15 Mar 2022
Edited: MatlabUser on 15 Mar 2022
actually it is not exactly as what I meant since median again gives one solid color and I wanted different colors based on the borders of the irregular region, so inpainting is a better filling. Thanks for your reply and efforts.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!