MATLAB: How to find the centroid of different sections of an image

blkproccentroidgridsimage processingImage Processing ToolboxMATLAB

I have an image that I want to divide in three parts and find the centroid of the parts separately and display them on original image, I used blkproc for dividing the image in [1 3] grids, but can't display the centroids. Here is the code I wrote,
i=imread('F:\line3.jpg');
i2=rgb2gray(i);
bw=im2bw(i2);
imshow(bw)
fun=@(x) regionprops(x,'centroid');
b=blkproc(bw,[1 3],fun);
But I can't get to display the centroids, as well as get their values. Any help will be much appreciated.

Best Answer

The centroid will be at 1/4 and 3/4 of the way across the image. Perhaps you meant weighted centroid instead? Don't threshold and don't use blkproc(). Just create 4 binary images and call regionprops 4 time, one for each quadrant, something like (untested)
% Create binaryImage
[rows, columns] = size(i2);
binaryImage = false(rows, columns);
r1 = int32(floor(rows/2));
c1 = int32(floor(columns/2));
% Create binary image of upper left quadrant.
binaryImage = false(rows, columns);
binaryImage(1:r1, 1:c1) = true;
% Label



labeledImage = bwlabel(binaryImage);
% Now call regionprops



measurementsUL = regionprops(labeledImage, i2, 'WeightedCentroid');
% Create binary image of upper rightquadrant.
binaryImage = false(rows, columns);
binaryImage(1:r1, (c1+1):end) = true;
% Label
labeledImage = bwlabel(binaryImage);
% Now call regionprops
measurementsUR = regionprops(labeledImage, i2, 'WeightedCentroid');
% Create binary image of lower left quadrant.
binaryImage = false(rows, columns);
binaryImage((r1+1):end, 1:c1) = true;
% Label
labeledImage = bwlabel(binaryImage);
% Now call regionprops
measurementsLL = regionprops(labeledImage, i2, 'WeightedCentroid');
% Create binary image of lower rightquadrant.
binaryImage((r1+1):end, (c1+1):end) = true;
% Label
labeledImage = bwlabel(binaryImage);
% Now call regionprops
measurementsLR = regionprops(labeledImage, i2, 'WeightedCentroid');