用 Matlab 检测指谷
detecting finger valleys with Matlab
我有一个这样的手的二值图像:
我必须编写一个 Matlab 函数来检测两个手指之间的谷。
参数为二值图像和两指尖坐标
我是图像处理的新手,不知道如何开始。
我建议将两个输入点之间的黑色区域隔离开,然后在这个连通分量中找到最高点。
您可以尝试以下方法(您可能需要调整一些参数,但这应该是一个好的开始)。
I = rgb2gray(imread('<your path>'));
%input parameters - points which represents two finger tips.
x1 = 408; y1 = 441;
x2 = 454; y2 = 373;
%binarize image
I = im2bw(I);
%noise reduction - close holes
I2 = imclose(I,strel('disk',10));
%draw a line between p1 and p2
ind = drawline([y1 x1],[y2 x2],size(I));
lineMat = zeros(size(I));
lineMat(ind) = 1;
%adds the line to the image
I2 = I2 | lineMat;
%finds a point in the middle of the line
[lineY, lineX] = ind2sub(size(I),ind);
midX = lineX(ceil(length(ind)/2));
midY = lineY(ceil(length(ind)/2));
%finds a point which resides in the connected component which is between
%the line and the two finger.
xSeed = midX;
ySeed = midY -5;
%perform imfill operation, starting from (xSeed,ySeed),
%in order to find the conected component in which the point (xSeed,ySeed)
%resides.
diffMat = imfill(I2,[ySeed xSeed])~=I2;
%finding the highest point in this connected component
[Y, X] = ind2sub(size(diffMat),find(diffMat));
minInd = find(Y==min(Y),1,'first');
yValley = Y(minInd);
xValley = X(minInd);
%presents result
imshow(I);hold on;
plot(x1,y1,'r.','MarkerSize',20);
plot(x2,y2,'r.','MarkerSize',20);
plot(xValley,yValley,'b.','MarkerSize',20);
*画线函数取自drawline webpage.
最终结果(红色输入点,蓝色输出点)。
只是算法而已,但是MatLab中肯定有这些功能:
- 计算凸包
- 计算差异:凸包减去原始形状。然后你就有了你要找的所有山谷,加上一些小图案。
- 连通分量标记。
- 删除小部件。然后手指之间就有了所有的山谷。
- 然后您可以select使用指尖坐标选择您想要的那个。
我有一个这样的手的二值图像:
我必须编写一个 Matlab 函数来检测两个手指之间的谷。 参数为二值图像和两指尖坐标
我是图像处理的新手,不知道如何开始。
我建议将两个输入点之间的黑色区域隔离开,然后在这个连通分量中找到最高点。 您可以尝试以下方法(您可能需要调整一些参数,但这应该是一个好的开始)。
I = rgb2gray(imread('<your path>'));
%input parameters - points which represents two finger tips.
x1 = 408; y1 = 441;
x2 = 454; y2 = 373;
%binarize image
I = im2bw(I);
%noise reduction - close holes
I2 = imclose(I,strel('disk',10));
%draw a line between p1 and p2
ind = drawline([y1 x1],[y2 x2],size(I));
lineMat = zeros(size(I));
lineMat(ind) = 1;
%adds the line to the image
I2 = I2 | lineMat;
%finds a point in the middle of the line
[lineY, lineX] = ind2sub(size(I),ind);
midX = lineX(ceil(length(ind)/2));
midY = lineY(ceil(length(ind)/2));
%finds a point which resides in the connected component which is between
%the line and the two finger.
xSeed = midX;
ySeed = midY -5;
%perform imfill operation, starting from (xSeed,ySeed),
%in order to find the conected component in which the point (xSeed,ySeed)
%resides.
diffMat = imfill(I2,[ySeed xSeed])~=I2;
%finding the highest point in this connected component
[Y, X] = ind2sub(size(diffMat),find(diffMat));
minInd = find(Y==min(Y),1,'first');
yValley = Y(minInd);
xValley = X(minInd);
%presents result
imshow(I);hold on;
plot(x1,y1,'r.','MarkerSize',20);
plot(x2,y2,'r.','MarkerSize',20);
plot(xValley,yValley,'b.','MarkerSize',20);
*画线函数取自drawline webpage.
最终结果(红色输入点,蓝色输出点)。
只是算法而已,但是MatLab中肯定有这些功能:
- 计算凸包
- 计算差异:凸包减去原始形状。然后你就有了你要找的所有山谷,加上一些小图案。
- 连通分量标记。
- 删除小部件。然后手指之间就有了所有的山谷。
- 然后您可以select使用指尖坐标选择您想要的那个。