1.首先下载所需的opencv库
直接在官网release中查找所需版本
选择合适自己的平台
我下载的是较低版本的,防止不兼容。
之后点击解压到自己所需的文件夹中
之后在对应文件夹之下便会出现相应的文件
添加环境变量
因为opencv要在x64环境下运行,我配置时选择所有配置,也有专门配置debug x64的配置的,感觉比较复杂,结果是一样的(目前的认知)。
首先配置相关的头文件路径
将下面路径包含到附加包含目录下,这个版本下只有这一个目录,其它的安装可能会有opencv文件夹,也可以包含进去。
然后是添加库目录bin,我选择的是vc15
程序需要运行,需要将动态链接库添加到项目文件夹中。
我是指定的解压opencv的路径,如果为了项目更加稳定,可以在项目文件夹下创建相关文件夹之后,将所需库文件复制过去,然后再指定头文件目录和附加库目录。最后在程序运行时要x64
2.下面是一个测试小程序,去除注释可以测试其他功能。
#define WINDOW_SIZE 200
using namespace cv;
using namespace std;
int main()
{
//vector<Point> point;
//point.push_back(Point(13, 16));
//point.push_back(Point(42, 63));
//std::cout << "point" << point[0] << std::endl;
Mat image = imread("C://Users//lenovo//Desktop//ffff.jpg", 1);
namedWindow("Original Image", 1);
Mat gray;
cvtColor(image, gray, COLOR_RGB2GRAY);
// Show stuff
/*cout << "dims="<<image.dims <<"\n"<< endl;
cout << "flags=" << image.flags << "\n" << endl;
cout << "rows=" << image.rows << "\n" << endl;
cout << "cols=" << image.cols << "\n" << endl;
cout << "size=" << image.size << "\n" << endl;
int x = 125, y = 125;
cv::Vec3b vec3b = image.at<cv::Vec3b>(x, y);
uchar vec3b0 = image.at<cv::Vec3b>(x, y)[0];
uchar vec3b1 = image.at<cv::Vec3b>(x, y)[1];
uchar vec3b2 = image.at<cv::Vec3b>(x, y)[2];
std::cout << "vec3b = " << vec3b << std::endl;
std::cout << "vec3b0 = " << (int)vec3b0 << std::endl;
std::cout << "vec3b1 = " << (int)vec3b1 << std::endl;
std::cout << "vec3b2 = " << (int)vec3b2 << std::endl;*/
//int lineType = 0;
创建一些点
//Point rookPoints[1][20];
//rookPoints[0][0] = Point(WINDOW_SIZE / 4, 7 * WINDOW_SIZE / 8);
//rookPoints[0][1] = Point(3 * WINDOW_SIZE / 4, 7 * WINDOW_SIZE / 8);
//rookPoints[0][2] = Point(3 * WINDOW_SIZE / 4, 13 * WINDOW_SIZE / 16);
//rookPoints[0][3] = Point(11 * WINDOW_SIZE / 16, 13 * WINDOW_SIZE / 16);
//rookPoints[0][4] = Point(19 * WINDOW_SIZE / 32, 3 * WINDOW_SIZE / 8);
//rookPoints[0][5] = Point(3 * WINDOW_SIZE / 4, 3 * WINDOW_SIZE / 8);
//rookPoints[0][6] = Point(3 * WINDOW_SIZE / 4, WINDOW_SIZE / 8);
//rookPoints[0][7] = Point(26 * WINDOW_SIZE / 40, WINDOW_SIZE / 8);
//rookPoints[0][8] = Point(26 * WINDOW_SIZE / 40, WINDOW_SIZE / 4);
//rookPoints[0][9] = Point(22 * WINDOW_SIZE / 40, WINDOW_SIZE / 4);
//rookPoints[0][10] = Point(22 * WINDOW_SIZE / 40, WINDOW_SIZE / 8);
//rookPoints[0][11] = Point(18 * WINDOW_SIZE / 40, WINDOW_SIZE / 8);
//rookPoints[0][12] = Point(18 * WINDOW_SIZE / 40, WINDOW_SIZE / 4);
//rookPoints[0][13] = Point(14 * WINDOW_SIZE / 40, WINDOW_SIZE / 4);
//rookPoints[0][14] = Point(14 * WINDOW_SIZE / 40, WINDOW_SIZE / 8);
//rookPoints[0][15] = Point(WINDOW_SIZE / 4, WINDOW_SIZE / 8);
//rookPoints[0][16] = Point(WINDOW_SIZE / 4, 3 * WINDOW_SIZE / 8);
//rookPoints[0][17] = Point(13 * WINDOW_SIZE / 32, 3 * WINDOW_SIZE / 8);
//rookPoints[0][18] = Point(5 * WINDOW_SIZE / 16, 13 * WINDOW_SIZE / 16);
//rookPoints[0][19] = Point(WINDOW_SIZE / 4, 13 * WINDOW_SIZE / 16);
//const Point* ppt[1] = { rookPoints[0] };
//int npt[] = { 20 };
//fillPoly(image,
// ppt,
// npt,
// 1,
// Scalar(255, 255, 255),
// lineType);
imshow("Original Image", image);
imshow("gray Image", gray);
// Wait until user press some key
waitKey();
return 0;
由于环境不同,若安装出问题请在下面留言,大家一起交流。