Edge tracing - left side only
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi, I'm trying to trace the left edge of this image, I don't want the top portion, only the left side. I Have 2 methods listed below but neither are working quite the way I need it to. Any help would be greatly appreciated! Thank you.
clc;
clear;
close all;
I = imread('Frame4.jpg');
figure %figure 1
imshow(I)
hold on;
threshold = 69;
Igray = im2gray(I);
title('Original Image')
IBW = Igray>threshold;
figure %figure 1 BW
imshow(IBW) %threshold
title('Binarized Image 1')
%%Method 1
[rows, columns, numberOfColorChannels] = size(IBW);
lowThreshold = 1;
highThreshold = 500;
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(lowThreshold, highThreshold, grayImage)
mask = IBW >= lowThreshold & IBW <= highThreshold;
leftedge = nan(rows,1);
% topRows = nan(1, columns);
for row = 1 : rows
thisRow = mask(row,:);
% Find top row
t = find(thisRow,1);
if ~isempty(t)
leftedge(row) = t;
end
end
imshow(IBW)
hold on;
plot(leftedge,'r-', 'LineWidth', 2);
axis on
grid on
% METHOD 2
mask = false(size(IBW));
mask(300+200:650,130-50:200) = true;
b = visboundaries(mask,'color','r') ;
hold on ;
g = visboundaries(IBW(500-400:650,130-129:200),'color','b');
g.Children(1);
g.Children(2).Visible='off';
g.Children(1).LineWidth = 2;
grid on
axis on
hold on;
h = visboundaries(BB(200-199:450,90-89:130),'color','r'); %226-225:300
% h = visboundaries(BB(:,590:551-550),'color','r');
hold off
h.Children(1);
h.Children(2).Visible='off';
h.Children(1).LineWidth = 2;
grid on
axis on
댓글 수: 0
채택된 답변
Image Analyst
2023년 9월 22일
@Carolina Vazquez 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;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'frame 4.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
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)
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
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% 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';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------------------
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Get mask by thresholding at 128.
lowThreshold = 128;
highThreshold = 255;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Draw line at threshold. Put the threshold in the title of the histogram plot.
xline(lowThreshold, 'Color', 'r', 'LineWidth', 2);
caption = sprintf('Histogram of Image. Threshold at %d', lowThreshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Display initial mask.
subplot(2, 2, 3);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------------------
% Clean up the initial mask -- get rid of huge white frame around the edge of the image.
mask = imclearborder(mask);
%--------------------------------------------------------------------------------------------------------
% Optional: find areas of blobs so we can filter out small ones.
props = regionprops(mask, 'Area');
allAreas = sort([props.Area])
% Get rid of blobs smaller that pixels, like the noise at the top of the image.
mask = bwareaopen(mask, 100);
%--------------------------------------------------------------------------------------------------------
% Display the final mask
subplot(2, 2, 4);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Final Mask Image with Left Edge in Red', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Scan down row (line) by row, finding column of the left edge.
leftColumns = nan(rows,1);
% topRows = nan(1, columns);
for row = 1 : rows
thisRow = mask(row,:);
% Find left edge (location of first white pixel in this row).
t = find(thisRow,1, 'first');
if ~isempty(t)
leftColumns(row) = t;
end
end
hold on;
y = 1 : rows;
plot(leftColumns, y, 'r-', 'LineWidth', 3);
axis on
grid on
댓글 수: 5
Image Analyst
2023년 10월 8일
Find corresponding points and find the distance between them. But it depends on lots of things, like does the shape of the edge change a lot between frames, what direction does it move, does it rotate, etc.?
If you can't find corresponding points then you may have to find some point to use, like the midpoint or something. Or maybe the closest point to each initial frame that is in the subsequent frame.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!