java opencv 轮廓检测_OpenCV 轮廓检测心得笔记

程序采用OpenCV中国的例程,下面列举了各个详细函数的功能及简单说明。

/**************************************************

* 轮廓检测

* 主要函数:

*      cvFindContours

*      cvDrawContours

**************************************************/

/***********************************************************************

* OpenCV example

* By Shiqi Yu 2006

***********************************************************************/

#include "cv.h"

#include "cxcore.h"

#include "highgui.h"

int main( int argc, char** argv )

{

//声明IplImage指针

IplImage* pImg = NULL;

IplImage* pContourImg = NULL;

CvMemStorage * storage = cvCreateMemStorage(0);   //创建一个堆栈,存储轮廓用

CvSeq * contour = 0;    //设置存取提取的指针

int mode = CV_RETR_EXTERNAL;  //提取物体最外层轮廓

if( argc == 3)

if(strcmp(argv[2], "all") == 0)

mode = CV_RETR_CCOMP; //内外轮廓都检测

//创建窗口

cvNamedWindow("src", 1);

cvNamedWindow("contour",1);

//载入图像,强制转化为Gray

if( argc >= 2 &&

(pImg = cvLoadImage( argv[1], 0)) != 0 )

{

cvShowImage( "src", pImg );

//为轮廓显示图像申请空间

//3通道图像,以便用彩色显示

pContourImg = cvCreateImage(cvGetSize(pImg),

IPL_DEPTH_8U,

3);

//copy source image and convert it to BGR image

cvCvtColor(pImg, pContourImg, CV_GRAY2BGR);

//查找contour

cvFindContours( pImg, storage, &contour, sizeof(CvContour),

mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

}

else

{

//销毁窗口

cvDestroyWindow( "src" );

cvDestroyWindow( "contour" );

cvReleaseMemStorage(&storage);

return -1;

}

//将轮廓画出

cvDrawContours(pContourImg, contour,

CV_RGB(0,0,255), CV_RGB(255, 0, 0),

2, 2, 8, cvPoint(0,0));

//显示图像

cvShowImage( "contour", pContourImg );

cvWaitKey(0);

//销毁窗口

cvDestroyWindow( "src" );

c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值