Calculating each trees row and coulmn from an orchads image

1 view (last 30 days)
Hi,
I have an image of a trees orchard, after segmentation and regionprops function, I have a table with proporties for each tree:
My goal is to count the number of rows and to assign each tree its row and column (the number in the row).
The right image is the thermal image of the orchard, the left image contains the symbol * (colored red) in the center of each tree according to centroid x and y.
This is what I've tried so far, which kinda works but with very low accuracy:
while (~isempty(sortedData))
if row_num > 1
filteredData= sortedData;
end
sortedData = sortrows(filteredData,["CentroidY",'CentroidX'],{'descend','descend'});
longtitDelta = (sortedData{1:1,11}-sortedData{1:end,11});%Y
latitDelta = (sortedData{1:1,10}-sortedData{1:end,10});%X
angle=atand(longtitDelta./latitDelta);
angle(find(isnan(angle))) = 0;
sortedData.angle = angle;
abs(diff(sortedData.angle));
sortedData.angle(1:end-1) = abs(diff(sortedData.angle));
last_tree = find((sortedData.angle > Differentiate),1,'first');
if (isempty(last_tree))
% If there is no more rows found, the last tree is as the length of the left sorted data
last_tree = size(sortedData,1);
end
amount=amount+last_tree;
dataRows=sortedData(1:last_tree,1:end-1);
dataRows = sortrows(dataRows,'CentroidX','descend');
dataRows.rownum = repelem(row_num,last_tree)';
dataRows.Tree_num=(1:last_tree)';
dataWithRows(first_tree:amount,1:size(Trees,2)+2) = dataRows;
sortedData = sortedData(last_tree+1:end, :);
row_num = row_num + 1;
first_tree= amount + 1;
end
Any help or ideas how to solve this problem will be highly appreciated!

Answers (1)

Anurag
Anurag on 14 Dec 2023
Edited: Anurag on 15 Dec 2023
Hi Stav,
I understand that you are trying to assign row and column numbers to each tree in the dataset.
The most efficient algorithm to follow for this type of data is k-means clustering. This method can be more robust and can adapt to various orchard layouts without relying on manual differentiation of angles.
Please refer to the example code for implementation details:
%Sample data
CentroidX = [10, 15, 20, 25, 30, 35, 40, 45, 50];
CentroidY = [5, 10, 15, 20, 25, 30, 35, 40, 45];
% Create the dummy table
Trees = table(CentroidX', CentroidY', 'VariableNames', {'CentroidX', 'CentroidY'});
% Combine the X and Y centroids into a matrix
centroids = [Trees.CentroidX, Trees.CentroidY];
% Specify the number of rows you want
numRows = 5; % Change this to your desired number of rows
% Use kmeans to cluster the trees into rows
[idx, centroids] = kmeans(centroids, numRows);
% Create a new column in the table to store the row numbers
Trees.RowNum = idx;
% Sort the table based on row numbers and tree numbers within each row
SortedTrees = sortrows(Trees, {'RowNum', 'CentroidX'}, {'ascend', 'descend'});
% Display the table with row numbers and tree numbers
disp(SortedTrees(:, {'RowNum', 'CentroidX', 'CentroidY'}));
RowNum CentroidX CentroidY ______ _________ _________ 1 30 25 1 25 20 2 50 45 2 45 40 3 15 10 3 10 5 4 40 35 4 35 30 5 20 15
Some more algorithms that can be leveraged for the same task are listed below:
  • Hierarchical Clustering
  • Ordering Points to Identify the Clustering Structure (OPTICS)
For additional information please refer to the following documentation:
  2 Comments
Image Analyst
Image Analyst on 14 Dec 2023
Edited: Image Analyst on 14 Dec 2023
How do you "adapt to various orchard layouts without relying on manual differentiation of angles"? Because your code above won't classify them correctly since the orchard grid is rotated. I believe you're going to have to find the angle and then rotate it first, before using kmeans. I have my own ideas, but how are you doing it?
Image Analyst
Image Analyst on 15 Dec 2023
@Anurag not sure what your edit was but I don't see anything about rotating. Can you use @stav marzuk's original image and then use gscatter to overlay the rows or columns?
For what it's worth, you can use the Radon transform to determine the rotation angle, as shown in my attached demo.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!