How to get centroid specific part of an image

6 ビュー (過去 30 日間)
Ajay Goyal
Ajay Goyal 2015 年 3 月 9 日
コメント済み: Ajay Goyal 2015 年 3 月 11 日
I am intended to compute centroid of a part of image. I am getting centroid of all parts of the image which makes me confused of selecting the right one. my code is:
I = imread('Murine_Tibia_Crosssection.png')
Ibw = im2bw(I);
Ibw = imfill(Ibw,'holes');
Ilabel = bwlabel(Ibw);
stat = regionprops(Ilabel,'centroid');
imshow(I); hold on;
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
end
please help me to get the right one.
  2 件のコメント
Andrew Newell
Andrew Newell 2015 年 3 月 10 日
I formatted your question. Please have a look at Markup Help.
Ajay Goyal
Ajay Goyal 2015 年 3 月 11 日
Christiaan Sir, Your help is deeply appreciated. You made my day since I was working on this problem for last 3 days. I have made some amendments in your code to get it fit to my need. clc;clear all;close all; J = imread('final1.jpg'); figure(1) imshow(J) I=imcomplement(J); figure(2) imshow(I) stat = regionprops(I,'centroid'); h = fspecial('unsharp'); I_filtered = imfilter(I,h); figure(3) imshow(I_filtered) level = graythresh(I_filtered); BlackWhite = im2bw(I_filtered, level); figure(4) imshow(BlackWhite); original = BlackWhite; filled = imfill(original, 'holes'); holes = filled & ~original; bigholes = bwareaopen(holes, 500); smallholes = holes & ~bigholes; new = original | smallholes; figure(5) imshow(new); stat = regionprops(new,'centroid'); hold on; for x = 1: numel(stat) plot(stat(x).Centroid(1),stat(x).Centroid(2),'b*','linewidth',2); end Thanks Again

サインインしてコメントする。

採用された回答

Christiaan
Christiaan 2015 年 3 月 10 日
編集済み: Christiaan 2015 年 3 月 10 日
Dear Ajay,
If an image shows multiple centroids, there are some tricks to still determine the centroid. First you can filter the image using the function medfilt2. Then you can make the image black and white with the function im2bw. Finally you can fill holes when the size of the hole is smaller than a specific size with the function bwareaopen.
See the code below for an example:
clc;clear all;close all;
I = imread('Murine_Tibia_Crosssection.png');
figure(1)
imshow(I)
stat = regionprops(I,'centroid');
I_filtered = medfilt2(I, [12 12]);
figure(2)
imshow(I_filtered)
level = graythresh(I_filtered)
BlackWhite = im2bw(I_filtered, level);
figure(3)
imshow(BlackWhite);
original = BlackWhite;
filled = imfill(original, 'holes');
holes = filled & ~original;
bigholes = bwareaopen(holes, 500);
smallholes = holes & ~bigholes;
new = original | smallholes;
figure(4)
imshow(new);
stat = regionprops(new,'centroid');
hold on; for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro','linewidth',5);
end
hold off
Good luck! Christiaan

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 3 月 10 日
My Image Segmentation Tutorial shows you how to get centroids.

Ajay Goyal
Ajay Goyal 2015 年 3 月 11 日
Christiaan Sir, Your help is deeply appreciated. You made my day since I was working on this problem for last 3 days. I have made some amendments in your code to get it fit to my need. clc;clear all;close all; J = imread('final1.jpg'); figure(1) imshow(J) I=imcomplement(J); figure(2) imshow(I) stat = regionprops(I,'centroid'); h = fspecial('unsharp'); I_filtered = imfilter(I,h); figure(3) imshow(I_filtered) level = graythresh(I_filtered); BlackWhite = im2bw(I_filtered, level); figure(4) imshow(BlackWhite); original = BlackWhite; filled = imfill(original, 'holes'); holes = filled & ~original; bigholes = bwareaopen(holes, 500); smallholes = holes & ~bigholes; new = original | smallholes; figure(5) imshow(new); stat = regionprops(new,'centroid'); hold on; for x = 1: numel(stat) plot(stat(x).Centroid(1),stat(x).Centroid(2),'b*','linewidth',2); end Thanks Again

Community Treasure Hunt

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

Start Hunting!

Translated by