how to covert an irregular shape into an equivalent filled square?
4 ビュー (過去 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
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.
採用された回答
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 件のコメント
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
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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!