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

4 visualizaciones (últimos 30 días)
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 comentarios
DGM
DGM el 13 de Mzo. de 2022
Editada: DGM el 13 de Mzo. de 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 el 14 de Mzo. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 13 de Mzo. de 2022
Editada: Image Analyst el 14 de Mzo. de 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 comentarios
MatlabUser
MatlabUser el 14 de Mzo. de 2022
Editada: MatlabUser el 15 de Mzo. de 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 el 14 de Mzo. de 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);

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 13 de Mzo. de 2022
Editada: Matt J el 13 de Mzo. de 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 comentarios
MatlabUser
MatlabUser el 15 de Mzo. de 2022
Editada: MatlabUser el 15 de Mzo. de 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.
Image Analyst
Image Analyst el 15 de Mzo. de 2022
Looks like regionfill() is what you wanted.

Iniciar sesión para comentar.

Categorías

Más información sobre Images en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by