How can i crop the image based on white pixel value

7 ビュー (過去 30 日間)
Stephen john
Stephen john 2022 年 7 月 31 日
コメント済み: Image Analyst 2022 年 8 月 2 日
Hello Everyone, I hope you are doing well. i have the following image I want to crop the image based on min and maximum value. as you can see i have the pixel value at 348 , 343 and 338 , so he minimum value is 338 and maximum value is 348 so i want to crop the image and so the values lies in the image.

採用された回答

Image Analyst
Image Analyst 2022 年 7 月 31 日
Not sure I see anything from that image. Anyway if you crop the image based on the image's min and max value, you will of course end up with the entire image - all pixels.
If the image has values below some other "min" value you specify you can do
mask = grayImage >= minValue & grayImage <= maxValue;
[r, c] = find(mask);
row1 = min(r);
row2 = max(r);
col1 = min(c);
col2 = max(c);
croppedImage = grayImage(row1:row2, col1:col2);
  18 件のコメント
Stephen john
Stephen john 2022 年 8 月 2 日
@Image Analyst Your code work good. If i want to crop 5 pixel above and 5 pixel below the Values how can i do that?
Image Analyst
Image Analyst 2022 年 8 月 2 日
croppedImage = grayImage((row1 - 5) : (row2 + 5), col1 : col2);

サインインしてコメントする。

その他の回答 (1 件)

DGM
DGM 2022 年 7 月 31 日
Assuming that the image is 2D, this is one way
A = imread('pepcircmask.png');
imshow(A)
% find the first and last row containing nonzero elements
nzrows = any(A,2);
idx1 = find(nzrows,1,'first');
idx2 = find(nzrows,1,'last');
% extract that region
A = A(idx1:idx2,:);
imshow(A)
  12 件のコメント
Stephen john
Stephen john 2022 年 8 月 2 日
@Image Analyst Okay i think i can't deliver it properly . the code you share work Okay, I want to use the same code and crop the image 5 pixel above the one white pixel value and 5 pixel below the 5 pixel value so i can get some black background too.
DGM
DGM 2022 年 8 月 2 日
Something like this?
A = imread('image1.png');
imshow(A)
% find the first and last row containing nonzero elements
% note that padding is potentially restricted
% if blob is located close to the image boundary
nzrows = any(A,2);
idx1 = max(find(nzrows,1,'first')-5,1);
idx2 = min(find(nzrows,1,'last')+5,size(A,1));
% extract that region
A = A(idx1:idx2,:,:);
imshow(A)
% the size of the cropped image
size(A)
ans = 1×2
25 1000
% show a square sample of the cropped region
imshow(A(:,1:15))

サインインしてコメントする。

カテゴリ

Help Center および File Exchange3-D Volumetric Image Processing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by