opencv程序报错问题

weixin_38202930 2018-10-15 12:14:31
大家好!opencv小白一枚,从网上找了个检测直线检测并求交点的代码,但是生成解决方案成功,却运行不了,报错如下:

引发了异常: 读取访问权限冲突。

cv::Vec<int,4>:perator[](...) 返回 0x7536004。

如有适用于此异常的处理程序,该程序便可安全地继续运行。

求助各位大神!!万分感谢!!!

代码如下:
# include <opencv2/opencv.hpp>
# include <iostream>

using namespace cv;
using namespace std;

/*函数功能:求两条直线交点*/
/*输入:两条Vec4i类型直线*/
/*返回:Point2f类型的点*/
Point2f getCrossPoint(Vec4i LineA, Vec4i LineB)
{
double ka, kb;
ka = (double)(LineA[3] - LineA[1]) / (double)(LineA[2] - LineA[0]); //求出LineA斜率
kb = (double)(LineB[3] - LineB[1]) / (double)(LineB[2] - LineB[0]); //求出LineB斜率

Point2f crossPoint;
crossPoint.x = (ka*LineA[0] - LineA[1] - kb*LineB[0] + LineB[1]) / (ka - kb);
crossPoint.y = (ka*kb*(LineA[0] - LineB[0]) + ka*LineB[1] - kb*LineA[1]) / (ka - kb);
return crossPoint;
}

int main() {

Mat src, grayImg, binImg, result;
src = imread("./1.jpg");
//imshow("srcimage", src);
//waitKey(30);

/*检查图像是否载入*/
if (src.empty()) {
printf("Error Loading Image...\n");
return -1;
}

/*转为灰度图*/
if (src.channels() == 3) {
cvtColor(src, grayImg, CV_BGR2GRAY);
}
else if (src.channels() == 2) {
grayImg = src.clone();
}

/*二值化*/
threshold(grayImg, binImg, 100, 255, THRESH_BINARY);
//adaptiveThreshold(grayImg, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
//imshow("binary image", binImg);
//waitKey(100);

/*腐蚀*/
Mat element = getStructuringElement(MORPH_RECT, Size(2, 1));
Mat erodeImg;
erode(binImg, erodeImg, element);
//imshow("erode", erodeImg);
//waitKey(100);

/*霍夫直线检测*/
vector<Vec4i> Lines;
HoughLinesP(erodeImg, Lines, 1, CV_PI / 360, 200, 100, 10);
Vec4i LineStand = Lines[0];
Vec4i LineAnother;
double ka = (double)(LineStand[1] - LineStand[3]) / (double)(LineStand[0] - LineStand[2]);
double kb;
for (int i = 1; i < Lines.size(); i++)
{
double ki = (double)(Lines[i][1] - Lines[i][3]) / (double)(Lines[i][0] - Lines[i][2]);
if (ki*ka < 0)
{
LineAnother = Lines[i];
kb = ki;
}
}

/*画出两条直线*/
result = src.clone();
line(result, Point(LineStand[0], LineStand[1]), Point(LineStand[2], LineStand[3]), Scalar(0, 255, 0), 2, 8);
line(result, Point(LineAnother[0], LineAnother[1]), Point(LineAnother[2], LineAnother[3]), Scalar(0, 0, 255), 2, 8);
cout << "直线A过点(" << LineStand[0] << "," << LineStand[1] << ")以及点(" << LineStand[2] << "," << LineStand[3] << ");斜率为:" << ka << endl;
cout << "直线B过点(" << LineAnother[0] << "," << LineAnother[1] << ")以及点(" << LineAnother[2] << "," << LineAnother[3] << ");斜率为:" << kb << endl;

/*求交点并画点保存,result.jpg存储在工程目录下*/
Point2f crossPoint;
crossPoint = getCrossPoint(LineStand, LineAnother);
circle(result, crossPoint, 6, Scalar(255, 0, 0));
imwrite("./result.jpg", result);
cout << "Copyrigth@zym" << endl;
cout << "交点坐标为:" << crossPoint << endl;
imshow("result", result);
waitKey(100);
system("pause");

return 0;
}
...全文
317 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
robertbo 2018-10-19
  • 打赏
  • 举报
回复
用debug模式逐步运行,定位到错误地方在哪。
程序中处理流程应该没问题,就是缺少很多判断条件,比如读取的图像中没有直线的话,后面直接调用Lines[0]就会报错。诸如此类的判断需要多加一些。
赵4老师 2018-10-19
  • 打赏
  • 举报
回复
引用 2 楼 robertbo 的回复:
用debug模式逐步运行,定位到错误地方在哪。
程序中处理流程应该没问题,就是缺少很多判断条件,比如读取的图像中没有直线的话,后面直接调用Lines[0]就会报错。诸如此类的判断需要多加一些。

小心驶得万年船。
赵4老师 2018-10-15
  • 打赏
  • 举报
回复
将opencv*.dll拷贝到exe文件所在目录下。

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧