How to find the centroid of different sections of an image?

3 views (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 5 Nov 2013
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');
  3 Comments
Shah
Shah on 5 Nov 2013
It worked, but how do I modify this for three sections instead of quadrants, that is top, middle and bottom portion. And thanks for your answer.
Image Analyst
Image Analyst on 5 Nov 2013
Just create a new binary image with the section you want true:
binaryImage = false(rows, columns);
binaryImage(row1:row2, :) = true;
Change row1 and row2 according to the third you want. They'll equal 1, rows/2, 2*rows/3, and end.

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!