1. 安装VS2015与OpenCV
下载地址:https://opencv.org/releases.html,将下载的exe文件,解压/安装到选定目录即可。
Note:OpenCV自带的只有x64,x86需要自己编译。下面仅以x64为例配置。
VS2015+OpenCV4可参考:https://blog.csdn.net/toandylee/article/details/84837551
2. 配置系统的环境变量
若不配置系统的环境变量,运行时可能会提示:
错误,丢失opencv_word320.dll
等。
方法1. 右键计算机->属性->高级系统设置->高级(标签)->环境变量,在Path
中加入bin
目录。
方法2. 在命令行中输入(用英文分号;
分隔):set PATH=X:\opencv\build\x64\vc14\bin;%PATH%
然后重启电脑即可。
3. 配置VS2015
3.1 建立一个C++控制台空项目
3.2 打开属性管理器:
视图->属性管理器,点开Release|64
->Microsoft.Cpp.x64.user
,右键属性
。
3.3 在VC++目录->包含目录
填入:
X:\opencv\build\include
X:\opencv\build\include\opencv
X:\opencv\build\include\opencv2
3.4 在VC++目录->库目录
填入:
X:\opencv\build\x64\vc14\lib
3.5 在链接器->输入->附加依赖项
填入:
opencv_world320.lib 或 opencv_world320d.lib
备注:opencv_world320.lib用于
Release
,opencv_world320d.lib用于Debug
,只能二选一。
4.测试样例:
#include <iostream>
#include <opencv.hpp>
using namespace cv;
using namespace std;
static Mat onAffine(Mat* im, Point2f dif);
static Mat onRot(int pos, int rat, Mat* im);
static void onMouse(int Event, int x, int y, int drag, Mat* im);
void main(int argc, char* argv[])
{
Mat Cam = imread("Girl.png"), dst;
int pos = 36, rat = 10, hes = 40; //initial value
namedWindow("Video");
createTrackbar("Angle", "Video", &pos, 72); //rotate the image
createTrackbar("Scale", "Video", &rat, 20); //scale the image
setMouseCallback("Video", (MouseCallback)onMouse, &Cam); //drag image
while ((waitKey(50) & 255) != 13)
{//press the Enter key to exit the programe
dst = onRot(pos, rat, &Cam); imshow("Video", dst);
}//end while
}//end main
static void onMouse(int Event, int x, int y, int drag, Mat* im)
{
static Mat res = im->clone(); static const Point ini(0, 0);
static Point seed(ini), tdf(ini); Point now(x, y);
switch (Event)
{
case CV_EVENT_LBUTTONDOWN: seed = now; break;
case CV_EVENT_LBUTTONUP: tdf += (now - seed); break;
case CV_EVENT_RBUTTONUP: res.copyTo(*im); tdf = ini; break;
}//end switch
if (drag == CV_EVENT_FLAG_LBUTTON) //drag the mouse to move the image
*im = onAffine(&res, tdf + (now - seed)); //use Affine transformation
}//end onMouse
static Mat onAffine(Mat* im, Point2f dif)
{
Point2f org[3], tri[3]; org[1] = Point2f(im->cols / 2, 0);
org[0] = Point2f(0, 0); org[2] = Point2f(0, im->rows / 2);
for (int i = 0; i<3; ++i) tri[i] = org[i] + dif;
Mat warp = getAffineTransform(org, tri), dst;
warpAffine(*im, dst, warp, im->size()); return dst;
}//end onAffine
static Mat onRot(int pos, int rat, Mat* im)
{
Point center(im->cols/2.0 + 0.5, im->rows/2.0 + 0.5);
double angle = 5 * pos - 180, scale = 0.1*rat;
Mat rot = getRotationMatrix2D(center, angle, scale), dst;
warpAffine(*im, dst, rot, im->size()); return dst;
}//end onRot
5.备注:
OpenCV3.2移除了默认的SURF和SIFT库,放到了OpenCV的
extra modules
(即opencv_contrib)中。SURF和SIFT的源码,参见:https://github.com/opencv/opencv_contrib/tree/master/modules/xfeatures2d/src
编译opencv_contrib,可参见:http://blog.csdn.net/cosmispower/article/details/60601151
opencv_aruco320.lib
opencv_bgsegm320.lib
opencv_bioinspired320.lib
opencv_calib3d320.lib
opencv_ccalib320.lib
opencv_core320.lib
opencv_datasets320.lib
opencv_dnn320.lib
opencv_dpm320.lib
opencv_face320.lib
opencv_features2d320.lib
opencv_flann320.lib
opencv_fuzzy320.lib
opencv_highgui320.lib
opencv_imgcodecs320.lib
opencv_imgproc320.lib
opencv_line_descriptor320.lib
opencv_ml320.lib
opencv_objdetect320.lib
opencv_optflow320.lib
opencv_phase_unwrapping320.lib
opencv_photo320.lib
opencv_plot320.lib
opencv_reg320.lib
opencv_rgbd320.lib
opencv_saliency320.lib
opencv_shape320.lib
opencv_stereo320.lib
opencv_stitching320.lib
opencv_structured_light320.lib
opencv_superres320.lib
opencv_surface_matching320.lib
opencv_text320.lib
opencv_tracking320.lib
opencv_video320.lib
opencv_videoio320.lib
opencv_videostab320.lib
opencv_xfeatures2d320.lib
opencv_ximgproc320.lib
opencv_xobjdetect320.lib
opencv_xphoto320.lib