标签:alt rom and i++ block long man article opencv2
要先变为二值图像:cvThreshold
提取轮廓:cvFindContours

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout
<< "\nThis program illustrates the use of findContours and drawContours\n"
<< "The original image is put up along with the image of drawn contours\n"
<< "Usage:\n"
<< "./contours2\n"
<< "\nA trackbar is put up which controls the contour level from -3 to 3\n"
<< endl;
}
const int w = 500;
int levels = 3;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
static void on_trackbar(int, void*)
{
Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
int _levels = levels - 3;
drawContours( cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128,255,255),
3, LINE_AA, hierarchy, std::abs(_levels) );
imshow("contours", cnt_img);
}
int main( int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, "{help h||}");
if (parser.has("help"))
{
help();
return 0;
}
// Mat img = Mat::zeros(w, w, CV_8UC1);
//Jeff --> we don‘t need to draw this by ourselves.
//Draw 6 faces
// for( int i = 0; i < 6; i++ )
// {
// int dx = (i%2)*250 - 30;
// int dy = (i/2)*150;
// const Scalar white = Scalar(255);
// const Scalar black = Scalar(0);
// if( i == 0 )
// {
// for( int j = 0; j <= 10; j++ )
// {
// double angle = (j+5)*CV_PI/21;
// line(img, Point(cvRound(dx+100+j*10-80*cos(angle)),
// cvRound(dy+100-90*sin(angle))),
// Point(cvRound(dx+100+j*10-30*cos(angle)),
// cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
// }
// }
// ellipse( img, Point(dx+150, dy+100), Size(100,70), 0, 0, 360, white, -1, 8, 0 );
// ellipse( img, Point(dx+115, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
// ellipse( img, Point(dx+185, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
// ellipse( img, Point(dx+115, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
// ellipse( img, Point(dx+185, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
// ellipse( img, Point(dx+115, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
// ellipse( img, Point(dx+185, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
// ellipse( img, Point(dx+150, dy+100), Size(10,5), 0, 0, 360, black, -1, 8, 0 );
// ellipse( img, Point(dx+150, dy+150), Size(40,10), 0, 0, 360, black, -1, 8, 0 );
// ellipse( img, Point(dx+27, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
// ellipse( img, Point(dx+273, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
// }
Mat img = imread("/home/unsw/lolo.jpg");
Mat gray;
cvtColor(img, gray, COLOR_RGB2GRAY );
Mat binary;
threshold(gray, binary, 200,255,THRESH_BINARY);
// (1) Pic One Show: show the faces
namedWindow( "image", 1 );
imshow( "image", binary );
// (2) Pic Two Show
//Extract the contours so that
vector<vector<Point> > contours0;
findContours( binary, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
contours.resize(contours0.size());
for( size_t k = 0; k < contours0.size(); k++ )
approxPolyDP(Mat(contours0[k]), contours[k], 3, true);
// Jeff --> the same name to bind window and trackbar together.
// qt qml to draw would be much better.
// callback: on_trackbar()
namedWindow( "contours", 1 );
createTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
on_trackbar(0,0);
waitKey();
return 0;
}
Reference: http://blog.csdn.net/felix86/article/details/38121959
采用cvFindContours提取轮廓,并过滤掉小面积轮廓,最后将轮廓保存。
1 static int getContoursByCplus(char* Imgname, double minarea, double whRatio) 2 { 3 cv::Mat src, dst, canny_output; 4 /// Load source image and convert it to gray 5 src = imread(Imgname, 0); 6 7 if (!src.data) 8 { 9 std::cout << "read data error!" << std::endl; 10 return -1; 11 } 12 blur(src, src, Size(3, 3)); 13 14 15 //the pram. for findContours, 16 vector<vector<Point> > contours; 17 vector<Vec4i> hierarchy; 18 19 /// Detect edges using canny 20 Canny(src, canny_output, 80, 255, 3); 21 /// Find contours 22 findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 23 //CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE 24 25 double maxarea = 0; 26 int maxAreaIdx = 0; 27 28 for (int i = 0; i<contours.size(); i++) 29 { 30 31 double tmparea = fabs(contourArea(contours[i])); 32 if (tmparea>maxarea) 33 { 34 maxarea = tmparea; 35 maxAreaIdx = i; 36 continue; 37 } 38 39 if (tmparea < minarea) 40 { 41 // *** 删除面积小于设定值的轮廓 42 contours.erase(contours.begin() + i); 43 std::wcout << "delete a small area" << std::endl; 44 continue; 45 } 46 //计算轮廓的直径宽高 47 Rect aRect =boundingRect(contours[i]); 48 if ((aRect.width / aRect.height)<whRatio) 49 { 50 // *** 删除宽高比例小于设定值的轮廓 51 contours.erase(contours.begin() + i); 52 std::wcout << "delete a unnomalRatio area" << std::endl; 53 continue; 54 } 55 } 56 /// Draw contours,彩色轮廓 57 dst= Mat::zeros(canny_output.size(), CV_8UC3); 58 for (int i = 0; i< contours.size(); i++) 59 { 60 //随机颜色 61 Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); 62 drawContours(dst, contours, i, color, 2, 8, hierarchy, 0, Point()); 63 } 64 // Create Window 65 char* source_window = "countors"; 66 namedWindow(source_window, CV_WINDOW_NORMAL); 67 imshow(source_window, dst); 68 cv:; waitKey(0); 69 70 return 0; 71 }
[OpenCV] Samples 04: contours2
标签:alt rom and i++ block long man article opencv2
原文地址:http://www.cnblogs.com/jesse123/p/6104980.html