一、准备工作
1、安装VS2013, 破解码为:BWG7X-J98B3-W34RT-33B3R-JVYW9
2、安装opencv-3.1.0,并配置环境变量。
环境变量:把D:\opencv\build\x64\vc11\bin加入系统变量Path里面。
二、在vs里配置Opencv
1、打开vs,点击文件—>新建—>项目—>win32控制台空项目,并取项目名字,如OpencvTest。
2、鼠标右击OpencvTest项目,属性—>配置属性—>VC++目录,
(1) 右边包含目录—>编辑—>添加,选择路径\opencv\build\include,\opencv\build\include\opencv, \opencv\build\include\opencv2,添加三个路径,点确定。
(2) 库目录—>编辑—>添加,选择路径\opencv\build\x64\vc11\lib,点确定。
(3) 链接器—>输入—>附加依赖项,编辑,添加依赖项,添加内容如下,后面的249为opencv版本号,可修改。
opencv_core249d.lib
opencv_imgproc249d.lib
opencv_highgui249d.lib
opencv_ml249d.lib
opencv_video249d.lib
opencv_features2d249d.lib
opencv_calib3d249d.lib
opencv_objdetect249d.lib
opencv_contrib249d.lib
opencv_legacy249d.lib
opencv_flann249d.lib
3、配置管理器,Debug—>新建—>x64—>确定。
4、附上可运行代码,检查是否配置成功。
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img1 = imread("1.png", IMREAD_GRAYSCALE);
if (img1.empty())
{
cout << "Cannot load image!" << endl;
return -1;
}
imshow("Original", img1); // Original
// Create mask image
Mat mask(img1.rows, img1.cols, CV_8UC1, Scalar(0,0,0));
circle(mask, Point(img1.rows/2,img1.cols/2), 150, 255, -1);
imshow("Mask",mask);
// perform AND
Mat r;
bitwise_and(img1,mask,r);
// fill outside with white
const uchar white = 255;
for(int i = 0; i < r.rows; i++)
for(int j = 0; j < r.cols; j++)
if (!mask.at<uchar>(i,j))
r.at<uchar>(i,j)=white;
imshow("Result",r);
waitKey(0);
return 0;
}