cluster analysis from edge detection

16 views (last 30 days)
Hello,
I am looking to perform a cluster analysis (K-means) for an image. I've converted the original image to a binary image and performed an edge detection, which should help with the clustering, but am stuck on how to implement a cluster analysis from the binary image. Any suggestions are greatly appreciated! The code for the binary edge detection is below and the image is attached. The end goal here is to identify patterns of wood orientation.
clear
RGB=imread('DJI_0022.jpg'); %inputs image
I=rgb2gray(RGB); %convers to grayscale
figure
imshow(I)
BW1 = edge(I,'Canny',0.6);
imshow(BW1)

Accepted Answer

Image Analyst
Image Analyst on 3 May 2020
Assuming you have a bunch of blobs that are the tree edges, I'd call regionprops() and ask for 'Orientation'. This will give you the average angle of every blob. You can then histogram the angles if you want.
props = regionprops(edgeImage, 'Orientation');
allAngles = [props.Orientation];
histogram(allAngles);
grid on;
xlabel('Angle', 'FontSize', 20);
ylabel('Count', 'FontSize', 20);
  16 Comments
Anna Marshall
Anna Marshall on 4 May 2020
Interestingly, it does look like the Canny filter lines up well with the wood orientations in the image overlay. Looking back at the code, I'm wondering if the code to find the centroid angles before plotting those lines is what is creating a discrepancy in the colored lines?
Image Analyst
Image Analyst on 4 May 2020
You might try getting the PixelList, like I already showed you in your other post, and then put those x,y values into polyfit to get the angle. Something like
% Get coordinates and fit to a line.
props = regionprops(edgeImage, 'PixelList', 'Centroid');
for k = 1 : length(props)
thisList = props(k).PixelList;
fprintf('\nGetting angle for blob #%d of %d.\n ', k, length(props))
thisx = thisList(k2, 1);
thisy = thisList(k2, 2);
coefficients = polyfit(thisx, thisy, 1); % Fit to a line
angles(k) = atand(coefficients(1));
end

Sign in to comment.

More Answers (1)

Anna Marshall
Anna Marshall on 4 May 2020
I'll give it a try! Thanks!
  6 Comments
Anna Marshall
Anna Marshall on 4 May 2020
Thank you!! That works great. I'm going to play around with different filtersm particularly the ridgeline-finding filters and see if there is one that might work a bit better than the canny filter. Thanks again for all the suggestions!!
Anna Marshall
Anna Marshall on 14 May 2020
@Image Analyst- I have another question to throw your way! I've been playing around with this code and one thing that I'm looking to try and do is 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. Do you have any ideas on what might work?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!