在OpenCV 3.1.0中显示关键点坐标

时间:2016-12-06 16:01:35

标签: c++ opencv

我在VS2013中使用OpenCV 3.1.0,我想要做的是使用SimpleBlobDetector来检测我的网络摄像头捕获的视频中的光点。

它有效,然后我计划显示标记关键点(光点)的坐标。我使用的代码是

vector<KeyPoint> detectKeyPoint;
Ptr<SimpleBlobDetector> sbd = SimpleBlobDetector::create(params);
sbd->detect(gray, detectKeyPoint);
cout << detectKeyPoint[0].pt.x << ", " << detectKeyPoint[0].pt.y << endl;

然而,当我尝试更改手电筒的位置时,控制台中显示的坐标不会刷新。它只显示了光点出现和消失的坐标。我使用错误的命令还是什么?

感谢任何帮助!

我的程序的完整代码在这里:

#include <opencv2\opencv.hpp>  

using namespace cv;
using namespace std;

int main()
{
//open the default camera  
VideoCapture cap(0);

//check if the camera is opened successfully  
if (!cap.isOpened())
{
    return -1;
}

vector<KeyPoint> detectKeyPoint;
KeyPoint* ppt;
Mat keyPointImage;

//![SBD] 
//set detector parameters 
SimpleBlobDetector::Params params;

//filter by area 
params.filterByArea = true;
params.minArea = 20;
params.maxArea = 2000;

//filter by circularity 
params.filterByCircularity = true;
params.minCircularity = 0.1;

//filter by convexity 
params.filterByConvexity = true;
params.minConvexity = 0.87;

//filter by inertia
params.filterByInertia = true;
params.minInertiaRatio = 0.01;

//instantiate a SBD pointer 
Ptr<SimpleBlobDetector> sbd = SimpleBlobDetector::create(params);
//create two windows showing..  
namedWindow("Original"); //the original video captured  
moveWindow("Original", 150, 300); 

namedWindow("VideoCapture"); //the video after setting threshold  
moveWindow("VideoCapture", 830, 300); 

for (;;)
{
    Mat frame, gray;
    //read every frame of the camera input  
    cap >> frame;
    if (frame.channels() == 3)
    {
        cvtColor(frame, gray, CV_BGR2GRAY);
    }
    else
    {
        frame.copyTo(gray);
        //frame >> gray; 
    }
    //setting threshold to create binary image, using THRESH_BINARY_INVERTED  
    threshold(gray, gray, 252, 255, 1);

    //detect lightspots in the frame and store them in detectKeyPoint 
    sbd->detect(gray, detectKeyPoint);

    cout << detectKeyPoint[0].pt.x << ", " << detectKeyPoint[0].pt.y << endl;

    //mark the key points with red circles 
    drawKeypoints(frame, detectKeyPoint, keyPointImage, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

    //display videos  
    imshow("Original", gray);
    imshow("VideoCapture", keyPointImage);

    waitKey(30);
}
return 0;
}

0 个答案:

没有答案