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

조회 수: 8 (최근 30일)
Matthew Peoples
Matthew Peoples 2021년 4월 6일
댓글: Adam Danz 2021년 4월 8일
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
Matthew Peoples
Matthew Peoples 2021년 4월 6일
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 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
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개)

Community Treasure Hunt

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

Start Hunting!

Translated by