![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1033345/image.png)
Automatic cropping of an image using threshold
13 次查看(过去 30 天)
显示 更早的评论
I have an grayscale image with a intensity distribution. The peaks of the light might be at one locatoion or contain several peaks at different locations. I like to remove all areas with "lower intensity" to make the analysis faster on an smaller image.
Thus I would like to threadhold the grayscale image to say 63% of max intensity to find the regions that is of interest. Then crop the grayscale image to include all those regions in a new smaller image for further image analysis.
Which functions can do this?
Previously I have done this manually (cropping and using the eye to determine the area).
A1 = imread('red-laser-beam-grayscale.jpg');
imshow(A1,[])
%improfile
% makes image smaller
figure(5)
histeq(A1);
% take out coordianates
pts = readPoints(A1, 2);
p1=[pts(1,1) pts(2,1)]
p2=[pts(1,2) pts(2,2)]
% crop and plot
imCell_cut= A1(p1(2):p2(2),p1(1):p2(1));
imshow(imCell_cut,[])
Edit: Added an example image, see attachments for images with the manual cropping from code above. Want this automated at threshold 63%.
0 个评论
采纳的回答
Image Analyst
2022-6-15
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 = 'red-laser-beam-grayscale.jpg';
% baseFileName = 'org.jpg';
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(:, :, 3);
else
grayImage = rgbImage;
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Display histogram
subplot(2, 2, 2);
histogram(grayImage, 256);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Binarize the image to get a mask.
% Get max gray scale
threshold = 0.63 * max(grayImage(:))
mask = grayImage > threshold;
% Display mask image.
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
drawnow;
caption = sprintf('Mask using a threshold of %.1f', threshold)
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Find upper left and lower right
[r, c] = find(mask);
yTop = min(r)
yBottom = max(r)
xLeft = min(c)
xRight = max(c)
% Crop gray scale image to that.
croppedGrayImage = grayImage(yTop:yBottom, xLeft:xRight);
% Display cropped gray scale image.
subplot(2, 2, 4);
imshow(croppedGrayImage);
axis('on', 'image');
drawnow;
title('Cropped Image', 'FontSize', fontSize, 'Interpreter', 'None');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1033345/image.png)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!