OpenCV矩形(長方/正方形) 檢測/尋找/搜尋/標定/標記
OpenCV矩形(長方/正方形) 檢測/尋找/搜尋/標定/標記
資料來源: https://mp.weixin.qq.com/s?subscene=23&__biz=MzIwMTE1NjQxMQ==&mid=2247485054&idx=1&sn=00eeb26a329e4e76a64ebfc4947ea6b0&chksm=96f3742aa184fd3cd6b43a9baca40f7cb3aa9bdf8d8ec73ba7592c1cdd491c6b320f218c565d&scene=7&key=391633c74d74d5c5ed92b8067722b7e9b8d4e714acfab02e293b75646deb5cb257bcc223d04293e52a34ba2d25cadfaeac15bd60a4097c79af272c759b9531a823b14fd912172ee14a9b73d5ba36b1b1&ascene=0&uin=MjIwODk2NDgxNw%3D%3D&devicetype=Windows+10+x64&version=62090529&lang=zh_TW&exportkey=AtFJkF12184D%2BLzw%2B92OGW0%3D&pass_ticket=XpKWTSs5D5AL70GOlf8f9nsq1J8zPUMrL3oMN4foYQdpL15qi6CeXIEotrwM%2FZ4t
https://github.com/alyssaq/opencv
結果圖:
其算法流程:
1.中值濾波去噪;
2.依次提取不同的顏色通道(BGR)檢測矩形;
3.對每一通道使用canny檢測邊緣或者使用多個閾值二值化;
4.使用findContours函數查找輪廓;
5.使用approxPolyDP函數去除多邊形輪廓一些小的波折;
6.找到同時滿足面積較大和形狀為凸的四邊形;
7.判斷輪廓中兩兩鄰接直線夾角餘弦是否小於0.3(意味著角度在90度附近),是則此四邊形為找到的矩形。
code:
// The "Square Detector" program. // It loads several images sequentially and tries to find squares in // each image #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> #include <math.h> #include <string.h> using namespace cv; using namespace std; static void help() { cout << "\nA program using pyramid scaling, Canny, contours, contour simpification and\n" "memory storage to find squares in a list of images\n" "Returns sequence of squares detected on the image.\n" "the sequence is stored in the specified memory storage\n" "Call:\n" "./squares\n" "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl; } int thresh = 50, N = 5; const char* wndname = "Square Detection Demo"; // helper function: // finds a cosine of angle between vectors // from pt0->pt1 and from pt0->pt2 static double angle( Point pt1, Point pt2, 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); } // returns sequence of squares detected on the image. // the sequence is stored in the specified memory storage static void findSquares( const Mat& image, vector<vector<Point> >& squares ) { squares.clear(); //s Mat pyr, timg, gray0(image.size(), CV_8U), gray; // down-scale and upscale the image to filter out the noise //pyrDown(image, pyr, Size(image.cols/2, image.rows/2)); //pyrUp(pyr, timg, image.size()); // blur will enhance edge detection Mat timg(image); medianBlur(image, timg, 9); Mat gray0(timg.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(&timg, 1, &gray0, 1, ch, 1); // try several threshold levels for( int l = 0; l < N; l++ ) { // hack: use Canny instead of zero threshold level. // Canny helps to catch squares with gradient shading if( l == 0 ) { // apply Canny. Take the upper threshold from slider // and set the lower to 0 (which forces edges merging) Canny(gray0, gray, 5, thresh, 5); // dilate canny output to remove potential // holes between edge segments dilate(gray, gray, Mat(), Point(-1,-1)); } else { // apply threshold if l!=0: // tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0 gray = gray0 >= (l+1)*255/N; } // find contours and store them all as a list findContours(gray, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); vector<Point> approx; // test each contour 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); // square contours should have 4 vertices after approximation // relatively large area (to filter out noisy contours) // and be convex. // 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++ ) { // find the maximum cosine of the angle between joint edges double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1])); maxCosine = MAX(maxCosine, cosine); } // if cosines of all angles are small // (all angles are ~90 degree) then write quandrange // vertices to resultant sequence if( maxCosine < 0.3 ) squares.push_back(approx); } } } } } // the function draws all the squares in the image static void drawSquares( Mat& image, const vector<vector<Point> >& squares ) { for( size_t i = 0; i < squares.size(); i++ ) { const Point* p = &squares[i][0]; int n = (int)squares[i].size(); //dont detect the border if (p-> x > 3 && p->y > 3) polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, LINE_AA); } imshow(wndname, image); } int main(int /*argc*/, char** /*argv*/) { static const char* names[] = { "imgs/2Stickies.jpg", "imgs/manyStickies.jpg",0 }; help(); namedWindow( wndname, 1 ); vector<vector<Point> > squares; for( int i = 0; names[i] != 0; i++ ) { Mat image = imread(names[i], 1); if( image.empty() ) { cout << "Couldn't load " << names[i] << endl; continue; } findSquares(image, squares); drawSquares(image, squares); //imwrite( "out", image ); int c = waitKey(); if( (char)c == 27 ) break; } return 0; }