zoukankan      html  css  js  c++  java
  • Opencv step by step


    这里举出三个案例:


    #include <cv.h>
    #include <highgui.h>
    
    
    void image_smooth(IplImage * image)
    {
    	cvNamedWindow("#1image input");
    	cvShowImage("#1image input", image);	
    	
    	/* create 8 bit 3 channel image*/
    	IplImage *out_image = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);
    	/* smooth 3*3 around every point */
    	cvSmooth(image, out_image, CV_GAUSSIAN, 3, 3);
    	cvNamedWindow("#1image output");
    	cvShowImage("#1image output", out_image);
    
    	cvReleaseImage(&out_image);
    	cvWaitKey(0);
    	
    	cvDestroyWindow("#1image input");
    	cvDestroyWindow("#1image output");
    }
    
    IplImage *doPyrDown(IplImage *in)
    {
    	IplImage *out = cvCreateImage(cvSize(in->width/2, in->height/2) ,in->depth, in->nChannels);
    	cvPyrDown(in, out);
    	return out;
    }
    
    IplImage *doCanny(IplImage *in, double lowTh, double highTh, double aperture)
    {
    	//3 channel is ok
    	printf("channels = %d
    ", in->nChannels);
    	//if(1 != in->nChannels)
    	//	return NULL;
    	printf("start processing...
    ");
    	IplImage *out = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);
    	cvCanny(in, out, lowTh, highTh, aperture);
    	return out;
    }
    
    
    int main(int argc,char **argv)
    {
    
    
    	printf("this is image transforming
    ");
            IplImage *image;  
    	/* todo:how to judge load fail?*/
            image = cvLoadImage(argv[1]);  
          
            if(argc != 2)  
            {  
                std::cout << "No image data
    ";  
                return -1;  
            }  
    
    /*
    	image_smooth(image);
    */
    
    	cvNamedWindow("image input");
    	cvNamedWindow("image output");
    	//IplImage *img1 = doPyrDown(image);//#1
    	IplImage *img1 = doCanny(image, 10, 100, 3);//#2
    
    	cvShowImage("image input", image);	
    	cvShowImage("image output", img1);	
    	cvReleaseImage(&image);
    	cvReleaseImage(&img1);
    	cvWaitKey(0);
    	cvDestroyWindow("image input");
    	cvDestroyWindow("image output");
    
    
    
    
    
    	return 0;
    }
    

    其中,

    image_smooth
    是将图像平滑处理。

    其中关键的一行是

    	cvSmooth(image, out_image, CV_GAUSSIAN, 3, 3);


    效果如下:

    这样处理后,图片变模糊了。



    doPyrDown
    是图像变成原来的0.25倍,它调用的
    cvPyrDown
    函数对于传入参数有较严格的要求,传入的两个图像要接近0.25倍,详情看官方文档.






    doCanny
    是进行边缘检测,虽然书中不支持三通道的,但是代码里面是支持的,所以我直接传了张一般的图片进去。。

    效果如下(女神 奥黛丽·赫本):





  • 相关阅读:
    用简单C程序分析DOS下的EXE文件【转】
    汇编函数的调用[转自KingofCoders]
    windows下的shellcode剖析浅谈[转自看雪]
    如何写一个简单的病毒程序[转]
    不用注册访问论坛技巧
    函数调用堆栈变化分析[转自看雪]
    IT学生解惑真经(下载)
    Windows 汇编语言编程教程[转]
    ESP定律介绍(转自看雪论坛贴)
    【Anti Virus专题】1.1 病毒的原理 [转自看雪]
  • 原文地址:https://www.cnblogs.com/tanhangbo/p/4282615.html
Copyright © 2011-2022 走看看