How can i crop the image based on white pixel value
Show older comments
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.

Accepted Answer
More Answers (1)
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 Comments
Stephen john
on 31 Jul 2022
What size is the array? As I said, that example assumes the array is 2D.
A = imread('blobs.png');
imshow(A)
% reduce the image to a binary mask using whatever conditions you want
mk = rgb2gray(A)>0;
% find the first and last row containing nonzero elements
nzrows = any(mk,2);
idx1 = find(nzrows,1,'first');
idx2 = find(nzrows,1,'last');
% extract that region
A = A(idx1:idx2,:,:);
imshow(A)
Stephen john
on 1 Aug 2022
Stephen john
on 1 Aug 2022
DGM
on 1 Aug 2022
What image? The only image is a screenshot of a window containing an axes containing a downsampled copy of an image. If you want to work on an image, work on the image instead of a screenshot of an image.
Stephen john
on 1 Aug 2022
The image is 2D. The original code I posted should work on it.
A = imread('image1.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)
% the size of the cropped image
size(A)
% show a square sample of the cropped region
imshow(A(:,1:15))
Image Analyst
on 2 Aug 2022
Essentially the same thing I did and he said he didn't want it. That's why I gave up.
Stephen john
on 2 Aug 2022
Image Analyst
on 2 Aug 2022
Edited: Image Analyst
on 2 Aug 2022
And no one has ever understood what you really meant. What we heard was that you have a tall black image with a white blob in it near the bottom, and that you want a new cropped image that has only the white blob in it. Essentially cropping the image to the bounding box of that checkerboard-like blob. So @DGM and I both did that but you say it's not what you want. Yet I don't ever see an image of what you DO want despite many posts.
And besides the cropping, no one ever understood what you are talking about with the min and max value. Your posted image has values of only 0 and 255 - no others.
Please re-read the posting guidelines:
Stephen john
on 2 Aug 2022
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)
% show a square sample of the cropped region
imshow(A(:,1:15))
Categories
Find more on Deep Learning for Image Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









