在matlab中填充手​​形对象

时间:2013-11-17 10:50:43

标签: matlab image-processing

我正在尝试使用matlab填充imfill中的“手”对象。对象没有连接在底部,所以我画了一条线并保存了它。现在该对象已完成,我正在尝试使用imfill( ,'holes');填充它,但它无效。请有人帮帮我....

这是图片:

enter image description here

我的代码:

I = imread('45Hand.jpg');
im=rgb2gray(I);
[junk threshold] = edge(im, 'sobel');
fudgeFactor = 2;
BWs = edge(im,'sobel', threshold * fudgeFactor);
%figure, imshow(BWs), title('binary');
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
figure, imshow(BWsdil), title('dilated gradient mask');
hold on
p1=[700,100];
p2=[700,1200];
plot([p1(2),p2(2)],[p1(1),p2(1)],'Color','w','LineWidth',4);

f = getframe(gca);
BWsdil = frame2im(f);

imwrite(BWsdil, 'image2.jpg');
img = imread('image2.jpg');
figure, imshow(img);
BWdfill = imfill(img, 'holes');
figure, imshow(BWdfill);

1 个答案:

答案 0 :(得分:1)

尝试此操作,如果线路调整出现问题,请将其设置回[700,100]和[700,1200]
#I have changed it to some different value. (see code)

I = imread('45Hand.jpg');
im=rgb2gray(I);
[h,w]=size(im);
[junk,threshold] = edge(im, 'sobel');
fudgeFactor = 2;
BWs = edge(im,'sobel', threshold * fudgeFactor);
%figure, imshow(BWs), title('binary');
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
figure, imshow(BWsdil), title('dilated gradient mask');
hold on
p1=[h,0]; %  To draw a line on the bottom edge.
p2=[h,w];
plot([p1(2),p2(2)],[p1(1),p2(1)],'Color','w','LineWidth',4);

f = getframe(gca);
BWsdil = frame2im(f);

imwrite(BWsdil, 'image2.jpg');
img = imread('image2.jpg');
img = im2bw(img, graythresh(img));

figure, imshow(img);
BWdfill = imfill(img, 'holes');
figure, imshow(BWdfill);


这样你就可以得到满满的手...... ....

问题是您尝试在imfill(bw)图片上使用grayscale,但它适用于二进制图片...请参阅doc here

现在输出图像如下:
filled hand