contour 轮廓

cvFindContours函数说明:

http://blog.sina.com.cn/s/blog_4bc179a80100hs50.html

 

cvDrawContours函数说明:

http://blog.csdn.net/fulva/article/details/7535760

http://blog.sina.com.cn/s/blog_609c4e7201013ftl.html

 

 (一),图片的外边框

 

#include <stdio.h>
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv\cxcore.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

using namespace std;
using namespace cv;

int main()
{
IplImage *pImg = NULL,*pImgc = NULL;
CvMemStorage *storage = cvCreateMemStorage(0); /* 动态内存存储     内存存储器是一个可用来存储诸如序列,轮廓,图形,子划分等动态增长数据结构的底层结构。   */

CvSeq *contour = 0;     可动态增长元素序列
int mode = CV_RETR_EXTERNAL;//CV_RETR_CCOMP内外轮廓都检测
/*CV_RETR_EXTERNAL只检测最外边的轮廓

  CV_RETR_CCOMP内外轮廓都检测
*/

/*创建窗口,载入图像,强制转换成gray*/
cvNamedWindow("src",1);
cvNamedWindow("contour",1);
if ((pImg = cvLoadImage("Lena.jpg",0)) == 0)
{
cvDestroyWindow("src");
cvDestroyWindow("contour");
cvReleaseMemStorage(&storage);
return -1;
}

cvShowImage("src",pImg);


pImgc = cvCreateImage(cvGetSize(pImg),IPL_DEPTH_8U,3); /* 为轮廓显示图像申请空间,3通道图像,以便用彩色显示*/
cvCvtColor(pImg,pImgc,CV_GRAY2BGR);

//查找contour
cvFindContours(pImg,storage,&contour,sizeof(CvContour),mode,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));


 //轮廓
cvDrawContours(pImgc,contour,CV_RGB(0,0,255),CV_RGB(255,43,2),2,2,8,cvPoint(0,0));

cvShowImage("contour",pImgc);

cvWaitKey(0);

cvDestroyWindow("src");
cvDestroyWindow("contour");
cvReleaseImage(&pImg);
cvReleaseImage(&pImgc);
cvReleaseMemStorage(&storage);

return 0;
}

 

 

(二)自绘图形,并描绘出图形的边框

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv\cxcore.h>
#include <stdio.h>

using namespace std;
using namespace cv;

#define w 500

int levels = 3;
CvSeq* contours = 0;

void on_trackbar(int pos)工具选择条
{
IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
CvSeq* _contours = contours;
int _levels = levels - 3;
if( _levels <= 0 ) // get to the nearest face to make it look more funny
_contours = _contours->h_next->h_next->h_next;
cvZero( cnt_img );
cvDrawContours( cnt_img, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels, 3, CV_AA, cvPoint(0,0) );
cvShowImage( "contours", cnt_img );
cvReleaseImage( &cnt_img );
}


int main()
{
int  i,j;
CvMemStorage *storage = cvCreateMemStorage(0);
IplImage *img = cvCreateImage(cvSize(w,w),8,1),*imgc=NULL;

cvZero(img);
for(i=0;i<6;i++)
{
int dx = (i%2)*250-30;
int dy = (i/2)*150;

//http://blog.sina.com.cn/s/blog_4ffc4c3001010hyb.html
这两个颜色是底下绘图使用的颜色

  CvScalar white = cvRealScalar(231),black = cvRealScalar(35);
if( i == 0 )
{
for( j = 0; j <= 10; j++ )
{
double angle = (j+5)*CV_PI/21;
/*!!!
cvLine()的实现:
http://blog.csdn.net/timidsmile/article/details/7804878

*/
cvLine(img, cvPoint(cvRound(dx+100+j*10-80*cos(angle)),
cvRound(dy+100-90*sin(angle))),
cvPoint(cvRound(dx+100+j*10-30*cos(angle)),
cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
}
}
/*!!!
cvEllipse的用法:
http://baike.baidu.com/view/4863180.htm
*/
cvEllipse( img, cvPoint(dx+350, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
}
cvNamedWindow( "image", 1 );
cvShowImage( "image", img );

cvFindContours( img, storage, &contours, sizeof(CvContour),
CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );

// comment this out if you do not want approximation
contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );

cvNamedWindow( "contours", 1 );
cvCreateTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );

on_trackbar(0);
cvWaitKey(0);
cvReleaseMemStorage( &storage );
cvReleaseImage( &img );

return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值