使用OpenCV在手识别程序中绘制凸度缺陷

时间:2014-11-06 18:03:48

标签: opencv image-processing

在尝试确定手的二进制图像中的凸度缺陷然后尝试绘制它们时,我遇到两个问题。

我看过其他类似的问题,但我找不到合适的解决方案。

1。由凸性缺陷的确定引起的误差。

正如您在本文底部的完整代码中所看到的,我使用以下代码来确定凸性缺陷。

if(hulldf.size()>3){convexityDefects(contours_poly[j],hulldf[j],defects[j]);}

当我运行程序时,我收到以下错误。

OpenCV Error: Assertion failed (ptnum > 3) in convexityDefects, file C:\opencv\m
odules\imgproc\src\contours.cpp, line 1969
terminate called after throwing an instance of 'cv::Exception'
what():  C:\opencv\modules\imgproc\src\contours.cpp:1969: error: (-215) ptnum
3 in function convexityDefects

我已尝试在条件hulldf.size()>3中增加该值3,但它间歇性地解决了问题,我不知道其背后的原因。是否有更好的解决方案永久修复它?

2。绘制凸起缺陷

通过绘制轮廓和凸包得到以下结果,但我不知道如何绘制凸度缺陷的点。 enter image description here

我想在凸起缺陷的坐标处以及凸壳的坐标处放置一个圆。但我不确切知道它是如何完成的。

我未能成功绘制凸度缺陷点的代码如下,

for(int p=0;p<contours.size();p++)
{
 Vec4i l = defects[p];
 circle(img,Point(l[0],l[1]),3,Scalar(0,0,250),2,8);
}

会出现以下错误

  

错误:从'std :: vector&gt;'转换要求非标量类型'cv :: Vec4i {aka cv :: Vec}'|

我该如何解决这个问题?

代码

    using namespace std;
using namespace cv;

int lH = 0;
int lS = 0;
int lV = 0;
int uH = 180;
int uS = 255;
int uV = 255;
int trakingStatus = 0;
double area;


void trackbar()
{
    namedWindow("thresh",CV_WINDOW_FREERATIO);
    createTrackbar("lowerH","thresh",&lH,180);
    createTrackbar("upperH","thresh",&uH,180);

    createTrackbar("lowerS","thresh",&lS,255);
    createTrackbar("upperS","thresh",&uS,255);

    createTrackbar("lowerV","thresh",&lV,255);
    createTrackbar("upperV","thresh",&uV,255);

    createTrackbar("Tracking","thresh",&trakingStatus,1);
}

void findArea(Mat &img)
{
Moments mu;
mu = moments(img,true);
area = mu.m00;
}

void filterImage(Mat &img)
{
Mat erodeElement  = getStructuringElement(MORPH_RECT,Size(3,3));
Mat dilateElement = getStructuringElement(MORPH_RECT,Size(8,8));

erode(img,img,erodeElement);
//erode(img,img,erodeElement);
dilate(img,img,dilateElement);
dilate(img,img,dilateElement);
}

int main()
{

trackbar();
VideoCapture cap(0);

namedWindow("main",CV_WINDOW_AUTOSIZE);

Mat img(600,600,CV_8UC3);
Mat imgG(600,600,CV_8UC3);
Mat img2(img.size(),CV_8SC3);
Mat hsv;
int c;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

while(1)
{
    cap >> img;
    flip(img,img,1);

    GaussianBlur(img,imgG,Size(9,9),2,2);

    cvtColor(imgG,hsv,CV_RGB2HSV);
    inRange(hsv,Scalar(lH,lS,lV),Scalar(uH,uS,uV),img2);
    filterImage(img2);
    findArea(img2);
    imshow("thresh",img2);

    if(trakingStatus == 1 && area>5000)
    {

    findContours(img2,contours,hierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,Point(0,0));
    vector<vector<Point> > hull(contours.size());
    vector<vector<int> > hulldf(contours.size());
    vector<vector<Point> > contours_poly(contours.size());
    vector<vector<Vec4i> > defects(contours.size());
    //vector<Vec4i> defects;
    for(int i=0;i < contours.size();i++)
    {
        approxPolyDP(contours[i],contours_poly[i],5,true);
    }
    for(int j=0;j<contours.size();j++)
    {
        convexHull(contours_poly[j],hull[j],false);
        convexHull(contours_poly[j],hulldf[j],false);
        if(hulldf.size()>20)
        {
            convexityDefects(contours_poly[j],hulldf[j],defects[j]);
        }
    }
    //drawing contours and convex hulls
    for(int k=0;k<contours.size();k++)
    {
        drawContours(img,contours_poly,k,Scalar(0,250,0),2,8,hierarchy,0,Point());
        drawContours(img,hull,k,Scalar(250,0,0),2,8,hierarchy,0,Point());
    }
    //plotting convexity defects points
    for(int p=0;p<contours.size();p++)
    {
        Vec4i l = defects[p];
        circle(img,Point(l[0],l[1]),3,Scalar(0,0,250),2,8);
    }
    }

    imshow("main",img);

    c=waitKey(33);
    if(c==27)
        break;
}
return 0;
}

0 个答案:

没有答案