• Opencv step by step



    上个博客提到的阈值化只是针对图像全局进行阈值化,opencv提供了一个更好的函数cvAdaptiveThreshold,可以做到局部特征的阈值化,这样一来,

    整个图像的信息可以被更好的提取。


    #include <cv.h>
    #include <highgui.h>
    #include "math.h"
    
    IplImage *img_gray = NULL, *img_thres = NULL, *img_adaptive = NULL;
    
    
    
    
    
    int main(int argc, char **argv)
    {
    
    	double threadshould = 100.0;
    	int threadshould_type = CV_THRESH_BINARY;
    	int adaptive_method = CV_ADAPTIVE_THRESH_GAUSSIAN_C;
    	int block_size = 11;
    	double offset = 5;
    
    
    	if(NULL == (img_gray = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE)))
    		return -1;
    
    	img_thres = cvCreateImage(cvGetSize(img_gray), img_gray->depth, 1);
    	img_adaptive = cvCreateImage(cvGetSize(img_gray), img_gray->depth, 1);
    
    	cvThreshold(img_gray, img_thres, threadshould, 255, threadshould_type);
    	cvAdaptiveThreshold(img_gray, img_adaptive, 255, adaptive_method, threadshould_type, block_size, offset);
    
    
    
    	cvNamedWindow("ORG", 1);
    	cvNamedWindow("THRES", 1);
    	cvNamedWindow("ADAPTIVE_THRES", 1);
    
    
    	cvShowImage("ORG", img_gray);
    	cvShowImage("THRES", img_thres);
    	cvShowImage("ADAPTIVE_THRES", img_adaptive);
    
    
    	while(1) {
    
    		if(cvWaitKey(10) & 0x7f == 27)
    			break;
    
    	}
    
    
    	cvDestroyWindow("ORG");
    	cvDestroyWindow("THRES");
    	cvDestroyWindow("ADAPTIVE_THRES");
    	cvReleaseImage(&img_gray);
    	cvReleaseImage(&img_thres);
    	cvReleaseImage(&img_adaptive);
    
    }

    这里使用了阈值化和自适应阈值的比较。可以简单的看效果,明显是自适应阈值比较容易提取特征(虽然左图好看一点):



  • 相关阅读:
    UWP开发必备:常用数据列表控件汇总比较
    CodeForces 372 A. Counting Kangaroos is Fun
    ubuntu 13.10 eclipse 菜单栏不可用的问题
    Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)
    Git/Github使用方法小记
    Ubuntu 下jdk的安装
    VIM简明教程
    codeforces 371 C-Hamburgers
    codeforces 371A K-Periodic Array
    计算机网络中IP地址和MAC地址
  • 原文地址:https://www.cnblogs.com/tanhangbo/p/4282602.html
Copyright © 2020-2023  润新知