I need to correct this code to fill each connected component in the image New_bw
2 次查看(过去 30 天)
显示 更早的评论
clear all;
i = imread('resturant.jpg');
g = rgb2gray(i);
Canny_g = edge(g,'Canny');
bw = im2bw(g);
CC = bwconncomp(bw)
New_bw = zeros(size(g));
for j = 1:1:CC.NumObjects
Temp = zeros(size(g));
Temp(CC.PixelIdxList{j}) = 1;
New_bw = New_bw + Temp;
clear Temp;
end
figure, imshow(New_bw), title('image of components before'), hold on;
Regs = regionprops(CC,'BoundingBox','Centroid')
MatFill = zeros(10,1);
for i = 1:10 %CC.NumObjects
pts = Regs(i).BoundingBox;
plot(pts(1),pts(2),'*b');
MatFill(i,1) = round(pts(1));
MatFill(i,2) = round(pts(2));
end
figure, imshow(New_bw), title('image of components after'), hold on;
MatFill
ImFill = imfill(New_bw,MatFill,4);
figure, imshow(ImFill), title('after filling'), hold on;
回答(3 个)
Walter Roberson
2017-11-23
The "locations" parameter must be linear indices. https://www.mathworks.com/help/images/ref/imfill.html#inputarg_locations
MatFill(i) = sub2ind(size(CC, round(pts(2)), round(pts(1)) );
Note the bounding box is in X, Y order, but that indexing is in Y, X order.
I would point out, though, that you should not assume that the lower left corner of the bounding box of the object definitely is or definitely is not occupied. If it is not occupied then the imfill is going to fill around the connected component, everything over to the edge of the next connected component over.
I think you should be considering asking to fill holes instead.
7 个评论
Walter Roberson
2017-11-23
I am not sure at the moment. I have not tried to regionprops on a CC before.
I think I am going to bed now.
Image Analyst
2017-11-23
I'm not familiar with this:
ImFill = imfill(New_bw,MatFill,4);
usually I'd do
filledImage = imfill(binaryImage, 'holes'); % Fill all holes in all blobs.
Not sure why you're trying to compile a list of locations to fill. Please attach your restaurant.jpg image and I'll see what I can do.
6 个评论
Image Analyst
2017-11-24
You should have done:
New_bw = false(size(g));
instead of
New_bw = zeros(size(g));
That said, the code is still weird, but anyway I know you don't want anymore help since you haven't attached your image after two requests, so I assume it's working to your satisfaction (quirky as it is), so good luck with the rest of the project.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!