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