opencv-ios开发笔记5 利用二值图像简单提取轮廓

来源:互联网 发布:札幌住宿 知乎 编辑:程序博客网 时间:2024/06/05 02:31

 从摄像头获取的图像的轮廓检测

-(void)getContours:(cv::Mat &)image{     cvtColor(image,image,CV_BGR2GRAY);////将图像转化为灰度图    threshold(image,image,128,255,cv::THRESH_BINARY);//利用阈值二值化    std::vector<std::vector<cv::Point>> contours;    //// find    findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);    //// draw   // NSLog(@"矩形个数:%lu",contours.size());    drawContours(image,contours,-1,cvScalar(255),2);    // 轮廓表示为一个矩形    // cv::Rect r = boundingRect(cv::Mat(contours[0]));    // rectangle(image, r, cvScalar(128), 2);    self.imageview.image=MatToUIImage(image);}


感觉检测的轮廓很脏,不知道怎么处理,但是速度很快,感觉不掉帧。


参考代码如下,找不到出处了:

#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:      如果 src(x,y)>threshold ,dst(x,y) = max_value; 否则,dst(x,y)=0;threshold_type=CV_THRESH_BINARY_INV:  如果 src(x,y)>threshold,dst(x,y) = 0; 否则,dst(x,y) = max_value.threshold_type=CV_THRESH_TRUNC:  如果 src(x,y)>threshold,dst(x,y) = threshold; 否则dst(x,y) = src(x,y).threshold_type=CV_THRESH_TOZERO:  如果src(x,y)>threshold,dst(x,y) = src(x,y) ; 否则 dst(x,y) = 0。threshold_type=CV_THRESH_TOZERO_INV:  如果 src(x,y)>threshold,dst(x,y) = 0 ; 否则dst(x,y) = src(x,y).*/cvNamedWindow("binaryImage",1);cvShowImage("binaryImage",g_gray);cvFindContours(g_gray,g_storage,&contours);/*cvFindContours从二值图像中检索轮廓,并返回检测到的轮廓的个数        cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq first_contour,                            int header_size CV_DEFAULT(sizeof(CvContour)),                            int mode CV_DEFAULT(CV_RETR_LIST),                            int method CV_DEFAULT(CV_CHAIN_APPROX_SIMPLE),                            CvPoint offset CV_DEFAULT(cvPoint(0,0)));        img是已经二值化的图像,storage返回的边界,CvSeq是返回的边界序列,是链式还是树状等由mode确定。header是返回轮廓的个数,method是提取轮廓的方法,offset是轮廓的偏移量,相当于起始点*/cvZero(g_gray);if(contours){cvDrawContours(g_gray,contours,cvScalarAll(255),cvScalarAll(255),100);/*绘制轮廓的函数void cvDrawContours( CvArr *img, CvSeq* contour,CvScalar external_color, CvScalar hole_color,int max_level, int thickness=1,int line_type=8, CvPoint offset=cvPoint(0,0) );            img是初始图像,contour是轮廓序列,external_color是外轮廓的颜色,hole_color是内轮廓的颜色,max_level是所化轮廓最大层数,适用于完整轮廓里面还有轮廓,如同心圆           thickness是线的粗细,line_type是线的类型,offset是偏移量,也就是起始点*/cvShowImage("Contours",g_gray);}}int main(int argc, char* argv[]){    g_image=cvLoadImage("2.jpg");IplImage *img=cvCreateImage(cvGetSize(g_image),8,3);cvCopyImage(g_image,img);cvNamedWindow("Contours",1);cvNamedWindow("src",1);cvShowImage("src",img);cvCreateTrackbar("Threshold","Contours",&g_thresh,1,on_trackbar);on_trackbar(0);cvWaitKey(0);printf("Hello World!\n");return 0;}



0 0