1

我正在做我的项目,但我遇到的一个问题是我不知道如何只从之前检测过的正方形区域中检测关键点。下面是我的演示,到目前为止,我的代码将检测正方形内外的关键点:https ://www.youtube.com/watch?feature=player_embedded&v=3U8V6PhMnZ8

这是我找到正方形的代码:

       const int threshold_level = 2;
    for (int l = 0; l < threshold_level; l++)
    {

        gray = gray0 >= (l+1) * 255 / threshold_level;

        // Find contours and store them in a list
        findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        // Test contours
        vector<Point> approx;
        for (size_t i = 0; i < contours.size(); i++)
        {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation
                if (approx.size() == 4 &&
                        fabs(contourArea(Mat(approx))) > 3000 &&
                        isContourConvex(Mat(approx)))
                {
                        double maxCosine = 0;

                        for (int j = 2; j < 5; j++)
                        {
                                double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                                maxCosine = MAX(maxCosine, cosine);
                        }

                        if (maxCosine < 0.3)
                                squares.push_back(approx);
                }
        }
    }

这是我绘制正方形和角点的代码:

 const Point* p = &squares[i][0];
        int n = (int)squares[i].size();       
        Point p1 = squares[i][0];
        Point p2 = squares[i][1];
        Point p3 = squares[i][2];
        Point p4 = squares[i][3];
        cout<<"p1 is "<<p1<<" p2 is "<<p2<<" p3 is "<<p3<<" p4 is "<<p4<<endl;
        circle(image, squares[i][0], 3, Scalar(0,0,255), 5, 8, 0);
        circle(image, squares[i][1], 3, Scalar(0,255,255), 5, 8, 0);
        circle(image, squares[i][2], 3, Scalar(255,0,255), 5, 8, 0);
        circle(image, squares[i][3], 3, Scalar(255,255,0), 5, 8, 0);

        polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, CV_AA);

这是我检测关键点的代码:

    Mat gray_image;
    vector<KeyPoint> keyPoints; 
    cvtColor(image, gray_image, CV_BGR2GRAY); 
    FastFeatureDetector fast(60); 
    fast.detect(gray_image,keyPoints);  
    drawKeypoints(image, keyPoints,image, Scalar::all(255), DrawMatchesFlags::DRAW_OVER_OUTIMG); 
4

2 回答 2

1

您有两种可能的解决方案:

  1. 检测所有关键点,然后检查它们是否在正方形内。
  2. 从图像中裁剪正方形以生成新图像,然后在那里检测关键点。

干杯,

于 2013-01-17T12:56:58.030 回答
1

您可以使用裁剪图像

矩形 r(left,top,width,height); // 我们感兴趣的部分图片

Mat roi(fullImage, r); // 将创建对原始图像的矩形 r 的引用。请注意,它不是副本。

于 2013-01-17T13:37:18.493 回答