bwconncomp and regionprops: centroids not found for objects on the edge of the image, why not?
1 次查看(过去 30 天)
显示 更早的评论
Hi all
I'm measuring properties of objects in an image by converting it to a binary image with im2bw(), computing connected regions with bwconncomp() and finally measuring the properties with regionprops(). Right now I'm just finding centroids and areas.
My problem is that objects touching the edge of the image (e.g. containing pixels which are located on the edge of the image) are not getting their centroids computed. Below is the bw image with the centroids overlaid and as you can see there are not centroids for those edge objects.
The code for constructing this image is below. Both code (as .m file) and image (b19_1_1.png) are contained in the analyse_image.zip file - ready to run once extracted.
What am I doing wrong / not knowing? I need those edge objects detected as well (e.g. their centroids computed).
% Input file
file = '/tmp/b19_1_1.png';
% Read the image
img = imread(file);
% Smooth the image before thresholding
h = fspecial('average', [3 3]);
img_smooth = mat2gray(filter2(h, img));
% Threshold and invert colors
bw = imcomplement(im2bw(img_smooth, 0.87));
% Compute connected regions and properties
cc = bwconncomp(bw, 8);
s = regionprops(cc, {'area', 'centroid'});
% Centroid x and y coordinates
centroids = [s.Centroid];
x = centroids(1:2:end-1)';
y = centroids(2:2:end)';
% Show image and centroids
figure;
imshow(bw);
hold on;
plot(x,y, 'or')
0 个评论
采纳的回答
Image Analyst
2013-11-19
If you zoom in you'll see a single pixel wide line running around the outside of your image, so it's one giant square ring. The centroid of that is at the middle of the picture. Add these lines after you create bw to fix it:
% Erase single pixel line running around boundary.
bw(1,:) = false;
bw(end,:) = false;
bw(:,1) = false;
bw(:,end) = false;
3 个评论
Image Analyst
2013-11-19
That's what I had done first but because of subsampling to shrink it down for my display, it wasn't there. I didn't see the single pixel wide line until I zoomed way in.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!