【问题标题】:proximate contour to rectagle接近矩形的轮廓
【发布时间】:2019-02-15 21:20:30
【问题描述】:

我正在尝试从图像中检测和提取扑克牌。计划是检测卡片的轮廓,然后使用轮廓区域提取它们。 (有没有更有效的方法?) 问题是我在使用非闭合轮廓时遇到了问题:

有了这个轮廓,我无法计算矩形的面积。因此,我执行了形态变换来闭合轮廓,产生了这个:

在边缘提取之后:

给我留下了这些边缘有扭曲角的“矩形”。如何将这些伪矩形近似为完美的几何矩形?

有没有更有效的方法?

到目前为止,这是我的代码:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;
using namespace std;

#define BKG_THRESH 60 // preProcessImg

Mat src;


void preProcessImg(Mat* _img){
    Mat aux_gray;

    cvtColor(*_img, aux_gray, CV_BGR2GRAY );

    GaussianBlur(aux_gray, *_img, Size(5,5), 0);
}



int main( int argc, char** argv ){
    vector<vector<Point>> contours;


    /// Load an image
    src = imread("img.jpg");

    preProcessImg(&src);

    Canny(src, src, 30, 200);

    //Mostrar imagem
    namedWindow( "canny_output", CV_WINDOW_NORMAL); // Create a window
    imshow( "canny_output", src);
    waitKey(0);

    Mat structuringElement = getStructuringElement(MORPH_ELLIPSE, Size(7, 7));
    morphologyEx(src, src, MORPH_CLOSE, structuringElement);

    //Mostrar imagem
    namedWindow( "morph_transf", CV_WINDOW_NORMAL); // Create a window
    imshow( "morph_transf", src);
    waitKey(0);

    findContours(src, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
    for( int i = 0; i< contours.size(); i++){
        Scalar color( rand()&255, rand()&255, rand()&255 );
        drawContours( drawing, contours, i, color );
    }

    //Mostrar imagem
    namedWindow( "contours", CV_WINDOW_NORMAL); // Create a window
    imshow( "contours", drawing);
    waitKey(0);

    return 0;
  }    

【问题讨论】:

    标签: c++ opencv opencv-contour


    【解决方案1】:

    更稳健的方法是在 Canny 之后找到线(霍夫线),将它们相交并找到矩形。轮廓对噪声并不鲁棒。

    【讨论】:

    • 谢谢!我试试看
    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2021-11-19
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多