Centroid Location is incorrect when edited in actual image data

5 views (last 30 days)
I'm trying to use the image processing toolbox to find the weighted centroid locations of a given binary image. Once I've found this the centroid plots correctly, however the x & y coordinates of the centroid location don't seem to correspond to the original image data directly.
When I change the value of the centroid location in the original image data and then show the edited image, the area I've edited is not where the centroid location was supposed to be.
imagedata = imread('C:\Program Files\MATLAB\R2009b\toolbox\images\imdemos\rice.png')
blackwhite = ~(imagedata<150);
[x,y,handles.ibw2] = bwselect(blackwhite, 4);
s = regionprops(handles.ibw2,'centroid');
centroids = cat(1,round(s.Centroid));
hold on
gcf
plot(centroids(:,1), centroids(:,2), 'b*');
axis image
Here it plots the centroid correctly on the figure. When using that same centroid information to edit the image however, it changes the location. Am I needing to normalize this somehow, or is there information about the centroid that I'm not understanding? Here's the simple code I used to change the binary value at the centroid location.
blackwhite(centroids(:,1),centroids(:,2)) = 1
imshow(blackwhite)

Accepted Answer

Sven
Sven on 1 Dec 2011
One place where the code does something different to what I think you intended is here:
blackwhite(centroids(:,1),centroids(:,2)) = 1
"centroids" as returned by the regionprops() function return X and Y coordinates. Note that this is different to I and J substitutive indices.
For example:
MAT = false(10);
MAT(2, 9) = true;
stats = regionprops(MAT,'Centroid')
returns: stats =
Centroid: [9 2]
Note that we set (2,9) to be on, but the centroid is reported as (9,2). That's because matrices are indexed in IJ coordinates, rather than XY.
Now there is a further confusion that could occur if centroids has more than one row. Let's try another small example:
MAT = false(5);
centroids = [1 2; 5 4]; % Presumed as XY coordinates
MAT(centroids(:,2),centroids(:,1)) = true % Note I reverse XY columns for IJ indices
There are two "coordinates" being set (at IJ substitutive indices of (2,1) and (4,5)). Note however that the result has 4 pixels turned on:
MAT =
0 0 0 0 0
1 0 0 0 1
0 0 0 0 0
1 0 0 0 1
0 0 0 0 0
This is because substitutive indices work like a mask. Any pixels with rows and columns matching any combination of the IJ masks gets turned on. I think you intended to use indices as follows:
MAT = false(5);
centroids = [1 2; 5 4]; % Presumed as XY coordinates
inds = sub2ind(size(MAT), centroids(:,2), centroids(:,1));
MAT(inds) = true
Now the result looks more like what you intended:
MAT =
0 0 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
  1 Comment
Micheal
Micheal on 1 Dec 2011
The reversal was the problem. I forgot that x is column value and y is row value. Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!