how to fill the black regions inside the white region? imfill() is not working.

조회 수: 7 (최근 30일)
Pradeep Gowda
Pradeep Gowda 2015년 9월 19일
댓글: Image Analyst 2015년 9월 19일
this is the image.

답변 (1개)

Hamoon
Hamoon 2015년 9월 19일
imfill does not work because it thinks that your image is like this:
Actually this algorithm is not as smart as you are, and it doesn't know, that it's a retina image an it should be circular. So if you just want to fill the area for this image, you can write this:
img = imread('mm.png'); % your image
img2 = false(size(img)+2);
img2(2:end-1,2:end-1)=img; % your image with 1 pixel padding from each side
% you can also use padarray function
img2(1,:)=true;
img2(end,:)=true; % let first and end rows of your new image be "1"
img3 = imfill(img2,'holes'); % use imfill for holes
outputImage = img3(2:end-1,2:end-1); % eliminate padding pixels
imshow(outputImage)
so img2 will be considered something like this:
---
and the output image is like this:
---
But it only works for this image or images like this, I just wanted to show you what the problem is.
  댓글 수: 3
Hamoon
Hamoon 2015년 9월 19일
It is hard to talk about generalization when you are dealing with image processing problems, but yes you can use dilation ( using imdilate in Matlab) to extend your image and then use erosion ( using imerode in Matlab) to erode it. dilation extend your object to all directions and this will fill the holes, but other areas of your object will extend too, so you need to erode it to have your object in the same size as it was in the first place. If you choose proper morphological structuring element ( strel) with proper size, then you will get an acceptable output.
Image Analyst
Image Analyst 2015년 9월 19일
Erosion, dilation, opening, and closing will all change the shape of the outline(s), unlike hole filling. You could use bwconvhull() but that would change the shape of that little nub sticking out of the -45 degrees location.

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

Community Treasure Hunt

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

Start Hunting!

Translated by