MATLAB: How can i find out where the centriod is present in a grided image

centroiddigital image processinggridsimage acquisitionimage analysisimage processingimage segmentationMATLABroi

I have taken an image from the camera and generated grids on it. How can i know in which grid my centroid is pesent. I need to generate a variable or a number which i can use further. For example the centroid is in the 3rd box it generates 3 and i can send it to arduino using serial communication for futher action. I hope you get the idea. Can ROI be helpful in this case if yes then how can i use it ?
any help ?

Best Answer

If anyone is looking for the solution you can do this if you have one centroid in the image
If you want to get the grid row and column numbers, you should plot the grid with predefined grid row and column width:
rowWidth = 52; % Grid row width in pixels
colWidth = 53; % Grid column width in pixels
[rows, columns, ~] = size(Image);
for row = 1 : rowWidth : rows
line([1, columns], [row, row], 'Color', 'r');
end
for col = 1 : colWidth : columns
line([col, col], [1, rows], 'Color', 'r');
end
To get the the grid row and column numbers (assuming you have only one centroid):
% Get the row and column of the centroid (in grid units)
centroidGridCol = ceil(stat(1).Centroid(1) / colWidth);
centroidGridRow = ceil(stat(1).Centroid(2) / rowWidth);