因此,作为OpenCV的新手,我try 检测图像的一部分("PSSU"和"356750/22G1"字符之间的粗线),然后从原始图像中减go 它.我想有一个干净的最终图像,然后我可以通过OCR.

enter image description here

我已经成功地检测到了这条线(红色突出显示). 线路检测代码如下所示:

enter image description here

import cv2
import numpy as np
 
# Reading the required image in
# which operations are to be done.
# Make sure that the image is in the same
# directory in which this python program is
img = cv2.imread('c:\\ml\\test.jpg')
 
# Convert the img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
# Apply edge detection method on the image
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
 
# This returns an array of r and theta values
# 4th value ( 400 ) is threshold of how thick the line is thats to be detected.   Higher value = thicker line to be detected.
lines = cv2.HoughLines(edges, 1, np.pi/180, 400)
 
# The below for loop runs till r and theta values
# are in the range of the 2d array
for r_theta in lines:
    arr = np.array(r_theta[0], dtype=np.float64)
    r, theta = arr
    # Stores the value of cos(theta) in a
    a = np.cos(theta)
 
    # Stores the value of sin(theta) in b
    b = np.sin(theta)
 
    # x0 stores the value rcos(theta)
    x0 = a*r
 
    # y0 stores the value rsin(theta)
    y0 = b*r
 
    # x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
    x1 = int(x0 + 1000*(-b))
 
    # y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
    y1 = int(y0 + 1000*(a))
 
    # x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
    x2 = int(x0 - 1000*(-b))
 
    # y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
    y2 = int(y0 - 1000*(a))
 
    # cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
    # (0,0,255) denotes the colour of the line to be
    # drawn. In this case, it is red.
    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
 
# All the changes made in the input image are finally
# written on a new image houghlines.jpg
cv2.imwrite('linesDetected.jpg', img)

那么,现在如何从原始图像中减go 这条线(红色高亮部分)呢?

谢谢.

推荐答案

拥有(x1, y1)(x2, y2),您可以将图像分成两部分,如下所示:

img_left = img[0:x1, 0:y1]
img_right = img[0:x2, 0:y2]

然后再加入他们:

final_img = np.concatenate((img_left, img_right), axis=1)

Python-3.x相关问答推荐

Python:字典和列表:在列表字典中搜索子列表的有效方法

检测点坐标 - opencv findContours()

CDKTF ec2 具有特定私有 IP 地址的娱乐

如果集合大于 len(x),则 pandas 在重复的行中拆分集合列

我正在使用 python 线程,当查询 mysql 时,代码似乎在运行并保持在无限循环中,没有返回任何错误

安装没有 sudo 权限的 python3 和 pip3

缺失时推断的数据类可选字段

numpy是如何添加@运算符的?

如何在数据['column']中的'string'等条件下应用pandas

使用正则表达式捕获组解析地址

python total_ordering:为什么使用 __lt__ 和 __eq__ 而不是 __le__?

在数据类中创建类变量的正确方法

python asyncio - 如何等待取消的屏蔽任务?

如何使用 d.items() 更改 for 循环中的所有字典键?

如何找出从哪个模块导入名称?

导入父目录进行简要测试

Selenium Python - 处理没有这样的元素异常

在动态链接库 Anaconda3\Library\bin\mkl_intel_thread.dll 中找不到序数 242

在 Visual Studio Code 中调试 Scrapy 项目

Pylint 中的模块PyQt5.QtWidgets错误中没有名称QApplication