gpt4 book ai didi

opencv - 拆分图像中的数字

转载 作者:太空宇宙 更新时间:2023-11-03 23:10:57 24 4
gpt4 key购买 nike

我可以访问很多手工填写的报告。报告中的一列包含时间戳,我想尝试在不手动查看每个报告的情况下进行识别。

我正在玩 split 时间的想法,例如00:30,分为四位数,并通过在 MNIST 上训练的分类器运行这些数字,以识别实际时间戳。

当我在 Photoshop 中手动提取四位数字并通过 MNIST 分类器运行这些数字时,效果非常好。但到目前为止,我还没有弄清楚如何以编程方式将数字序列拆分为单个数字。我尝试在 OpenCV 中使用不同类型的计数结果,但它的工作不是很可靠。

有什么建议吗?

我已经 added a screenshot报告中的一些相关列。

最佳答案

我会做这样的事情(没有代码,只要它只是一个想法,你可以测试它看看是否有效):

  1. 按照 Rick M. 上面的建议,为每组数字提取每个区域。因此,您将在图像形式下有许多 Kl [hour] 矩形。

  2. 对于这些矩形中的每一个,提取(使用 OpenCV 轮廓特征)每个 ROI。如果不需要,请删除 Kl(您知道此 ROI 的尺寸(您可以使用 img.shape 计算)并且它们具有或多或少相同的尺寸)

  3. 使用上面使用的相同脚本提取所有数字。您可以查看我的问题/答案以找到执行此操作的一些代码。在某些情况下,您会遇到下划线问题。在SO上搜索这个,很少有完整的代码解决方案。

  4. 现在,关于分手。我们知道 ROI 的格式是小时,所以 hh:mm(或 4 位数字)。一个简单的(也是非常基本的)拆分字符的解决方案是将你得到的 ROI 分成一半,里面有 2 个数字。这是一个原始解决方案,但在您的情况下应该表现良好,因为附加的数字仅为 2。

  5. 一些数字将输出“缺失部分”。这可以通过使用一些 erosion/dilation/skeletonization 来避免。 .

这里没有字母,只有数字,因此 MNIST 应该可以很好地工作(不完美,请记住这一点)。

在某些情况下,提取数据并不是一项艰巨的任务,但识别数字会让您有点头疼。

我希望我能尽快提供一些代码来显示上述步骤。

编辑 - 代码

这是我编写的一些代码。最终输出是这样的:

output

该代码 100% 与此图像一起工作,因此,如果某些东西不适合您,请检查文件夹/路径/模块安装

希望这对您有所帮助。

import cv2
import numpy as np

# 1 - remove the vertical line on the left

img = cv2.imread('image.jpg', 0)
# gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img, 100, 150, apertureSize=5)

lines = cv2.HoughLines(edges, 1, np.pi / 50, 50)
for rho, theta in lines[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), (255, 255, 255), 10)

cv2.imshow('marked', img)
cv2.waitKey(0)
cv2.imwrite('image.png', img)


# 2 - remove horizontal lines

img = cv2.imread("image.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_orig = cv2.imread("image.png")

img = cv2.bitwise_not(img)
th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -2)
cv2.imshow("th2", th2)
cv2.waitKey(0)
cv2.destroyAllWindows()

horizontal = th2
rows, cols = horizontal.shape

# inverse the image, so that lines are black for masking
horizontal_inv = cv2.bitwise_not(horizontal)
# perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
# reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)
cv2.imshow("masked img", masked_img_inv)
cv2.waitKey(0)
cv2.destroyAllWindows()

horizontalsize = int(cols / 30)
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontalsize, 1))
horizontal = cv2.erode(horizontal, horizontalStructure, (-1, -1))
horizontal = cv2.dilate(horizontal, horizontalStructure, (-1, -1))
cv2.imshow("horizontal", horizontal)
cv2.waitKey(0)
cv2.destroyAllWindows()

# step1
edges = cv2.adaptiveThreshold(horizontal, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, -2)
cv2.imshow("edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

# step2
kernel = np.ones((1, 2), dtype="uint8")
dilated = cv2.dilate(edges, kernel)
cv2.imshow("dilated", dilated)
cv2.waitKey(0)
cv2.destroyAllWindows()

im2, ctrs, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)

# Getting ROI
roi = img[y:y + h, x:x + w]

# show ROI
rect = cv2.rectangle(img_orig, (x, y), (x + w, y + h), (255, 255, 255), -1)

cv2.imshow('areas', rect)
cv2.waitKey(0)

cv2.imwrite('no_lines.png', rect)


# 3 - detect and extract ROI's

image = cv2.imread('no_lines.png')
cv2.imshow('i', image)
cv2.waitKey(0)

# grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
cv2.waitKey(0)

# binary
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

# dilation
kernel = np.ones((8, 45), np.uint8) # values set for this image only - need to change for different images
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)

# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)

# Getting ROI
roi = image[y:y + h, x:x + w]

# show ROI
# cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 255), 1)
# cv2.waitKey(0)

# save only the ROI's which contain a valid information
if h > 20 and w > 75:
cv2.imwrite('roi\\{}.png'.format(i), roi)

cv2.imshow('marked areas', image)
cv2.waitKey(0)

这些是后续步骤:

  1. 了解我写的内容 ;)。这是最重要的一步。

  2. 使用上面的代码片段(尤其是第 3 步),您可以删除提取图像中剩余的 Kl

  3. 为每个图像创建文件夹并提取数字。

  4. 使用 MNIST,识别每个数字。

关于opencv - 拆分图像中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50327815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com