opencv学习日记——检测直线并绘制直线

opencv学习日记——检测直线并绘制直线

1、图像处理结果

效果不尽人意,感觉还是适合单一图形的检测
在这里插入图片描述

2、代码部分

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

//画出直线
void drawLine(Mat &img,
              vector<Vec2f> lines,
              double rows,
              double cols,
              Scalar scalar,
              int n
              )
{
    Point pt1, pt2;
    for (size_t i = 0; i < lines.size(); i++)
    {
        float rho = lines[i][0];
        float theta = lines[i][1];
        double a = cos(theta);
        double b = sin(theta);
        double x0 = a * rho, y0 = a * rho;
        double length = max(rows, cols);

        pt1.x = cvRound(x0 + length * (-b));
        pt2.y = cvRound(y0 + length * (a));

        pt1.x = cvRound(x0 - length * (-b));
        pt2.y = cvRound(y0 - length * (a));

        line(img, pt1, pt2, scalar, n);
    }

}

int main()
{
    Mat img = imread("a11.jpeg", IMREAD_GRAYSCALE);
    if (img.empty())
    {
        cout << "输入图像为空";
        return -1;
    }

    Mat edge;
    //边缘检测
    //Canny(原图, 新图, 阈值, 阈值, 卷积核大小)
    Canny(img, edge, 400, 600, 3, false);
    //二值化
    //threshold(原图, 新图, 阈值, 阈值, 类型)
    threshold(edge, edge, 100, 400, THRESH_BINARY);

    vector<Vec2f> lines1, lines2;
    //直线检测
    //HoughLines(原图, 直线信息, 极坐标下的长度, 极坐标下的角度, 阈值, 距离分辨率除数, 弧度分辨率除数)
    HoughLines(edge, lines1, 1, CV_PI / 180, 200, 0, 0);
    HoughLines(edge, lines2, 1, CV_PI / 180, 150, 0, 0);

    Mat img1, img2;
    img.copyTo(img1);
    img.copyTo(img2);
    drawLine(img1, lines1, edge.rows, edge.cols, Scalar(255), 2);
    drawLine(img2, lines2, edge.rows, edge.cols, Scalar(255), 2);

    imshow("edge", edge);
    imshow("img", img);
    imshow("img1", img1);
    imshow("img2", img2);

    waitKey(0);






    
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python和OpenCV中实现直线检测,可以使用Hough变换来检测直线。Hough变换是一种常用的图像处理方法,可用于检测直线、圆等几何形状。 以下是一个简单的示例代码,使用Hough变换来检测直线并计算交点: ```python import cv2 import numpy as np # 读取图像 img = cv2.imread('test.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 边缘检测 edges = cv2.Canny(gray, 50, 150, apertureSize=3) # Hough变换检测直线 lines = cv2.HoughLines(edges, 1, np.pi/180, 200) # 计算交点 points = [] for i in range(len(lines)): for j in range(i+1, len(lines)): rho1, theta1 = lines[i][0] rho2, theta2 = lines[j][0] if abs(theta1 - theta2) < np.pi/4: continue a = np.array([[np.cos(theta1), np.sin(theta1)], [np.cos(theta2), np.sin(theta2)]]) b = np.array([rho1, rho2]) x0, y0 = np.linalg.solve(a, b) points.append((int(x0), int(y0))) # 绘制直线和交点 for line in lines: rho, theta = line[0] a = np.cos(theta) b = np.sin(theta) x0 = a*rho y0 = b*rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(img, (x1,y1), (x2,y2), (0,0,255), 2) for point in points: cv2.circle(img, point, 5, (0,255,0), -1) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在代码中,首先读取图像并进行灰度转换和边缘检测。然后使用Hough变换检测直线,并计算交点。最后绘制直线和交点,并显示图像。 需要注意的是,在计算交点时,需要将两条直线的极坐标表示转换为直角坐标表示,并使用线性方程组求解。 希望这个例子能够帮助到你实现直线检测并计算交点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值