Goal
In this tutorial you will learn how to:
- Use the OpenCV function cv::findContours
- 使用OpenCV函数cv :: findContours
- Use the OpenCV function cv::drawContours
- 使用OpenCV函数cv :: drawContours
Theory
虽然Canny之类的边缘检测算法可以根据像素之间的差异,检测出轮廓边界的像素,但是它并没有将轮廓作为一个整体。所以,需要把这些边缘像素组装成轮廓。Code
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
void thresh_callback(int, void* );
int main( int argc, char** argv )
{
String imageName("lena.jpg"); // by default
if (argc > 1)
{
imageName = argv[1];
}
src = imread(imageName, IMREAD_COLOR);
if (src.empty())
{
cerr << "No image supplied ..." << endl;
return -1;
}
cvtColor( src, src_gray, COLOR_BGR2GRAY ); //灰度图
blur( src_gray, src_gray, Size(3,3) ); //均值滤波
const char* source_window = "Source";
namedWindow( source_window, WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback ); //滑动条
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
void thresh_callback(int, void* )
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Canny( src_gray, canny_output, thresh, thresh*2, 3 ); //输入,输出,阀值,阀值,内核大小。
findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
//输入,输出(存储为点的向量),轮廓的检索模式,轮廓的逼近方法,每一个轮廓点可以选择的偏移量
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( size_t i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
//输入,轮廓,轮廓index,颜色,厚度,线型,层次结构,如果它是0,只绘制指定的轮廓。如果它是1,该函数绘制轮廓和所有嵌套的轮廓。
//如果它是2,则该函数绘制轮廓,所有嵌套轮廓,所有嵌套到嵌套轮廓,等等。轮廓偏移参数
}
namedWindow( "Contours", WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
}
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
void thresh_callback(int, void* );
int main( int argc, char** argv )
{
String imageName("lena.jpg"); // by default
if (argc > 1)
{
imageName = argv[1];
}
src = imread(imageName, IMREAD_COLOR);
if (src.empty())
{
cerr << "No image supplied ..." << endl;
return -1;
}
cvtColor( src, src_gray, COLOR_BGR2GRAY ); //灰度图
blur( src_gray, src_gray, Size(3,3) ); //均值滤波
const char* source_window = "Source";
namedWindow( source_window, WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback ); //滑动条
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
void thresh_callback(int, void* )
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Canny( src_gray, canny_output, thresh, thresh*2, 3 ); //输入,输出,阀值,阀值,内核大小。
findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
//输入,输出(存储为点的向量),轮廓的检索模式,轮廓的逼近方法,每一个轮廓点可以选择的偏移量
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( size_t i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
//输入,轮廓,轮廓index,颜色,厚度,线型,层次结构,如果它是0,只绘制指定的轮廓。如果它是1,该函数绘制轮廓和所有嵌套的轮廓。
//如果它是2,则该函数绘制轮廓,所有嵌套轮廓,所有嵌套到嵌套轮廓,等等。轮廓偏移参数
}
namedWindow( "Contours", WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
install(TARGETS DisplayImage RUNTIME DESTINATION bin