简体   繁体   中英

How do you get x&y coordinates and size of a cv::keypoint? I keep getting a segfault

I've been over and over various cv::KeyPoint resources and followed many tips to no avail so far. cv::KeyPoint Class Reference

// --- BLOB DETECTION --- //
    // Storage for blobs
    vector<KeyPoint> keypoints;
    // Set up detector with params
    Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);  
    // Detect blobs
    detector->detect( camThresh, keypoints);
    // Draw keypoints
    drawKeypoints( camRaw, keypoints, camBlobs, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

I;m very glad I got this to work up until this point, but I really need to get the XY and size values of those keypoints. The closest suggestion I'd almost gotten to work (see below) throws out a segment fault.

float x = keypoints[i].pt.x;

and

Point2f p = keypoints[i].pt;

Lead me to the same outcome. Someone in the suggestions linked about mentioned the same problem. Any one have any tips? Thanks!

I found out why the program was crashing. I was not checking for whether there even were any active keypoints at all. So,if the program starts without anything in the camera's view it crashes. It's completely valid to use, for example :

        if(keypoints.size()>0) {
        if((char)keyboard == 'c') {
            float x0 = keypoints[0].pt.x;
            float y0 = keypoints[0].pt.y;
            cout << "Point0 Xpos = " << x0 << "\n";
            cout << "Point0 Ypos = " << y0 << "\n";
        }
    }

As long as there are some objects being tracked when the program starts. Once nothing is in the camera's view the application crashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM