【问题标题】:how to find extreme point of the contour opencv c++如何找到轮廓opencv c ++的极值点
【发布时间】:2017-11-16 10:12:42
【问题描述】:

我有这张图片:

如何在 OpenCv c++ 中实现如图所示的极值点查找器代码?

有人知道吗?

# determine the most extreme points along the contour

leftmost = tuple(cnt [cnt[:,:,0].argmin()][0]);
rightmost = tuple(cnt [cnt[:,:,0].argmax()][0]);
topmost = tuple(cnt [cnt[:,:,1].argmin()][0]);
bottommost = tuple(cnt [cnt[:,:,1].argmax()][0]);

【问题讨论】:

标签: c++ opencv image-processing opencv-contour


【解决方案1】:

假设你已经有了轮廓并且它是一个点向量,你可以使用std::minmax_element函数来达到同样的目的。它支持一种比较方法,如果它遵循签名,则可以是任何你想要的方法。因此,与您所做的类似的简单代码是:

#include <iostream>
#include <vector>
#include <algorithm>

// this is not necessary, just for testing (OpenCV Points should be used instead)
struct Point
{
    int x;
    int y;


    Point():
    x(0), y(0){}
    Point(int xVal, int yVal):
    x(xVal), y(yVal){}
};

int main()
{
    std::vector<Point> cnt = {
        {1,2},{4,5},{1,6}, {7,8}, {9,3}, {2,6}
    };

    // compare x axis
    auto val = std::minmax_element(cnt.begin(), cnt.end(), [](Point const& a, Point const& b){
        return a.x < b.x;
    });

    std::cout << " leftMost [ " << val.first->x << ", " << val.first->y << " ]" << std::endl;
    std::cout << " RightMost [ " << val.second->x << ", " << val.second->y << " ]" << std::endl;

    // compare y axis
    val = std::minmax_element(cnt.begin(), cnt.end(), [](Point const& a, Point const& b){
        return a.y < b.y;
    });

    std::cout << " TopMost [ " << val.first->x << ", " << val.first->y << " ]" << std::endl;
    std::cout << " BottomMost [ " << val.second->x << ", " << val.second->y << " ]" << std::endl;


    return 0;
}

这是一个link 来运行它。

在这个例子中,Point 结构只是为了模拟 OpenCV,但它的工作原理完全相同。我创建了一个测试向量,运行 minmax_element 仅比较 x 轴并打印结果。我对y 轴做同样的事情。

这个函数返回一对迭代器到这样的对象,如果你需要它们被返回,请确保复制它们:)

【讨论】:

  • 谢谢你的回复,但是如果我的向量是:vector > contours;要在机器人操作中使用,我必须需要这个极值点作为像素。
  • 因为我必须使用 :::findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 函数来实时找到相同的轮廓。它显示命运错误
  • 什么错误?编辑您的问题(使用其下方的编辑按钮)并为您的问题添加更新。你应该这样做contours[i]contours是点向量的向量,该函数使用点向量,你需要先检查它是否有点,如果没有就会出错。
  • 非常感谢!!我的问题是:- 当我使用 `vector > contours;` 你的程序不起作用。当我使用vector&lt;Point&gt; &gt; contoursonly 我的findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 不起作用
  • 严重代码描述项目文件行抑制状态错误(活动) E0312 没有合适的用户定义转换来自“std::vector<:point std::allocator>> " 到 "cv::Point" 存在 ql
【解决方案2】:
void thresh_callback(int, void* )
{
    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Canny( src_gray, canny_output, thresh, thresh*2, 3 );
    findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );

    Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );

    for( size_t i = 0; i< contours.size(); i++ )
    {
        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
        drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
    }

    namedWindow( "Contours", WINDOW_AUTOSIZE );
    imshow( "Contours", drawing );
}

要使用这段代码,上面的代码是如何工作的

【讨论】:

    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2017-02-07
    • 2019-10-07
    • 2011-11-27
    • 1970-01-01
    相关资源
    最近更新 更多