关于 OpenCV的简绍,各位百度即可,这里我直接介绍OpenCV在VS2010中的配置,希望能对各位起到帮助。
首先到官网下载OpenCV,然后双击运行,解压到相应文件夹。这里我解压到了D:\tools目录,自动生成opencv-2.4.8文件夹,我的是2.4.8的版本。
1.设置环境变量
OpenCV库函数需要通过用户环境变量调用所需要的库文件。计算机->右击->属性->高级系统设置->环境变量
在系统变量path变量中增加:
D:\tools\opencv-2.4.8\opencv\build\x86\vc10\bin;
x86代表32位的,x64对应64位的程序
vc10说明是在vs2010中开发,若果是VS2012,则应是D:\tools\opencv-2.4.8\opencv\build\x86\vc11\bin;
2.在VS中新建项目后,右击选择属性
在左边选择Configuration Properties->VC++ Directories,在右边的General中编辑Include Directories,增加以下三项:
D:\tools\opencv-2.4.8\opencv\build\include
D:\tools\opencv-2.4.8\opencv\build\include\opencv
D:\tools\opencv-2.4.8\opencv\build\include\opencv2
在Library Directories中增加以下一项:
D:\tools\opencv-2.4.8\opencv\build\x86\vc10\lib
在Linker->input->additional dependencies添加以下若干,根据自己需要的功能选择,详细查询资料:
opencv_calib3d248.lib
opencv_contrib248.lib
opencv_core248.lib
opencv_features2d248.lib
opencv_flann248.lib
opencv_gpu248.lib
opencv_highgui248.lib
opencv_imgproc248.lib
opencv_legacy248.lib
opencv_ml248.lib
opencv_objdetect248.lib
opencv_ts248.lib
opencv_video248.lib
这里的248是对应的版本,注意不同版本及时修改。
现在就可以调用OpenCV的函数了,下面是个高斯模糊的例子,大家只要把路径改了,应该就可以运行了。
#include "highgui.h" #include "time.h" #include "math.h" #include "omp.h" //获取核函数 float *getWeight(int k, float sigma) { int radius = 2 * k + 1; float *filter = new float[radius*radius]; float sum = 0; float e = 2.71828; int i, j; for (i = 1; i<radius + 1; i++) for (j = 1; j<radius + 1; j++) { float cifang = -((i - k - 1)*(i - k - 1) + (j - k - 1)*(j - k - 1)) / (2 * sigma*sigma); filter[(i - 1)*radius + j - 1] = pow(e, cifang) / (2 * 3.14159*sigma*sigma); sum = sum + filter[(i - 1)*radius + j - 1]; } //将权值归一化 for (i = 0; i<radius; i++) { printf("\n"); for (int j = 0; j<radius; j++) { filter[i*radius + j] = filter[i*radius + j] / sum; } } return filter; } float *mgaussian(int height, int width, int k, float *weight, float *imagedata, int type) { float *newimage = new float[height*width*type]; for (int i = 0; i<height; i++) { if (i == 0) printf("core=%d\n", omp_get_num_threads()); for (int j = 0; j<width; j++) { //对边缘像素的处理 if (i<k || j<k || i >= height - k || j >= width - k) { for (int m = 0; m<type; m++) newimage[3 * width*i + 3 * j + m] = imagedata[3 * width*i + 3 * j + m]; continue; } //高斯核函数计算 for (int mtype = 0; mtype<type; mtype++) { float pixelval = 0; for (int nh = 0; nh<2 * k + 1; nh++) for (int nw = 0; nw<2 * k + 1; nw++) { pixelval = pixelval + weight[nh*(2 * k + 1) + nw] * imagedata[type*(i + nh - k)*width + type*(j + nw - k) + mtype]; } newimage[type*i*width + type*j + mtype] = pixelval; } } } return newimage; } int main(int argc, char** argv) { double start = clock(); IplImage* img = cvLoadImage("C:\\Users\\heng\\Desktop\\test.jpg"); int mwidth, mheight, mtype; int i, j, k = 1; float sigma = 2; // printf("请输入高斯核函数的半径:"); // scanf("%d",&k); //printf("请输入高斯核函数的方差sigma:"); // scanf("%f",&sigma); mwidth = img->width; mheight = img->height; mtype = img->nChannels; //CvMat *imageMat=cvCreateMat(mheight,mheight*mtype,CV_32FC1); float *imageMat = new float[mwidth*mheight*mtype]; float *Gweight = getWeight(k, sigma); //去除像素点放到imageMat中 for (i = 0; i<mheight; i++) for (j = 0; j<mwidth; j++) { CvScalar s; s = cvGet2D(img, i, j);// get the (i,j) pixel value for (int index = 0; index<mtype; index++) { imageMat[3 * mwidth*i + 3 * j + index] = s.val[index]; } } float *imageMat2 = mgaussian(mheight, mwidth, k, Gweight, imageMat, mtype); IplImage* img2 = cvCreateImage(cvSize(mwidth, mheight), IPL_DEPTH_8U, mtype); //将处理后的图像值放入图像中显示 for (i = 0; i<mheight; i++) for (j = 0; j<mwidth; j++) { CvScalar s; for (int mmtype = 0; mmtype<mtype; mmtype++) { int p = int(imageMat2[mtype*mwidth*i + mtype*j + mmtype] + 0.5); if (p>255) p = 255; s.val[mmtype] = p; } cvSet2D(img2, i, j, s); } cvNamedWindow("显示原图像", CV_WINDOW_AUTOSIZE); cvShowImage("显示原图像", img); cvNamedWindow("显示高斯滤波处理后的图像", CV_WINDOW_AUTOSIZE); cvShowImage("显示高斯滤波处理后的图像", img2); double end = clock(); printf("time=%f", end - start); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&img2); cvDestroyWindow("显示原图像"); cvDestroyWindow("显示高斯滤波处理后的图像"); delete[] imageMat; delete[] imageMat2; }