OPenCV直方图均衡 和 图像缩放

1. 图像缩放

 

#include <cv.h> #include <highgui.h> #include <math.h> int main(int argc, char* argv[]) { IplImage *src = 0; //源图像指针 IplImage *dst = 0; //目标图像指针 float scale = 0.618; //缩放倍数为0.618倍 CvSize dst_cvsize; //目标图像尺寸 /* the first command line parameter must be image file name */ if ( argc == 2 && (src = cvLoadImage(argv[1], -1))!=0 ) { //如果命令行传递了需要打开的图片就无须退出,所以注释掉下面一行! //return -1; } else { src = cvLoadImage("tiger.jpg"); //载入工作目录下文件名为“tiger.jpg”的图片。 } dst_cvsize.width = src->width * scale; //目标图像的宽为源图象宽的scale倍 dst_cvsize.height = src->height * scale; //目标图像的高为源图象高的scale倍 dst = cvCreateImage( dst_cvsize, src->depth, src->nChannels); //构造目标图象 cvResize(src, dst, CV_INTER_LINEAR); //缩放源图像到目标图像 cvNamedWindow( "src", CV_WINDOW_AUTOSIZE ); //创建用于显示源图像的窗口 cvNamedWindow( "dst", CV_WINDOW_AUTOSIZE ); //创建用于显示目标图像的窗口 cvShowImage( "src", src ); //显示源图像 cvShowImage( "dst", dst ); //显示目标图像 cvWaitKey(-1); //等待用户响应 cvReleaseImage(&src); //释放源图像占用的内存 cvReleaseImage(&dst); //释放目标图像占用的内存 cvDestroyWindow( "src" ); //销毁窗口“src” cvDestroyWindow( "dst" ); //销毁窗口“dst” //void cvDestroyAllWindows(void); return 0; } 

 

2. 直方图均衡

#include "cv.h" #include "highgui.h" #define HDIM 256 // bin of HIST, default = 256 //直方图均衡 int main( int argc, char** argv ) { IplImage *src = 0, *dst = 0, *src1=0; CvHistogram *hist = 0; int n = HDIM; double nn[HDIM]; uchar T[HDIM]; CvMat *T_mat; int x; int sum = 0; // sum of pixels of the source image 图像中象素点的总和 double val = 0; if( (src=cvLoadImage("save.jpg", 0)) == NULL) // force to gray image return -1; cvNamedWindow( "source", 1 ); cvNamedWindow( "result", 1 ); // calculate histgram 计算直方图 hist = cvCreateHist( 1, &n, CV_HIST_ARRAY, 0, 1 ); cvCalcHist( &src, hist, 0, 0 ); // Create Accumulative Distribute Function of histgram val = 0; for ( x = 0; x < n; x++) { val = val + cvGetReal1D (hist->bins, x); nn[x] = val; } // Compute intensity transformation 计算变换函数的离散形式 sum = src->height * src->width; for( x = 0; x < n; x++ ) { T[x] = (uchar) (255 * nn[x] / sum); // range is [0,255] } // Do intensity transform for source image dst = cvCloneImage( src ); T_mat = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( T_mat, T, 0 ); // directly use look-up-table function 直接调用内部函数完成 look-up-table 的过程 cvLUT( src, dst, T_mat ); cvShowImage( "source", src ); cvShowImage( "result", dst ); cvWaitKey(0); cvSaveImage("save.jpg", src); cvDestroyWindow("source"); cvDestroyWindow("result"); cvReleaseImage( &src ); cvReleaseImage( &dst ); cvReleaseHist ( &hist ); return 0; }  

你可能感兴趣的:(image,function,command,DST,transformation,math.h)