亲宝软件园·资讯

展开

Opencv处理图像之轮廓提取

人气:0

#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
int main()
{
 
 IplImage* img = cvLoadImage("E:\\test.bmp",0); 
 IplImage* imgColor = cvCreateImage(cvGetSize(img),8,3);
 IplImage* contoursImage = cvCreateImage(cvSize(img->width,img->height),8,1);
 
 cvNamedWindow("hui");
 cvShowImage("hui",img);
 cvThreshold(img,img,100,255,CV_THRESH_BINARY); //二值化图像以100为阀值
 cvNamedWindow("2");
 cvShowImage("2",img);
 
 CvSeq* contours = 0,* contoursTemp =0; 
 cvZero(contoursImage);
 
 cvCvtColor(img,imgColor,CV_GRAY2BGR); //颜色转化函数
 
 
 //img :需要提前的图片需要二值
 //storage:存放的空间
 //contours:指向轮廓提取的第一个轮廓
 //sizeof(CvContour):采用的是哪一种结构以及大小
 //CV_RETR_LIST:轮廓存储方式
 //CV_CHAIN_APPROX_NONE:采用坐标集表示轮廓
 CvMemStorage* storage = cvCreateMemStorage(0);
 int total = cvFindContours(img,storage,&contours,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_NONE,cvPoint(0,0)); 
 printf("%d",total); //输出总的轮廓数
 contoursTemp = contours;
 
 while(contoursTemp!=0){
 for(int i = 0;i<contoursTemp->total;i++){
 CvPoint *pt = (CvPoint*)cvGetSeqElem(contoursTemp,i);
 cvSetReal2D(contoursImage,pt->y,pt->x,255.0);
 cvSet2D(imgColor,pt->y,pt->x,cvScalar(0,0,255.0));
 }
 contoursTemp = contoursTemp->h_next; 
 }
 cvNamedWindow("img");
 cvShowImage("img",imgColor);
 cvNamedWindow("contoursImage");
 cvShowImage("contoursImage",contoursImage);
 cvWaitKey(0);
 return 0;
}

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论