Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 利用质心提取物体_C++_Opencv - Fatal编程技术网

C++ 利用质心提取物体

C++ 利用质心提取物体,c++,opencv,C++,Opencv,我已经使用OpenCV开发了一个应用程序,它可以检测圆形形状并从二值图像中获取质心。现在我想删除从原始图像中未检测到的对象。所以我想知道是否有一个解决方案,通过图像的质心来提取物体 以下是我到目前为止得到的信息: 分割的结果如下: //读取图像 cv::Mat im=cv::imread(“11002847.bmp”,cv::imread\u灰度); 按位_not(im,im); cv::Mat im2; im.copyTo(im2); //检测轮廓以获得所有对象的质心 矢量轮廓; 向量层次

我已经使用
OpenCV
开发了一个应用程序,它可以检测圆形形状并从二值图像中获取质心。现在我想删除从原始图像中未检测到的对象。所以我想知道是否有一个解决方案,通过图像的质心来提取物体

以下是我到目前为止得到的信息:

分割的结果如下:

//读取图像
cv::Mat im=cv::imread(“11002847.bmp”,cv::imread\u灰度);
按位_not(im,im);
cv::Mat im2;
im.copyTo(im2);
//检测轮廓以获得所有对象的质心
矢量轮廓;
向量层次;
findContours(im2、等高线、层次、CV_RETR_树、CV_链_近似_简单、CV::Point(0,0));
向量μ(等高线.size());
对于(int i=0;ifile我希望我理解了您的问题,但为了以防万一,我将重新表述。您希望从顶部图像中删除轮廓,这些轮廓未被检测为底部图像中的关键点(即,它们中/周围没有红色圆圈)

为此,迭代所有的
轮廓
,并使用

希望有帮助

// Read image
    cv::Mat im = cv::imread( "11002847.bmp", cv::IMREAD_GRAYSCALE );
    bitwise_not(im, im);
    cv::Mat im2;
    im.copyTo(im2);


    //Detec the contour to get all the obeject centroid
    std::vector<std::vector<cv::Point> > contours;
    vector<cv::Vec4i> hierarchy;
    findContours( im2, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
    vector<cv::Moments> mu(contours.size() );
    for( int i = 0; i < contours.size(); i++ )
    { 
        mu[i] = moments( contours[i], false ); 
    }
    ofstream file;
    file.open("log.txt");
    ///  Get the mass centers:
    vector<cv::Point2f> mc( contours.size() );
    for( int i = 0; i < contours.size(); i++ )
    { 
        mc[i] = cv::Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); 
        file << mc[i].x << "   "<< mc[i].y << endl;
    } 
    file.close();


    //Dtect the circular objects and get there centroid
    // Setup SimpleBlobDetector parameters.
    cv::SimpleBlobDetector::Params params;
    // Change thresholds
    //params.minThreshold = 10;
    //params.maxThreshold = 200;
    // Filter by Area.
    params.filterByArea = true;
    params.minArea = 1;
    // Filter by Circularity
    params.filterByCircularity = true;
    params.minCircularity = 0.5;
    // Filter by Convexity
    params.filterByConvexity = true;
    params.minConvexity = 0.1;
    // Filter by Inertia
    params.filterByInertia = true;
    params.minInertiaRatio = 0.0;
    // Storage for blobs
    vector<cv::KeyPoint> keypoints;
    // Set up detector with params
    cv::SimpleBlobDetector detector(params);
    // Detect blobs
    detector.detect( im, keypoints);


    ofstream file2;
    file2.open("log2.txt");

    for(vector<cv::KeyPoint>::iterator it = keypoints.begin(); it != keypoints.end(); ++it) 
    {
        cv::KeyPoint k =  *it;
        file2 << k.pt.x << "      "<<  k.pt.y << endl;
    }

    file2.close();
    // Draw detected blobs as red circles.
    // DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures
    // the size of the circle corresponds to the size of blob
    cv::Mat im_with_keypoints;
    drawKeypoints( im, keypoints, im_with_keypoints, cv::Scalar(0,0,255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
    // Show blobs
    cv::imwrite("keypoints.bmp", im_with_keypoints );
auto end = contours.end();
for (auto contour_itr = contours.begin(); contour_itr != end; ++contour_itr) {
    auto keypoint_end = keypoints.end();
    for (auto keypoint_itr = keypoints.begin(); keypoint_itr != keypoint_end; ++ keypoint_itr) {
        auto contour = *contour_itr;
        auto keypoint = *keypoint_itr;

        if (cv::pointPolygonTest(contour, keypoint.pt, false) > 0) {
            // do your thing here (e.g. store contour into an array or paint it into a new image
        }
    }
}