Size filtering and show the position of centroids on the image

조회 수: 7 (최근 30일)
Hassan
Hassan 2011년 12월 17일
편집: Image Analyst 2021년 12월 20일
I have a binary image, BW and a grayscale image, I. I want to select only regions in the binary image with the same value that have an area > 50. For example there are 70 regions with area>50 and BW==1. I wonder how to show the position of the centroids of these regions with their number (1:70) on the image. Any suggestions?

채택된 답변

Image Analyst
Image Analyst 2011년 12월 17일
편집: Image Analyst 2021년 12월 20일
Size filtering and finding and displaying centroids is done in my demo "BlobsDemo" as well as some other useful things.
Otherwise, here's a shortened demo:
% Demo by Image Analyst, December, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
numSpaces = 6;
myBoard = zeros(numSpaces+1,numSpaces+1);
computersBoard = zeros(numSpaces+1,numSpaces+1);
% Read in sample image
grayImage = imread('kobi.png');
if ndims(grayImage) >= 3
grayImage = rgb2gray(grayImage);
end
% Resize this image to get our blobs in the range where 50 is a good blob area.
grayImage = imresize(grayImage, 0.6);
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo
title('Original Image', 'FontSize', fontSize);
% Get histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Gray Level Histogram', 'FontSize', fontSize);
% Get mask
threshold = 65;
xline(threshold, 'Color', 'r', 'LineWidth', 2)
mask = grayImage < threshold;
% Fill holes
mask = imfill(mask, 'holes');
% Erode to separate them.
mask = imerode(mask, true(11));
subplot(2, 2, 3);
imshow(mask);
% Get areas of this mask
props = regionprops(mask, 'Area', 'Centroid');
allAreas = sort([props.Area])
centroids = vertcat(props.Centroid);
% Make title
caption = sprintf('%d blobs ranging from %d to %d pixels', length(props), min(allAreas), max(allAreas))
title(caption, 'FontSize', fontSize);
% Place crosshairs on the centroids.
xc = centroids(:, 1);
yc = centroids(:, 2);
hold on
plot(xc, yc, 'r+', 'MarkerSize', 15, 'LineWidth', 2);
% Now extract only those blobs with area >= 50 pixels using bwareaopen() or bwareafilt().
mask = bwareaopen(mask, 50);
subplot(2, 2, 4);
imshow(mask);
% Now redo the analysis.
% Get areas of this mask
props = regionprops(mask, 'Area', 'Centroid');
allAreas = sort([props.Area])
% Make title
caption = sprintf('%d blobs ranging from %d to %d pixels', length(props), min(allAreas), max(allAreas))
title(caption, 'FontSize', fontSize);
% Mark centroids on the image
for k = 1 : length(props)
xc = props(k).Centroid(1);
yc = props(k).Centroid(2);
caption = sprintf('+ #%d at (%.1f, %.1f)', k, xc, yc);
text(xc, yc, caption, 'Color', 'r', 'FontWeight', 'bold')
end
g = gcf;
g.WindowState = 'maximized'

추가 답변 (3개)

bym
bym 2011년 12월 17일
from the documentation:
I = imread('coins.png');
figure, imshow(I)
bw = im2bw(I, graythresh(getimage));
figure, imshow(bw)
bw2 = imfill(bw,'holes');
L = bwlabel(bw2);
s = regionprops(L, 'centroid');
centroids = cat(1, s.Centroid);
%Display original image and superimpose centroids.
imshow(I)
hold(imgca,'on')
plot(imgca,centroids(:,1), centroids(:,2), 'r*')
hold(imgca,'off')
  댓글 수: 3
Image Analyst
Image Analyst 2011년 12월 18일
Obviously then you didn't look up my BlobsDemo. Any reason why not?
Hassan
Hassan 2011년 12월 19일
thanks Image Analyst. your code was quite useful.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 12월 18일
If centroid #K is at position x, y, then to label that point on the graph with that number, use
text(x,y,num2str(K));
  댓글 수: 1
Hassan
Hassan 2011년 12월 19일
thanks Walter. this was what I wanted to complete proecsm's code for my work.

댓글을 달려면 로그인하십시오.


Devie Nur AIni
Devie Nur AIni 2021년 12월 20일
편집: Image Analyst 2021년 12월 20일
Ada yang tau ga kenapa pas coba aku running dia eror dan tulisannya
"check for missing argument or incorrect argument data tupe in call to function 'centroid'
% Peroleh pusat massa dan letakkan di tengah citra
[xc, yc] = centroid(m,n);
xc = round(xc);
yc = round(yc);
xc = xc - round((n/2));
yc = yc - round((m/2));
  댓글 수: 1
Image Analyst
Image Analyst 2021년 12월 20일
@Devie Nur AIni, you have evidently, according to the error message, created a function called centroid, and that function does not expect two inputs m and n. Place your cursor in there and type control-D to edit that function. Or else do this on the command line:
>> edit centroid.m
I don't know what that function is supposed to do. Did you write it, or did someone else write it and you just put it on your computer?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by