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);

}

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



原文地址:https://www.cnblogs.com/tanhangbo/p/4282602.html