我正试图从图像中检测并提取扑克牌。计划是检测卡片的轮廓,然后使用轮廓区域提取它们。 (有更有效的方法吗?)问题是我在非闭合轮廓上遇到麻烦:
有了这个轮廓,我无法计算矩形的面积。因此,我进行了形态转换以关闭轮廓,产生:
让我留下这些带有扭曲角落边缘的“矩形”。如何将这些伪矩形近似为完美的几何矩形?
有更有效的方法吗?
到目前为止,这是我的代码:
#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;
}
更健壮的方法是在Canny找到线条(Hough线),与它们相交并找到矩形。轮廓对噪音不稳健。