how can I fill these bw images to generate a mask?

조회 수: 1(최근 30일)
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});
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});
subplot(4, 3, i+3);imshow(bw{i});title(myParams{i});
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;

채택된 답변

Image Analyst
Image Analyst 2022년 8월 22일
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;
There are several other ways to do this if you don't want a nested for loop.
  댓글 수: 1
Teshan Rezel
Teshan Rezel 2022년 8월 23일
@Image Analyst fantastic stuff (as always), thank you!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by