OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 2622|回复: 0

opencv程序报错问题

[复制链接]
发表于 2018-10-15 11:51:05 | 显示全部楼层 |阅读模式

大家好!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;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-4-29 12:36 , Processed in 0.008158 second(s), 14 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表