Boundary tracing in image with similar angles
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a script that is identifying angles within an image and overlaying a colored line (one unique color for each angle) to visualiza patterns of angles in the image (thanks to @Image Analyst). I'm now hoping to add borders that group together "clusters" of the same colors aka same angles. For example, something like the attached sketch. I've tried the boundary function, but can't get it to work quite right. Does anyone have any ideas on what might work?
My second question relates to the filter used to identify the edges in this code. I've been using a canny filter, but I'm noticing that it isn't quite picking up on all the pieces of wood in my imagery. I only want the filter to pick up the wood (not the vegetation and other features in the image).
This is the code for the angle orientation patterns and a sample image is attached:
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
% Just for the demo. If you put this into your own function you'll want to get rid of the close and clear commands.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
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;
rgbImage = imread('DJI_0022.jpg'); %inputs image
hFig = figure;
subplot(2, 2, 1);
imshow(rgbImage)
axis('on', 'image');
impixelinfo;
title('Original RGB Image', 'FontSize', 20);
grayImage = rgb2gray(rgbImage); %convers to grayscale
subplot(2, 2, 2);
imshow(grayImage)
axis('on', 'image');
impixelinfo;
title('Gray Scale Image', 'FontSize', 20);
edgeImage = edge(grayImage, 'Canny', 0.6);
subplot(2, 2, 3);
imshow(edgeImage)
axis('on', 'image');
title('Canny Edge Image', 'FontSize', 20);
hFig.WindowState = 'maximized';
drawnow;
% Get coordinates and plot a line over them.
imshow(grayImage)
axis('on', 'image');
title('Line Grouping', 'FontSize', 20);
hFig.WindowState = 'maximized';
drawnow;
props = regionprops(edgeImage, 'PixelList', 'Centroid');
lineLength = 20; % Whatever you want.
hold on;
lineColorMap = jet(181); % One color for every 1 degrees.
angles = zeros(1, length(props));
for k = 1 : length(props)
thisList = props(k).PixelList;
fprintf('\nFor blob #%d of %d:\n ', k, length(props))
thisx = thisList(:, 1);
thisy = thisList(:, 2);
coefficients = polyfit(thisx, thisy, 1); % Fit to a line
angles(k) = atand(coefficients(1 ));
% Get the centroid
x = props(k).Centroid(1);
y = props(k).Centroid(2);
% Get the line endpoints.
x1 = x + lineLength * cosd(angles(k));
x2 = x - lineLength * cosd(angles(k));
y1 = y + lineLength * sind(angles(k));
y2 = y - lineLength * sind(angles(k));
thisColor = lineColorMap(round(angles(k) + 90) + 1, :);
fprintf('Drawing line from (%.1f to %.1f) to (%.1f, %.1f)\n', x1, y1, x2, y2); line([x1,x2], [y1,y2], 'Color', thisColor);
if mod(k, 100) == 0
drawnow;
end
end
% Plot the histogram of angles.
subplot(2, 2, 4)
histObject = histogram(angles);
grid on;
xlabel('Angle', 'FontSize', 20);
ylabel('Count', 'FontSize', 20);
title('Histogram of angles', 'FontSize', 20);
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!