OpenCV C++/Obj-C:检测一张纸/正方形检测 [英] OpenCV C++/Obj-C: Detecting a sheet of paper / Square Detection
问题描述
我在我的测试应用程序中成功实现了 OpenCV 方块检测示例,但现在需要过滤输出,因为它非常混乱 - 还是我的代码有问题?
我对减少歪斜的论文的四个角点感兴趣(例如
原始图片:
代码:
双角( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {双 dx1 = pt1.x - pt0.x;双 dy1 = pt1.y - pt0.y;双 dx2 = pt2.x - pt0.x;双 dy2 = pt2.y - pt0.y;返回 (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);}- (std::vector)findSquaresInImage:(cv::Mat)_image{std::vector>正方形;cv::Mat pyr, timg, gray0(_image.size(), CV_8U), 灰色;整数阈值 = 50,N = 11;cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));cv::pyrUp(pyr, timg, _image.size());std::vector>轮廓;for( int c = 0; c <3; c++ ) {int ch[] = {c, 0};mixChannels(&timg, 1, &gray0, 1, ch, 1);for( int l = 0; l = (l+1)*255/N;}cv::findContours(灰色,轮廓,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);std::vector约;for( size_t i = 0; i 1000 && cv::isContourConvex(cv::Mat(approx))) {双最大余弦 = 0;for( int j = 2; j <5; j++ ){双余弦 = fabs(angle(approx[j%4],approx[j-2],approx[j-1]));maxCosine = MAX(maxCosine, cosine);}如果(最大余弦<0.3){squares.push_back(大约);}}}}}返回方块;}
编辑 17/08/2012:
要在图像上绘制检测到的方块,请使用以下代码:
cv::Mat debugSquares( std::vector > squares, cv::Mat image ){for ( int i = 0; i< squares.size(); i++ ) {//绘制轮廓cv::drawContours(image, squares, i, cv::Scalar(255,0,0), 1, 8, std::vector(), 0, cv::Point());//绘制边界矩形cv::Rect rect = boundingRect(cv::Mat(squares[i]));cv::rectangle(image, rect.tl(), rect.br(), cv::Scalar(0,255,0), 2, 8, 0);//绘制旋转矩形cv::RotatedRect minRect = minAreaRect(cv::Mat(squares[i]));cv::Point2f rect_points[4];minRect.points( rect_points );for ( int j = 0; j <4; j++ ) {cv::line( image, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0,0,255), 1, 8);//蓝色}}返回图像;}
这是 Stackoverflow 中反复出现的主题,由于我找不到相关的实现,我决定接受挑战.
我对 OpenCV 中的方块演示做了一些修改,下面生成的 C++ 代码能够检测到图像中的一张纸:
void find_squares(Mat& image, vector >& squares){//模糊将增强边缘检测垫子模糊(图像);中值模糊(图像,模糊,9);Mat gray0(blurred.size(), CV_8U),灰色;矢量<矢量<点>>轮廓;//在图像的每个颜色平面中找到正方形for (int c = 0; c <3; c++){int ch[] = {c, 0};mixChannels(&blurred, 1, &gray0, 1, ch, 1);//尝试几个阈值级别const int threshold_level = 2;for (int l = 0; l = (l+1) * 255/threshold_level;}//查找轮廓并将它们存储在列表中findContours(灰色,轮廓,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);//测试轮廓矢量<点>约;for (size_t i = 0; i 1000 & &isContourConvex(Mat(approx))){双最大余弦 = 0;for (int j = 2; j <5; j++){双余弦 = fabs(angle(approx[j%4],approx[j-2],approx[j-1]));maxCosine = MAX(maxCosine, cosine);}如果 (maxCosine <0.3)squares.push_back(大约);}}}}}
执行此程序后,这张纸将是vector
:
我让你编写函数来找到最大的正方形.;)
I successfully implemented the OpenCV square-detection example in my test application, but now need to filter the output, because it's quite messy - or is my code wrong?
I'm interested in the four corner points of the paper for skew reduction (like that) and further processing …
Input & Output:
Original image:
Code:
double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
- (std::vector<std::vector<cv::Point> >)findSquaresInImage:(cv::Mat)_image
{
std::vector<std::vector<cv::Point> > squares;
cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
int thresh = 50, N = 11;
cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
cv::pyrUp(pyr, timg, _image.size());
std::vector<std::vector<cv::Point> > contours;
for( int c = 0; c < 3; c++ ) {
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
for( int l = 0; l < N; l++ ) {
if( l == 0 ) {
cv::Canny(gray0, gray, 0, thresh, 5);
cv::dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
}
else {
gray = gray0 >= (l+1)*255/N;
}
cv::findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
std::vector<cv::Point> approx;
for( size_t i = 0; i < contours.size(); i++ )
{
cv::approxPolyDP(cv::Mat(contours[i]), approx, arcLength(cv::Mat(contours[i]), true)*0.02, true);
if( approx.size() == 4 && fabs(contourArea(cv::Mat(approx))) > 1000 && cv::isContourConvex(cv::Mat(approx))) {
double maxCosine = 0;
for( int j = 2; j < 5; j++ )
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}
if( maxCosine < 0.3 ) {
squares.push_back(approx);
}
}
}
}
}
return squares;
}
EDIT 17/08/2012:
To draw the detected squares on the image use this code:
cv::Mat debugSquares( std::vector<std::vector<cv::Point> > squares, cv::Mat image )
{
for ( int i = 0; i< squares.size(); i++ ) {
// draw contour
cv::drawContours(image, squares, i, cv::Scalar(255,0,0), 1, 8, std::vector<cv::Vec4i>(), 0, cv::Point());
// draw bounding rect
cv::Rect rect = boundingRect(cv::Mat(squares[i]));
cv::rectangle(image, rect.tl(), rect.br(), cv::Scalar(0,255,0), 2, 8, 0);
// draw rotated rect
cv::RotatedRect minRect = minAreaRect(cv::Mat(squares[i]));
cv::Point2f rect_points[4];
minRect.points( rect_points );
for ( int j = 0; j < 4; j++ ) {
cv::line( image, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0,0,255), 1, 8 ); // blue
}
}
return image;
}
This is a recurring subject in Stackoverflow and since I was unable to find a relevant implementation I decided to accept the challenge.
I made some modifications to the squares demo present in OpenCV and the resulting C++ code below is able to detect a sheet of paper in the image:
void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 9);
Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;
// find squares in every color plane of the image
for (int c = 0; c < 3; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);
// try several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //
// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}
// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;
for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}
if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}
After this procedure is executed, the sheet of paper will be the largest square in vector<vector<Point> >
:
I'm letting you write the function to find the largest square. ;)
这篇关于OpenCV C++/Obj-C:检测一张纸/正方形检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!