【问题标题】:opencv cornerSubPix Exception while converting python code to c++将python代码转换为c ++时出现opencvcornerSubPix异常
【发布时间】:2021-08-01 18:30:29
【问题描述】:

我正在尝试将 this response 移植到 c++,但我无法克服这个神秘的异常(见下图)。不知道限制因素是什么。我想这是图像颜色格式或角参数,但似乎没有任何效果。如果涉及到颜色格式转换请提供小代码sn-p。

Anubhav Singh 提供的 python 代码运行良好,但我想用 c++ 开发。任何帮助将不胜感激。

我正在使用 OpenCV04.2.0

void CornerDetection(){
std::string image_path = samples::findFile("../wing.png");
Mat img = imread(image_path);

Mat greyMat;
Mat dst;

cv::cvtColor(img, greyMat, COLOR_BGR2GRAY);
threshold(greyMat, greyMat, 0, 255, THRESH_BINARY | THRESH_OTSU);

cornerHarris(greyMat, dst, 9, 5, 0.04);
dilate(dst, dst,NULL);

Mat img_thresh;
threshold(dst, img_thresh, 0.32 * 255, 255, 0);
img_thresh.convertTo(img_thresh, CV_8UC1);

Mat labels = Mat();
Mat stats = Mat();
Mat centroids = Mat();
cv::connectedComponentsWithStats(img_thresh, labels, stats, centroids, 8, CV_32S);
TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 30, 0.001);

std::vector<Point2f> corners = std::vector<Point2f>();

Size winSize = Size(5, 5);
Size zeroZone = Size(-1, -1);
cornerSubPix(greyMat, corners, winSize, zeroZone, criteria);

for (int i = 0; i < corners.size(); i++)
{
    circle(img, Point(corners[i].x, corners[i].y), 5, Scalar(0, 255, 0), 2);
}

imshow("img", img);
waitKey();
destroyAllWindows();

}

【问题讨论】:

    标签: c++ opencv connected-components


    【解决方案1】:

    解决方案是在将corners 变量传递给cornerSubPix(...) 函数之前迭代质心以构建corners 向量。

    std::vector<Point2f> corners = std::vector<Point2f>();
    
    for (int i = 0; i < centroids.rows; i++)
    {
        double x = centroids.at<double>(i, 0);
        double y = centroids.at<double>(i, 1);
        corners.push_back(Point2f(x, y));
    }
    

    解决方案的输出仍然不完全是 python 输出,不管它解决了这个问题,以防其他人遇到这个问题。

    【讨论】:

      猜你喜欢
      • 2020-09-14
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      相关资源
      最近更新 更多