前端之家收集整理的这篇文章主要介绍了
初学opencv-单一阈值与自适应阈值,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include<stdio.h>
#include<cv.h>
#include<highgui.h>
#include "math.h"
IplImage *Igray=0,*It=0,*Iat;
//cvAdaptiveThreshold(src,dst,max_val,adaptive_method,threshold_type,block_size,param1)
//上述方法有两种不同的自适应阈值方法,通过参数adaptive_method设定
//上述方法是计算像素点周围block_size*block_size区域的加权平均,然后减去常数param1得到
int main(int argc,char**argv){
if(argc!=7)return -1;
double threshold=(double)atof(argv[1]);//设定阈值
int threshold_type=atoi(argv[2])?CV_THRESH_BINARY:CV_THRESH_BINARY_INV;//设置阈值类型
int adaptive_method=atoi(argv[3])?CV_ADAPTIVE_THRESH_MEAN_C:CV_ADAPTIVE_THRESH_GAUSSIAN_C;//通过变量设置自适应阈值
int block_size=atoi(argv[4]);
double offset=(double)atof(argv[5]);
if((Igray=cvLoadImage(argv[6],CV_LOAD_IMAGE_GRAYSCALE))==0){return -1;}//通过在dos命令下输入图像的位置来加载图像
It=cvCreateImage(cvSize(Igray->width,Igray->height),IPL_DEPTH_8U,1);
Iat=cvCreateImage(cvSize(Igray->width,1);
cvThreshold(Igray,It,threshold,255,threshold_type);
//自适应阈值化,改进了cvThreshold()
cvAdaptiveThreshold(Igray,Iat,offset);
cvNamedWindow("raw",1);
cvNamedWindow("Threshold",1);
cvNamedWindow("AdaptiveThreshold",1);
cvShowImage("raw",Igray);
cvShowImage("Threshold",It);
cvShowImage("AdaptiveThreshold",Iat);
cvWaitKey(0);
cvReleaseImage(&Igray);
cvReleaseImage(&It);
cvReleaseImage(&Iat);
cvDestroyAllWindows();
}