I realize in hindsight this is a bit of a confusing question. Basically I really need to get the dimensions (height and width) of the strawberry image within the white canvas (not the entire canvas itself). Hope someone can help! :)
How to find the (pixel) dimensions of images on a white background
1 次查看(过去 30 天)
显示 更早的评论
Hello there,
I have a folder of 60 images of irregular household items that are greyscale on a white 500x500 pixel canvas. I need to have the images rescaled (will do in photoshop) to match the average size of a different set of images. Using MATLAB, I found the references images have 1.51 times more non-white pixels (i.e. image pixels) than the items do and need to alter these images to match the size. However, because the images are of vastly different items (i.e. a paperclip vs a closet), they vary a lot by dark pixel count.
How can I find the dimensions (height and width) of these images in MATLAB. I've included an example of an image being
samplePic = imread(img_dir + "/hn_processed");
image_double = im2double(samplePic); % turn all white pixels to 1
% I tried the below code by it didn't do what I wanted
actual_image = find(image_double ~= 1);
[rows, columns, numberOfColorChannels] = size(actual_image);
used. Thank you!![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/574807/image.bmp)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/574807/image.bmp)
2 个评论
Adam Danz
2021-4-8
As long as the strawberry is oriented vertically or horizontally, the simple approach in my solution gives you to width and height.
采纳的回答
Adam Danz
2021-4-6
编辑:Adam Danz
2021-4-7
Assuming height and width are defined by the first and last non-white (or any background color) pixel from top-to-bottom and from left-to-right,
I = imread('image.bmp');
% Assumes the first pixel is the background color,
% otherwise just use 255 or likewise.
[row,col] = find(I~=I(1));
height = max(row)-min(row);
width = max(col)-min(col);
To visually confirm the measurements,
imshow(I)
hold on
r = rectangle('position', [min(col), min(row), width, height],'EdgeColor', 'r');
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!