現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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))
Stephen john
2022 年 8 月 1 日
編集済み: Stephen john
2022 年 8 月 1 日
Can you please share the correct code.
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)