我正在做一个项目,我有一张手的二进制图像(如下图),我只想提取边界来训练机器学习算法。
由于我是实时实现的,并且边界内可能有很多错误轮廓,因此在 opencv 中使用轮廓提取技术会减慢程序的速度。
我尝试过使用形态闭合操作,然后进行简单的边缘检测,但是我仍然在外边界内发现了很多信息。
我正在使用 kinect 传感器来跟踪手部,因此无法使用预处理技术纠正这些误差值。
我写的试图解决这个问题的函数是:
Mat CvHandRegion::sampleBoundaryPoints(Mat input)
{
Mat sampledOut;
sampledOut.create(input.rows, input.cols, CV_8U);
sampledOut.setTo(0);
for( int x = 0; x < input.cols; x += C_SAMPLING_STEP)
{
for( int y = 0; y < input.rows; y += C_SAMPLING_STEP)
{
if(input.at<uchar>(y,x) == 255)
{
sampledOut.at<uchar>(y,x) = 255;
break;
}
}
}
for( int x = 0; x < input.cols; x += C_SAMPLING_STEP)
{
for( int y = input.rows-1; y > 0; y -= C_SAMPLING_STEP)
{
if(input.at<uchar>(y,x) == 255)
{
sampledOut.at<uchar>(y,x) = 255;
break;
}
}
}
for( int y = 0; y < input.rows; y += C_SAMPLING_STEP)
{
for( int x = 0; x < input.cols; x += C_SAMPLING_STEP)
{
if(input.at<uchar>(y,x) == 255)
{
sampledOut.at<uchar>(y,x) = 255;
break;
}
}
}
for( int y = 0; y < input.rows; y += C_SAMPLING_STEP)
{
for( int x = input.cols-1; x > 0; x -= C_SAMPLING_STEP)
{
if(input.at<uchar>(y,x) == 255)
{
sampledOut.at<uchar>(y,x) = 255;
break;
}
}
}
return sampledOut;
}
这里 C_SAMPLING_STEP 保持为 4。这个想法基本上是从所有方面到达并在检测到从 0 到 255 的变化时终止循环。
有没有其他方法可以更有效和更健壮?
更新:我尝试使用不同的半径进行形态关闭操作,MORPH_ELLIPSE 的 Size(5,5) 输出是:
和尺寸(7,7)
和大小(9,9)
这在一定程度上解决了这个问题,但是我不确定要使用哪个大小,因为数据中的噪声变化很大(取决于手的姿势)。