1 寻找轮廓:findContours()函数
void findContours(InputOutputArray image,OutputArrayOfArrays contours,OutputArray hierarchy,int mode,int method,Point offset=Point())
2 绘制轮廓:drawContours()函数
void drawContours(InputOutputArray image,InoutArrayOfArrays contours,int contourIdx,const Scalar&color,int thickness=1,int lineType=8,InputArray hierarchy=noArray(),int maxLevel=INT_MAX,Point offset=Point())
//例子
//轮廓查找
include <opencv2/opencv.hpp>
include "opencv2/highgui/highgui.hpp"
include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
// 【1】载入原始图,且必须以二值图模式载入
Mat srcImage=imread("1.jpg", 0);
imshow("原始图",srcImage);
//【2】初始化结果图
Mat dstImage = Mat::zeros(srcImage.rows, srcImage.cols, CV_8UC3);
//【3】srcImage取大于阈值119的那部分
srcImage = srcImage > 119;
imshow( "取阈值后的原始图", srcImage );
//【4】定义轮廓和层次结构
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
//【5】查找轮廓
//此句代码的OpenCV2版为:
//findContours( srcImage, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
//此句代码的OpenCV3版为:
findContours( srcImage, contours, hierarchy,RETR_CCOMP, CHAIN_APPROX_SIMPLE );
// 【6】遍历所有顶层的轮廓, 以随机颜色绘制出每个连接组件颜色
int index = 0;
for( ; index >= 0; index = hierarchy[index][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
//此句代码的OpenCV2版为:
//drawContours( dstImage, contours, index, color, CV_FILLED, 8, hierarchy );
//此句代码的OpenCV3版为:
drawContours( dstImage, contours, index, color, FILLED, 8, hierarchy );
}
//【7】显示最后的轮廓图
imshow( "轮廓图", dstImage );
waitKey(0);
}
//查找并绘制轮廓
include "opencv2/highgui/highgui.hpp"
include "opencv2/imgproc/imgproc.hpp"
include
using namespace cv;
using namespace std;
//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------
define WINDOW_NAME1 "【原始图窗口】" //为窗口标题定义的宏
define WINDOW_NAME2 "【轮廓图】" //为窗口标题定义的宏
//-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量的声明
//-----------------------------------------------------------------------------------------------
Mat g_srcImage;
Mat g_grayImage;
int g_nThresh = 80;
int g_nThresh_max = 255;
RNG g_rng(12345);
Mat g_cannyMat_output;
vector<vector> g_vContours;
vector g_vHierarchy;
//-----------------------------------【全局函数声明部分】--------------------------------------
// 描述:全局函数的声明
//-----------------------------------------------------------------------------------------------
static void ShowHelpText( );
void on_ThreshChange(int, void* );
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
//【0】改变console字体颜色
system("color 1F");
//【0】显示欢迎和帮助文字
ShowHelpText( );
// 加载源图像
g_srcImage = imread( "1.jpg", 1 );
if(!g_srcImage.data ) { printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! \n"); return false; }
// 转成灰度并模糊化降噪
cvtColor( g_srcImage, g_grayImage, COLOR_BGR2GRAY );
blur( g_grayImage, g_grayImage, Size(3,3) );
// 创建窗口
namedWindow( WINDOW_NAME1, WINDOW_AUTOSIZE );
imshow( WINDOW_NAME1, g_srcImage );
//创建滚动条并初始化
createTrackbar( "canny阈值", WINDOW_NAME1, &g_nThresh, g_nThresh_max, on_ThreshChange );
on_ThreshChange( 0, 0 );
waitKey(0);
return(0);
}
//-----------------------------------【on_ThreshChange( )函数】------------------------------
// 描述:回调函数
//----------------------------------------------------------------------------------------------
void on_ThreshChange(int, void* )
{
// 用Canny算子检测边缘
Canny( g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh*2, 3 );
// 寻找轮廓
findContours( g_cannyMat_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
// 绘出轮廓
Mat drawing = Mat::zeros( g_cannyMat_output.size(), CV_8UC3 );
for( int i = 0; i< g_vContours.size(); i++ )
{
Scalar color = Scalar( g_rng.uniform(0, 255), g_rng.uniform(0,255), g_rng.uniform(0,255) );//任意值
drawContours( drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point() );
}
// 显示效果图
imshow( WINDOW_NAME2, drawing );
}
//-----------------------------------【ShowHelpText( )函数】----------------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
static void ShowHelpText()
{
//输出欢迎信息和OpenCV版本
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV3版的第70个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION );
printf("\n\n ----------------------------------------------------------------------------\n");
//输出一些帮助信息
printf( "\n\n\t欢迎来到【在图形中寻找轮廓】示例程序~\n\n");
printf( "\n\n\t按键操作说明: \n\n"
"\t\t键盘按键任意键- 退出程序\n\n"
"\t\t滑动滚动条-改变阈值\n" );
}
原文地址:https://www.cnblogs.com/shuguomeifuguo/p/12003068.html