You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How can i crop the image based on white pixel value
7 views (last 30 days)
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
Image Analyst
on 31 Jul 2022
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 Comments
Stephen john
on 31 Jul 2022
@Image Analyst What is gray image in this code?
If you zoom it you can see it clear there are three pixel values at 348 , 343 and 338
Stephen john
on 31 Jul 2022
@Image Analyst how to find minValue and max value as you give in the code
mask = grayImage >= minValue & grayImage <= maxValue;
Image Analyst
on 31 Jul 2022
Then min would be 338 (that is, if you're going to ignore the pixels with a value of 0), and the max would be 348.
Stephen john
on 31 Jul 2022
@Image Analyst But i want it to be in automatic not manually i put the min max value. Like 338 and 348
Image Analyst
on 31 Jul 2022
If you want the min value that's not zero, use
mask = grayImage ~ 0;
minValue = min(grayImage(mask))
maxValue = max(grayImage(mask))
Stephen john
on 1 Aug 2022
Edited: Stephen john
on 1 Aug 2022
Can you please share the correct code.
Image Analyst
on 1 Aug 2022
mask = grayImage ~= 0;
minValue = min(grayImage(mask))
maxValue = max(grayImage(mask))
Stephen john
on 1 Aug 2022
@Image Analyst it not working on my side. Can you please explain what you do in this code, it does not gives the min and max values
Stephen john
on 1 Aug 2022
Here is the image i have attached Please can you modified the final code which work on this image
Image Analyst
on 1 Aug 2022
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'image1.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgbImage(:, :, 1);
else
grayImage = rgbImage;
end
%--------------------------------------------------------------------------------------------------------
% Display the gray scale image.
subplot(2, 1, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
mask0 = grayImage ~= 0;
minValue = min(grayImage(mask0))
maxValue = max(grayImage(mask0))
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);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 1, 2);
imshow(croppedImage);
impixelinfo;
axis('on', 'image');
title('Cropped Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
Stephen john
on 1 Aug 2022
@Image Analyst Why the Value is between 5 and 15? and the image background should be black. and forground should be white.
Image Analyst
on 1 Aug 2022
Your original image had a white line-shaped blob that was 15 pixels high and 1000 pixels wide near the bottom of the image. I cropped that out as you requested: "i want to crop the image and so the values lies in the image."
Stephen john
on 1 Aug 2022
@Image Analyst We done cropped the image based on image Pixel Value Like in the above picture the value 9664,9658,9654 where white line exist.
Stephen john
on 1 Aug 2022
@Image Analyst When i resize it into 227x227 it shapes changes as you seen below.
How to get the original shape when i resize it into 227x227?
Image Analyst
on 1 Aug 2022
Edited: Image Analyst
on 1 Aug 2022
I really don't know what you want and I don't feel like, after many attempts, that you can explain it, so I'm giving up. Sorry. Perhaps get a native English speaker to explain it in much more detail and someone will answer.
Stephen john
on 2 Aug 2022
@Image Analyst I think i cant deliver it properly , i dnt understand what is 15 pixel high value? Can the pixel value should be value on y axis?
The main issue i am facing now is that when we cropped image and after resizing the shape changes. is there is any solution for that?
Stephen john
on 2 Aug 2022
@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?
More Answers (1)
DGM
on 31 Jul 2022
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
@DGM Its not working giving the error Index in position 1 exceeds array bounds.
DGM
on 1 Aug 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)
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
@DGM Here i have attached the image. Can you please modified the code for this image
DGM
on 2 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)
ans = 1×2
15 1000
% 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.
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
@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
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)
ans = 1×2
25 1000
% show a square sample of the cropped region
imshow(A(:,1:15))
See Also
Categories
Find more on 3-D Volumetric 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)