Finding the edge of an image
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Seejal Padhi
el 25 de Oct. de 2022
Comentada: Image Analyst
el 22 de Sept. de 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 comentarios
Image Analyst
el 30 de Oct. de 2022
@Seejal Padhi did you overlook my answer below (scroll down)? Anything wrong with it?
Respuesta aceptada
Image Analyst
el 26 de Oct. de 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');
Más respuestas (0)
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!