用于从手部图像中提取ROI的Matlab代码

时间:2014-09-16 14:33:34

标签: matlab image-processing

我正在研究应该用来处理手部图像的matlab代码,我需要从背景中分割对象(手),我使用阈值来做到这一点,下一步是从分割对象中提取特定的补丁(我看到有可能使用imellipse或imfreehand手动完成,但实际上我想自动为100张图像做。 你能帮我解决这个问题吗?是否有代码可以帮助选择具有特定尺寸的补丁进行进一步处理,并在选择所需补丁后如何删除其他部分并保存新结果?

image size is 240*320 grayscale as shown

代码,这里我展示了imrect的用法,但它给了我同样的手动选择,我不知道如何继续只保存我选择的区域?

x = imread ('0001hv1.bmp');    
b = im2double(x);

thresh_level = graythresh(b);
c = b > thresh_level;    
imshow(c);

d = im2double(c);    
d = im2double(c).*b;

imshow(d , []);

figure, imshow(d);

h = imrect(gca, [10 10 100 100]);

addNewPositionCallback(h,@(p) title(mat2str(p,3)));

fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));

setPositionConstraintFcn(h,fcn); 

1 个答案:

答案 0 :(得分:1)

你几乎让它正常工作。对图像进行阈值处理后,您可以通过查找最小和最大行和列值来确定手的边界框,这些值对应于手的左上角和右下角。因此,您需要做的就是确定像素为白色的位置,然后找到最小和最大的行和列值。

我会使用你的一些代码,但我会插入我最后会做的事情。请注意,我裁剪了顶部和底部10个像素,以及左右10个像素,因为当您对图像进行阈值处理时,似乎会出现某种不均匀的边框:

x = rgb2gray(imread('http://i.stack.imgur.com/VmnLv.jpg')); %// Read in your image from StackOverflow
x = im2double(x(10:end-10,10:end-10)); %// Crop out a 10 pixel border and convert to [0,1]
thresh_level = graythresh(x); %// Threshold the image
c = x > thresh_level;

%// NEW Code
%//----------
%//Find minimum spanning bounding box
[rows,cols] = find(c);
top_left_x = min(cols(:));
top_left_y = min(rows(:));
bottom_right_x = max(cols(:));
bottom_right_y = max(rows(:));

%//Determine width of bounding box
width = bottom_right_x - top_left_x + 1;
height = bottom_right_y - top_left_y + 1;

%// Draw rectangle onto image
imshow(x);
h = imrect(gca, [top_left_x top_left_y width height]);
%// End NEW Code
%// -----------

%// From your code... don't know what this is doing actually...
addNewPositionCallback(h,@(p) title(mat2str(p,3)));    
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));    
setPositionConstraintFcn(h,fcn); 

请注意,imrect需要一个要绘制矩形的图形的手柄,在您的情况下,您需要显示给用户的原始手。它还需要一个表示矩形的四元素数组,以便遵循以下约定:

[top_left_x top_left_y width height]

top_left_xtop_left_y是矩形左上角的列和行位置,而widthheight是此矩形的宽度和高度。因此,这完全对应于手掌的边界框。

这就是我得到的:

enter image description here