Resizing a binary image issue

6 次查看(过去 30 天)
Hello. I am trying to resize a binary image to match the dimensions of another workspace matrix. Yet I am losing the non-zero values in the new, resized matrix. I have attached the binary image ("BW_05052021.png") that serves as a mask.
%The value of the BW image is 10000 x 8082 logical
%Looking to resize the image matrix to 142 x 128, not worried about maintaining scalar ratio
nnz(BW); % ans = 952
BW_resize = imresize(BW, [142 128]); % Resizing BW to the specified dimensions; 142 rows and 128 columns
nnz(BW_resize); % ans = 0 and not sure why
If needed, I can clarify or provide additional information as needed. Thank you!
  1 个评论
Atsushi Ueno
Atsushi Ueno 2021-5-5
It looks like the original image is lost because the ratio of resizing is too high. If you compare the original image with images that have been reduced to 75%, 50%, and 25%, you can see the limit of the reduction ratio at which the original image can be recognized. You may need gray-scale image for your requirement.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-5-5
编辑:Walter Roberson 2021-5-5
This is to be expected.
You are compressing by a factor of 70 for rows and 63 for columns, so each output pixel is calculated by merging information from over 4400 pixels. If you were to take the mean over the area and say that it is to be set if the mean is > 1/2 then you would be far far far short for any pixel.
BW = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/608410/BW_05052021.png');
imshow(BW)
rb = floor(size(BW,1)/142);
cb = floor(size(BW,2)/128);
reduced = cast(blockproc(BW, [rb, cb], @(block) max(block.data(:))), class(BW));
size(reduced)
ans = 1×2
143 129
BW_resize = imresize(reduced, [142 128]);
imshow(BW_resize)

更多回答(1 个)

Image Analyst
Image Analyst 2021-5-5
编辑:Image Analyst 2021-5-5
To resize an image to match the size of another image, use imresize():
newImage2 = imresize(originalImage2, size(image1));

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by