Clear Filters
Clear Filters

I need to correct this code to fill each connected component in the image New_bw

2 views (last 30 days)
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;

Answers (3)

Walter Roberson
Walter Roberson on 23 Nov 2017
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 Comments
abdelrahim hashem
abdelrahim hashem on 23 Nov 2017
Edited: Walter Roberson on 23 Nov 2017
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 on 23 Nov 2017
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.

Sign in to comment.


Image Analyst
Image Analyst on 23 Nov 2017
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 Comments
abdelrahim hashem
abdelrahim hashem on 24 Nov 2017
Edited: abdelrahim hashem on 24 Nov 2017
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 on 24 Nov 2017
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.

Sign in to comment.


abdelrahim hashem
abdelrahim hashem on 24 Nov 2017
  5 Comments
abdelrahim hashem
abdelrahim hashem on 25 Nov 2017
Edited: abdelrahim hashem on 25 Nov 2017
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!