最近这两天一直在研究命令行参数的编译,现代吗如下:
#include <highgui.h>
#include <math.h>
#include <cv.h>
IplImage *Igray = 0, *It = 0, *Iat;
int main( int argc, char** argv )
{
//输入命令行
double threshold = (double)atof( argv[1] ); //convert string to double
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;
}
//创建同样大小8位灰度图用于输出
It = cvCreateImage( cvSize( Igray -> width, Igray -> height ), IPL_DEPTH_8U, 1 ); //单通道8位灰度图
Iat = cvCreateImage( cvSize( Igray -> width, Igray -> height ), IPL_DEPTH_8U, 1 );
//阈值化
cvThreshold( Igray, It, threshold, 255, threshold_type );
cvAdaptiveThreshold( Igray, Iat, 255, adaptive_method, threshold_type, block_size, offset );
//命名窗体输出
cvNamedWindow( "Raw", 1 );
cvNamedWindow( "Threshold", 1 );
cvNamedWindow( "Adaptive Threshold", 1 );
cvShowImage( "Raw", Igray );
cvShowImage( "Threshold", It );
cvShowImage( "Adaptive Threshold", Iat );
cvWaitKey(0);
//回收内存
cvReleaseImage( &Igray );
cvReleaseImage( &It );
cvReleaseImage( &Iat );
cvDestroyWindow( "Raw" );
cvDestroyWindow( "Threshold" );
cvDestroyWindow( "Adaptive Threshold" );
return 0;
}
上述代码是二值化和自适应二值化的比较,在该工程文件夹同级目录下面的Debug文件找到 自适应阈值.exe 文件,然后打开命令行提示符,针对上述文件可进行下面操作:
1,首先进到Debug文件,找到.exe文件,如D:\myopencv\opencv\Code\Opencv\Debug
2,在命令行输入下面:
D:\myopencv\opencv\Code\Opencv\Debug>自适应阈值.exe 15 1 1 71 15 D:\myopencv\opencv\Code\Opencv\自适应阈值\2.jpg
3,结果如下:
简单的和大家分享一下。