通过opencv自带的cvFindContours函数可以对简单地图像进行轮廓分析,但要求利用图像二值化能找到图像的轮廓,具体实现代码如下:
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
IplImage *g_image=NULL;
IplImage *g_gray=NULL;
int g_thresh=100;
//设定阈值
CvMemStorage *g_storage=NULL;
//滑块响应函数
void on_trackbar(int frame)
{
if(g_storage==NULL)
{
g_gray=cvCreateImage(cvGetSize(g_image),8,1);
g_storage=cvCreateMemStorage(0);//创造内存空间,存储边界
}
else
{
cvClearMemStorage(g_storage);//清空已有的数据
}
CvSeq *contours=0;//边界序列
cvCvtColor(g_image,g_gray,CV_BGR2GRAY);//将图像转化为灰度图
cvThreshold(g_gray,g_gray,g_thresh,255,CV_THRESH_BINARY);//利用阈值二值化
/*
void cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type )
src是初始矩阵,dst是处理后的矩阵,threshold是阈值大小&#x