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
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 个评论
abdelrahim hashem
abdelrahim hashem 2017-11-23
编辑:Walter Roberson 2017-11-23
OK, but still error in using:
for i = 1:CC.NumObjects
pts = Regs(i).BoundingBox;
MatFill(i) = sub2ind(size(g), round(pts(2)), round(pts(1)));
end
%%%%gives "Out of range subscript".
Walter Roberson
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
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 个评论
abdelrahim hashem
abdelrahim hashem 2017-11-24
编辑:abdelrahim hashem 2017-11-24
Now I knew the error; I have to convert the matrix of binary image into logical. When I convert it to logical, the next command has run:
BW = logical(BW);
BW2 = imfill(BW,XY_indices,8); XY_indices is the index of row and column of corner of all Bounding Boxes
Image Analyst
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.

请先登录,再进行评论。


abdelrahim hashem
abdelrahim hashem 2017-11-24
  5 个评论
abdelrahim hashem
abdelrahim hashem 2017-11-25
编辑:abdelrahim hashem 2017-11-25
Thanks, Image Analyst. I depended on the number of pixels in each region/objects in my previous work, Already I have published two papers in this direction, though I think that I need other properties/features to make good selection for text character regions and also to discard non-text character regions rather than number of pixels. Thanks again

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by