opencv-python 直线检测并延长

本文介绍了一种基于OpenCV的Canny边缘检测和HoughLinesP直线检测算法的表格提取方法,通过应用中学直线方程知识,实现对检测到的直线进行延长,以适应不同场景需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用Opencv中的Canny边缘检测基于HoughLinesP检测出来的直线,在适当的场景下需要延长检测出来的直线(表格提取)

原理就是中学学的直线方程的的应用.

def Extend_line(x1, y1, x2, y2, x, y, flag):
    if flag == 1:
        if y1 == y2:
            return 0, y1, x, y2
        else:
            k = (y2 - y1) / (x2 - x1)
            b = (x1*y2-x2*y1)/(x1-x2)
            x3 = 0
            y3 = b
            x4 = x
            y4 = int(k * x4+b)
        return x3, y3, x4, y4
    else:
        if x1 == x2:
            return x1, 0, x2, y
        else:
            k = (y2 - y1) / (x2 - x1)
            b = (x1 * y2 - x2 * y1) / (x1 - x2)
            y3 = 0
            x3 = int(-1*b/k)
            y4 = y
            x4 = int((y4-b)/k)
            return x3, y3, x4, y4

 

这里提供一个简单的基于PythonOpenCV的方法来检测屏幕上的白色直线延长它们。 首先,我们需要安装OpenCV导入它: ```python import cv2 ``` 然后,我们需要捕捉屏幕截图将其转换为灰度图像。这可以通过使用Pillow库中的ImageGrab模块和OpenCV的cvtColor函数来实现: ```python import numpy as np from PIL import ImageGrab # 获取屏幕截图 screen = np.array(ImageGrab.grab()) # 将截图转换为灰度图像 gray = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) ``` 接下来,我们可以使用OpenCV中的HoughLinesP函数来检测直线。这个函数可以帮助我们检测图像中的直线返回一个包含直线起点和终点坐标的数组。 ```python # 检测直线 lines = cv2.HoughLinesP(gray, 1, np.pi/180, threshold=100, minLineLength=100, maxLineGap=10) ``` 在我们得到直线数组之后,我们可以遍历它使用OpenCV的line函数来绘制直线。我们还可以使用直线的斜率和截距来计算直线延长线的起点和终点坐标。 ```python # 遍历直线数组绘制直线 for line in lines: x1, y1, x2, y2 = line[0] cv2.line(screen, (x1, y1), (x2, y2), (0, 255, 0), 2) # 计算直线的斜率和截距 slope = (y2 - y1) / (x2 - x1) intercept = y1 - slope * x1 # 计算直线延长线的起点和终点 extended_x1 = int(x1 - 1000 * slope) extended_y1 = int(intercept - 1000) extended_x2 = int(x2 + 1000 * slope) extended_y2 = int(intercept + 1000) # 绘制直线延长线 cv2.line(screen, (extended_x1, extended_y1), (extended_x2, extended_y2), (0, 0, 255), 2) # 显示结果 cv2.imshow('Screen', screen) cv2.waitKey(0) cv2.destroyAllWindows() ``` 最后,我们可以使用OpenCV的imshow函数来显示结果。我们还可以使用waitKey函数等待用户按下任意键关闭窗口。 完整代码如下: ```python import cv2 import numpy as np from PIL import ImageGrab # 获取屏幕截图 screen = np.array(ImageGrab.grab()) # 将截图转换为灰度图像 gray = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) # 检测直线 lines = cv2.HoughLinesP(gray, 1, np.pi/180, threshold=100, minLineLength=100, maxLineGap=10) # 遍历直线数组绘制直线 for line in lines: x1, y1, x2, y2 = line[0] cv2.line(screen, (x1, y1), (x2, y2), (0, 255, 0), 2) # 计算直线的斜率和截距 slope = (y2 - y1) / (x2 - x1) intercept = y1 - slope * x1 # 计算直线延长线的起点和终点 extended_x1 = int(x1 - 1000 * slope) extended_y1 = int(intercept - 1000) extended_x2 = int(x2 + 1000 * slope) extended_y2 = int(intercept + 1000) # 绘制直线延长线 cv2.line(screen, (extended_x1, extended_y1), (extended_x2, extended_y2), (0, 0, 255), 2) # 显示结果 cv2.imshow('Screen', screen) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值