什麼是輪廓?ios
輪廓是一系列相連的點組成的曲線,表明了物體的基本外形。c++
輪廓與邊緣好像挺像的?app
是的,確實挺像,那麼區別是什麼呢?簡而言之,輪廓是連續的,而邊緣並不全都連續(見下圖示例)。其實邊緣主要是做爲圖像的特徵使用,好比能夠用邊緣特徵能夠區分臉和手,而輪廓主要用來分析物體的形態,好比物體的周長和麪積等,能夠說邊緣包括輪廓。ide
邊緣和輪廓的區別(圖片來源:http://pic.ex2tron.top/cv2_understand_contours.jpg)ui
尋找輪廓的操做通常用於二值化圖,因此一般會使用閾值分割或Canny邊緣檢測先獲得二值圖。spa
【注:尋找輪廓是針對白色物體的,必定要保證物體是白色,而背景是黑色,否則不少人在尋找輪廓時會找到圖片最外面的一個框】.net
OpenCV4.1.0 C++ Sample Code:code
/** * @function findContours_Demo.cpp * @brief Demo code to find contours in an image * @author OpenCV team */ #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; Mat src_gray; int thresh = 100; RNG rng(12345); /// Function header void thresh_callback(int, void* ); /** * @function main */ int main( int argc, char** argv ) { /// Load source image CommandLineParser parser( argc, argv, "{@input | ../data/HappyFish.jpg | input image}" ); Mat src = imread( parser.get<String>( "@input" ) ); if( src.empty() ) { cout << "Could not open or find the image!\n" << endl; cout << "Usage: " << argv[0] << " <Input image>" << endl; return -1; } /// Convert image to gray and blur it cvtColor( src, src_gray, COLOR_BGR2GRAY ); blur( src_gray, src_gray, Size(3,3) ); /// Create Window const char* source_window = "Source"; namedWindow( source_window ); imshow( source_window, src ); const int max_thresh = 255; createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback ); thresh_callback( 0, 0 ); waitKey(); return 0; } /** * @function thresh_callback */ void thresh_callback(int, void* ) { /// Detect edges using Canny Mat canny_output; Canny( src_gray, canny_output, thresh, thresh*2 ); /// Find contours vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE ); /// Draw contours Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 ); for( size_t i = 0; i< contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) ); drawContours( drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0 ); } /// Show in a window imshow( "Contours", drawing ); }
Result:orm
應用1:尋找正方形(squares.cpp)blog
// The "Square Detector" program. // It loads several images sequentially and tries to find squares in // each image #include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/core/utils/filesystem.hpp" #include <iostream> using namespace cv; using namespace std; static void help(const char* programName) { cout << "\nA program using pyramid scaling, Canny, contours and contour simplification\n" "to find squares in a list of images (pic1-6.png)\n" "Returns sequence of squares detected on the image.\n" "Call:\n" "./" << programName << " [file_name (optional)]\n" "Using OpenCV version " << CV_VERSION << "\n" << endl; } int thresh = 50, N = 11; 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. static void findSquares( const Mat& image, vector<vector<Point> >& squares ) { squares.clear(); 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()); 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, 0, 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(contours[i], approx, arcLength(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(approx)) > 1000 && isContourConvex(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(); polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, LINE_AA); } imshow(wndname, image); } String absoluteFilePath(const String& relative_path) { String root_path = "F:/opencv/build/bin/sample-data/"; String path = utils::fs::join(root_path, relative_path); return path; } int main(int argc, char** argv) { static const char* names[] = { "pic1.png", "pic2.png", "pic3.png", "pic4.png", "pic5.png", "pic6.png", 0 }; help(names[0]); vector<vector<Point> > squares; for( int i = 0; names[i] != 0; i++ ) { string filename = absoluteFilePath(names[i]); Mat image = imread(filename, IMREAD_COLOR); if( image.empty() ) { cout << "Couldn't load " << filename << endl; continue; } findSquares(image, squares); drawSquares(image, squares); int c = waitKey(); if( c == 27 ) break; } return 0; }
結果:
推薦: