下载opencv,解压,配置环境变量,添加opencv安装路径\build\x64\vc15\bin (vc14 = Visual Studio 2015,vc15 = Visual Studio 2017及以上)。
新建项目
配置管理器选择x64
解决方案资源管理器,右键项目,属性,
VC++目录—VC++目录—包含目录:
...\opencv\build\include
...\opencv\build\include\opencv
...\opencv\build\include\opencv2
库目录 配置:
...\opencv\build\x64\vc15\lib
链接器 配置:
链接器–>输入–>附加依赖项
opencv_world430d.lib
测试代码(官方Houph直线检测的代码,做了一点修改)
#include
#include
#include
#include "opencv2/imgproc.hpp"
#include
#include
int main(void)
{
IplImage* src = cvLoadImage("D:\\11.jpg", 0);
if (src)
{
IplImage* dst = cvCreateImage(cvGetSize(src), 8, 1);
IplImage* color_dst = cvCreateImage(cvGetSize(src), 8, 3);
CvMemStorage* storage = cvCreateMemStorage();
CvSeq* lines = 0;
int i;
cvCanny(src, dst, 50, 200, 3);
cvCvtColor(dst, color_dst, CV_GRAY2BGR);
#if 0
lines = cvHoughLines2(dst, storage, CV_HOUGH_STANDARD, 1, CV_PI / 180, 150, 0, 0);
for (i = 0; i < lines->total; i++)
{
float* line = (float*)cvGetSeqElem(lines, i);
float rho = line[0];
float theta = line[1];
CvPoint pt1, pt2;
double a = cos(theta);
double b = sin(theta);
if (fabs(a) < 0.001)
{
pt1.x = pt2.x = cvRound(rho);
pt1.y = 0;
pt2.y = color_dst->height;
}
else if (fabs(b) < 0.001)
{
pt1.y = pt2.y = cvRound(rho);
pt1.x = 0;
pt2.x = color_dst->width;
}
else
{
pt1.x = 0;
pt1.y = cvRound(rho / b);
pt2.x = cvRound(rho / a);
pt2.y = 0;
}
cvLine(color_dst, pt1, pt2, cvScalar(255, 0, 0), 1, 8);
}
#else
std::vector X;
std::vector Y;
lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI / 180, 80, 30, 5);
for (i = 0; i < lines->total; i++)
{
CvPoint* line = (CvPoint*)cvGetSeqElem(lines, i);
cvLine(color_dst, line[0], line[1], cvScalar(255, 0, 0), 1, CV_AA);
cvCircle(color_dst, line[0], 3, cvScalar(0, 0, 255), -1);
cvCircle(color_dst, line[1], 3, cvScalar(0, 0, 255), -1);
X.push_back(line->x);
Y.push_back(line->y);
}
#endif
for (int i = 0; i < X.size(); i++) {
std::cout << X[i] << "," << Y[i] << std::endl;
}
cvNamedWindow("Source");
cvShowImage("Source", src);
cvNamedWindow("Hough");
cvShowImage("Hough", color_dst);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseImage(&color_dst);
cvReleaseMemStorage(&storage);
cvDestroyAllWindows();
return 1;
}
}