how can I fill these bw images to generate a mask?
2 views (last 30 days)
Show older comments
Teshan Rezel
on 22 Aug 2022
Commented: Teshan Rezel
on 23 Aug 2022
Hi folks,
I am trying to threshold an image using edge detection. So far, I have the following 2 images which demonstrate the effectiveness of each of the edge detection techniques used. I am trying to turn the result into a mask by filling inside the perimeter, with no success!
My initial approach was to turn each black pixel to true starting from the bottom of each image until it reached a white pixel for each column, but I'm unsure of how to do this.
My code is below:
i = 1;
myParams = {'Sobel','Prewitt','Roberts','log','zerocross','Canny','approxcanny'};
x = imadjust(im2gray(imread([path, num2str(i), '.png'])));
for i = 1 : 7
bw{i} = myMask(x, myParams{i});
end
figure;
subplot(4, 3, 2);imshow(x);title('Original Greyscale Image');
for i = 1:7
if i == 7
subplot(4, 3, i+4);imshow(bw{i});title(myParams{i});
else
subplot(4, 3, i+3);imshow(bw{i});title(myParams{i});
end
end
function bw = myMask(bw, param)
bw = im2bw(bw);
bw = edge(bw, param, "nothinning");
bw = bwareaopen(bw, 3);
bw = imclose(bw, strel("disk", 10));
bw = imfill(bw, 'holes');
bw = bwperim(bw);
bw = imdilate(bw, ones(12));
bw = imerode(bw, ones(10));
bw = imfill(~bw, 'holes');
bw = ~bw;
end
0 Comments
Accepted Answer
Image Analyst
on 22 Aug 2022
It's totally unnecessary to do edge detection. Not sure why beginners always want to do edge detection just because they can see an edge. What you want to do is to simply threshold and scan to find the first zero pixel. For each gray scale image, just call imbinarize and don't do any edge stuff:
bw = imbinarize(grayImage);
[rows, columns, numberOfColorChannels] = size(bw);
bottomRows = nan(1, columns);
% Scan across
for col = 1 : columns
for row = rows : -1 : 1
if ~bw(row, col) % If it's false/black/zero
bottomRows(col) = row;
break;
end
end
end
There are several other ways to do this if you don't want a nested for loop.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!