clear;close all;clc;I = imread('d.png');G = rgb2gray(I); %灰度化level = graythresh(I); %设定阈值BW = im2bw(I,level); %二值化BW_edge = edge(BW,'canny');dim =size(BW);i = floor(dim(1)/2);for m = 1:dim(2) if BW_edge(i,m)==1 dimen = [i,m]; break; endendcontour = bwtraceboundary(BW_edge,dimen,'N');background = zeros(size(BW));%imshow(background);%hold on;%plot(contour(:,2),contour(:,1),'g','LineWidth',1);sizeOfContour = size(contour);for m=1:sizeOfContour(1) coordinate = contour(m,:); x=coordinate(1); y=coordinate(2); background(x,y) = 1;endimshow(background);clear;close all;clc;I = imread('d.png');G = rgb2gray(I); %灰度化level = graythresh(I); %设定阈值BW = im2bw(I,level); %二值化BW_edge = edge(BW,'canny');dim =size(BW);i = floor(dim(1)/2);for m = 1:dim(2) if BW_edge(i,m)==1 dimen = [i,m]; break; endendcontour = bwtraceboundary(BW_edge,dimen,'N');background = zeros(size(BW));%imshow(background);%hold on;%plot(contour(:,2),contour(:,1),'g','LineWidth',1);sizeOfContour = size(contour);for m=1:sizeOfContour(1) coordinate = contour(m,:); x=coordinate(1); y=coordinate(2); background(x,y) = 1;endimshow(background);