Finding the edge of an image

6 views (last 30 days)
Seejal Padhi
Seejal Padhi on 25 Oct 2022
Commented: Image Analyst on 22 Sep 2023
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 Comments
Image Analyst
Image Analyst on 30 Oct 2022
@Seejal Padhi did you overlook my answer below (scroll down)? Anything wrong with it?
Seejal Padhi
Seejal Padhi on 31 Oct 2022
Thank you sm! It worked really well. I appreciate your help!

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Oct 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 = 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 Comments
Image Analyst
Image Analyst on 22 Sep 2023
Great. I'll get you code this morning.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!