如何从凸起缺陷点OpenCV中绘制最小的圆

时间:2014-05-30 06:18:18

标签: c++ macos opencv

我一直在使用OpenCV进行手部识别,我得到了这个代码,我可以找到轮廓,凸包和外壳。一些缺陷点。但我现在无法从缺陷点绘制最小的圆(只是轮廓)。我想知道如何使用像缺陷点这样的特定点来绘制圆形轮廓。顺便说一句,我使用的是XCode 5.1&对不起,我的英语不好。谢谢!

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cvaux.h>

using namespace cv;
using namespace std;


Mat image, gray_image, blur_image, binary_image;
int thresh = 90;

int main( int argc, char** argv){
    namedWindow("Window", CV_WINDOW_AUTOSIZE);
    image = imread("/Users/Jamesbond/Desktop/hand.png");
    cvtColor(image, gray_image, CV_BGR2GRAY);
    medianBlur(gray_image, blur_image, 11);
    Canny(blur_image, binary_image, 90, 180, 3);

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(binary_image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));

    if(contours.size()>0){
        vector<vector<int>>hull(contours.size());
        vector<vector<Vec4i>>convDef(contours.size());
        vector<vector<Point>>hull_points(contours.size());
        vector<vector<Point>> defect_points(contours.size());

        for( int i = 0; i < contours.size(); i++ )
        {
            convexHull( contours[i], hull[i], false );
            convexityDefects( contours[i],hull[i], convDef[i]);

            for(int k=0;k<hull[i].size();k++)
            {
                int ind=hull[i][k];
                hull_points[i].push_back(contours[i][ind]);
            }

            for(int k=0;k<convDef[i].size();k++)
            {
                int ind = convDef[i][k][2];
                defect_points[i].push_back(contours[i][ind]);
                circle(image,contours[i][ind],5,Scalar(255,0,0),-1);

                drawContours( image, contours, i, Scalar(255,0,255), 2, 8, vector<Vec4i>(), 0, Point());
                drawContours( image, hull_points, i, Scalar(255,255,0), 2, 8, vector<Vec4i>(), 0, Point());
            }
        }

        imshow("Window", image);
        waitKey(0);
    }
}

1 个答案:

答案 0 :(得分:1)

尚未测试过,但它应该是:

        // collect the defects:
        //
        for(int k=0;k<convDef[i].size();k++)
        {
            int ind = convDef[i][k][2];
            defect_points[i].push_back(contours[i][ind]);
            circle(image,contours[i][ind],5,Scalar(255,0,0),-1);
            drawContours( image, contours, i, Scalar(255,0,255), 2, 8, vector<Vec4i>(), 0, Point());
            drawContours( image, hull_points, i, Scalar(255,255,0), 2, 8, vector<Vec4i>(), 0, Point());
        }

        // draw smallest circle around defects:
        //
        Point center;
        float radius;
        minEnclosingCircle( defect_points, center, radius );
        circle(image,center,radius,Scalar(255,0,0),3);