【问题标题】:Converting graphs from a scanned document into data将扫描文档中的图表转换为数据
【发布时间】:2019-10-14 03:02:08
【问题描述】:

我目前正在尝试编写一些可以从书中一些不常见的图表中提取数据的东西。我扫描了这本书的页面,通过使用 opencv,我想从图中检测一些特征,以便将其转换为可用数据。在左图中,我正在寻找“三角形”的高度,在右图中,我正在寻找从中心到虚线与灰色区域相交的点的距离。在这两种情况下,我都想将这些值转换为数字数据以供进一步使用。

我想到的第一件事是检测图表的线条,希望我能以某种方式测量它们的长度或位置。为此,我使用霍夫线变换。下面的 sn-p 代码显示了我已经走了多远。

import numpy as np
import cv2

# Reading the image
img = cv2.imread('test2.jpg')
# Convert the image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Apply edge detection
edges = cv2.Canny(gray,50,150,apertureSize = 3)

# Line detection
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=50,maxLineGap=20)

for line in lines:
    x1,y1,x2,y2 = line[0]
    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('linesDetected.jpg',img)

唯一的问题是这种检测算法根本不准确。至少对我来说不是。并且为了从图表中提取一些数据,线条的检测应该有点准确。他们有什么办法我可以做到这一点吗?还是我检测线条的策略一开始就错了?我是否应该从检测其他内容开始,例如圆圈、对象大小、轮廓或颜色?

【问题讨论】:

  • 图表是圆形的,所以也许首先寻找那个圆圈,而不是线条?圆的各个部分有不同的色调,所以用色调来分割它们?在每个段的中间投影一条线,并找到饱和度变化的位置。 |第二张图表似乎以非常糟糕的分辨率扫描——你能获得更好的图像吗?

标签: python opencv hough-transform


【解决方案1】:

使用颜色分割是将此图转换为数据的一种简单方法。此方法确实需要一些手动注释。分割图形后,计算每种颜色的像素。查看 OpenCV 库中包含的演示文件中的“分水岭”演示:

import numpy as np
import cv2 as cv
from common import Sketcher

class App:
    def __init__(self, fn):
        self.img = cv.imread(fn)
        self.img = cv.resize(self.img, (654,654))
        h, w = self.img.shape[:2]
        self.markers = np.zeros((h, w), np.int32)
        self.markers_vis = self.img.copy()
        self.cur_marker = 1
        self.colors = np.int32( list(np.ndindex(2, 2, 3)) ) * 123
        self.auto_update = True
        self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors)

    def get_colors(self):
        return list(map(int, self.colors[self.cur_marker])), self.cur_marker

    def watershed(self):
        m = self.markers.copy()
        cv.watershed(self.img, m)
        cv.imshow('img', self.img)        
        overlay = self.colors[np.maximum(m, 0)]
        vis = cv.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv.CV_8UC3)
        cv.imshow('overlay', np.array(overlay, np.uint8))
        cv.imwrite('/home/stephen/Desktop/overlay.png', np.array(overlay, np.uint8))
        cv.imshow('watershed', vis)

    def run(self):
        while cv.getWindowProperty('img', 0) != -1 or cv.getWindowProperty('watershed', 0) != -1:
            ch = cv.waitKey(50)
            if ch >= ord('1') and ch <= ord('9'):
                self.cur_marker = ch - ord('0')
                print('marker: ', self.cur_marker)
            if self.sketch.dirty and self.auto_update:
                self.watershed()
                self.sketch.dirty = False
            if ch == 27: break
        cv.destroyAllWindows()


fn = '/home/stephen/Desktop/test.png'
App(cv.samples.findFile(fn)).run() 

输出将是这样的图像:

您可以使用以下代码计算每种颜色的像素数:

# Extract the values from the image
vals = []
img = cv.imread('/home/stephen/Desktop/overlay.png')
# Get the colors in the image
flat = img.reshape(-1, img.shape[-1])
colors = np.unique(flat, axis=0)
# Iterate through the colors (ignore the first and last colors)
for color in colors[1:-1]:
    a,b,c = color
    lower = a-1, b-1, c-1
    upper = a+1,b+1,c+1
    lower = np.array(lower)
    upper = np.array(upper)
    mask = cv.inRange(img, lower, upper)
    vals.append(sum(sum(mask)))
    cv.imshow('mask', mask)
    cv.waitKey(0)
cv.destroyAllWindows()

并使用以下代码打印输出数据:

names = ['alcohol', 'esters', 'biter', 'hoppy', 'acid', 'zoetheid', 'mout']
print(list(zip(names, vals)))

输出是:

[('alcohol', 22118), ('esters', 26000), ('biter', 16245), ('hoppy', 21170), ('acid', 19156), ('zoetheid', 11090), ('mout', 7167)]

【讨论】:

  • 这似乎是朝着正确方向迈出的一步。唯一的问题是我有一本书,里面全是这些图表,所以我必须找到一种方法来自动化这个过程。
  • @FrederikVanclooster 如果您想自动提取数据,如果您能证明对多个图表进行高分辨率扫描,将会很有帮助。
  • 目前没有。目的是通过使用上图中提供的“测试扫描”来找到策略。因此,当我找到一种工作方法时,我可以以良好的分辨率扫描整本书。问题是我家里的打印机产生的扫描结果比上面的扫描结果更差,所以我必须在接下来的几天里去某种复印中心......
  • 顺便问一下,“from common import Sketches”行指的是什么?我在哪里可以找到这个 Sketcher 课程?
  • 您可以在 OpenCV 示例 (/opencv/samples/python/common.py) 中找到 Sketcher 类。它有鼠标回调代码之类的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2018-01-10
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
相关资源
最近更新 更多