How can i crop the image based on white pixel value
7 次查看(过去 30 天)
显示 更早的评论
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
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
2022-7-31
@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
2022-7-31
@Image Analyst how to find minValue and max value as you give in the code
mask = grayImage >= minValue & grayImage <= maxValue;
Image Analyst
2022-7-31
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
2022-7-31
@Image Analyst But i want it to be in automatic not manually i put the min max value. Like 338 and 348
Image Analyst
2022-7-31
If you want the min value that's not zero, use
mask = grayImage ~ 0;
minValue = min(grayImage(mask))
maxValue = max(grayImage(mask))
Image Analyst
2022-8-1
mask = grayImage ~= 0;
minValue = min(grayImage(mask))
maxValue = max(grayImage(mask))
Stephen john
2022-8-1
@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
2022-8-1
Here is the image i have attached Please can you modified the final code which work on this image
Image Analyst
2022-8-1
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
2022-8-1
@Image Analyst Why the Value is between 5 and 15? and the image background should be black. and forground should be white.
Image Analyst
2022-8-1
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
2022-8-1
@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
2022-8-1
@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
2022-8-1
编辑:Image Analyst
2022-8-1
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
2022-8-2
@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
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?
更多回答(1 个)
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
2022-7-31
@DGM Its not working giving the error Index in position 1 exceeds array bounds.
DGM
2022-8-1
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
2022-8-1
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
2022-8-1
@DGM Here i have attached the image. Can you please modified the code for this image
DGM
2022-8-2
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
2022-8-2
Essentially the same thing I did and he said he didn't want it. That's why I gave up.
Image Analyst
2022-8-2
编辑:Image Analyst
2022-8-2
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
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
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 Exchange 中查找有关 3-D Volumetric Image Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)