How to find the (pixel) dimensions of images on a white background

1 view (last 30 days)
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!
  2 Comments
Matthew Peoples
Matthew Peoples on 6 Apr 2021
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! :)
Adam Danz
Adam Danz on 8 Apr 2021
As long as the strawberry is oriented vertically or horizontally, the simple approach in my solution gives you to width and height.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 6 Apr 2021
Edited: Adam Danz on 7 Apr 2021
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');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!