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

5 次查看(过去 30 天)
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 个评论
DGM
DGM 2022-3-13
编辑:DGM 2022-3-13
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 2022-3-14
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

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2022-3-13
编辑:Image Analyst 2022-3-14
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 个评论
MatlabUser
MatlabUser 2022-3-14
编辑:MatlabUser 2022-3-15
@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 2022-3-14
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);

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2022-3-13
编辑:Matt J 2022-3-13
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 个评论
MatlabUser
MatlabUser 2022-3-15
编辑:MatlabUser 2022-3-15
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by