通过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是阈值大小,max_value是可提供的最大值,与后面的threshold_type配合使用,如:
threshold_type:阈值类型 threshold_type=CV_THRESH_BINARY:

这篇博客介绍了如何使用OpenCV的cvFindContours函数在二值图像上找到并绘制轮廓。通过二值化处理,可以有效地分析图像的轮廓。
最低0.47元/天 解锁文章
7358

被折叠的 条评论
为什么被折叠?



