Make background of binary image all black
이전 댓글 표시
Hi,
Im trying to segment an image of an apple such that i can extract the boundary of the apple, but i first need to make all the background of the binary image black, and am not sure on how to do this.
The test photo is this:

the resultant output is this:

The code:
a = imread('q2.jpg');
b=rgb2gray(a);
bw = imbinarize(b);
bw2 = imcomplement(bw);
c =imfill(bw2,'holes');
imshow(c)
채택된 답변
추가 답변 (1개)
Parth Dethaliya
2020년 12월 23일
This process totally depends upon the specfic image so it is really hard to generalize the method, but i am showing you the approach to tackle such situations.
By running a function "bwareafilt" on the final image you obtained yo may get this results,
% c is your final image (After filling holes)
largest = bwareafilt(c,1); % This command yeilds first largest component
imshow(largest);

largest = bwareafilt(c,2); % This command yeilds first two largest components
imshow(largest);

So it is obvious that the region you are interested is second largest, now simply subtracting those two results you can achieve the goal.
largest = bwareafilt(c,2) - bwareafilt(c,1) ;
imshow(largest);

카테고리
도움말 센터 및 File Exchange에서 Display 2-D Images에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

