Finding the edge of an image

조회 수: 1 (최근 30일)
Seejal Padhi
Seejal Padhi 2022년 10월 25일
댓글: Image Analyst 2023년 9월 22일
Hello! I have an image, and I want to calculate the edge of it (as seen in red),. Then I want to take that edge and calculate the local min and max values of that edge. How would I go about doing this? Image is attached with and without annotation!
  댓글 수: 4
Image Analyst
Image Analyst 2022년 10월 30일
@Seejal Padhi did you overlook my answer below (scroll down)? Anything wrong with it?
Seejal Padhi
Seejal Padhi 2022년 10월 31일
Thank you sm! It worked really well. I appreciate your help!

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2022년 10월 26일
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 = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'wr_image.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
grayImage = 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(grayImage)
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(1, 3, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
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 = grayImage(:, :, 3);
end
% Update 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(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Threshold the image to get the bright blobs.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
lowThreshold = 31;
highThreshold = 255;
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(lowThreshold, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Take largest blob only.
mask = bwareafilt(mask, 1);
subplot(1, 3, 2);
imshow(mask)
title('Binary Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Get the top row for each column
topRows = nan(1, columns);
for col = 1 : columns
thisColumn = mask(:, col);
% Find top row
t = find(thisColumn, 1, 'first');
if ~isempty(t)
topRows(col) = t;
end
end
% Now overlay it onto the image
subplot(1, 3, 3);
imshow(grayImage)
hold on;
plot(topRows, 'r-', 'LineWidth', 4)
title('Top Row in Red', 'FontSize', fontSize, 'Interpreter', 'None');
  댓글 수: 6
Carolina Vazquez
Carolina Vazquez 2023년 9월 21일
Image Analyst
Image Analyst 2023년 9월 22일
Great. I'll get you code this morning.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by