【发布时间】:2020-08-17 06:52:32
【问题描述】:
我正在尝试使用 openCV 和 python 来提取图像的细节,然后将其存储在 csv 中。 由于从文本中检索数据会提高 OCR 的准确性,因此我正在尝试对图像进行预处理并生成鸟瞰图。 图像有很多噪点,背景与感兴趣区域的颜色相似。
源图片
方法 1) 我使用高斯模糊和自适应阈值化来消除一些噪音。 其次是形态学变换,得到一个公平的二值图像。 然后我使用外部层次结构搜索该区域的轮廓,然后根据该区域进行排序。 此外,在卡边缘上产生的轮廓是开放的,因此按区域排序并没有像我预期的那样工作。 但我想不出想要的输出。
def pre_process_image(img, skip_dilate=False):
proc = cv2.GaussianBlur(img.copy(), (9, 9), 0)
# ret, proc = cv2.threshold(proc,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# edged = cv2.Canny(proc, 100, 200)
proc = cv2.adaptiveThreshold(proc, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
if not skip_dilate:
kernel = np.array([[0., 1., 1.], [1., 1., 1.], [1., 1., 0.]], np.uint8)
proc = cv2.dilate(proc, kernel)
# proc = cv2.erode(proc.copy(), cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
return proc
processed = pre_process_image(res.copy())
contours = cv2.findContours(processed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Find contours
contours = imutils.grab_contours(contours)
contours = sorted(contours, key = cv2.contourArea, reverse = True)
max_len = 0
for cnt in contours:
if(cv2.arcLength(cnt, False) > max_len):
max_len = cv2.arcLength(cnt, False)
connt = cnt
p = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
external_only = cv2.drawContours(p.copy(), connt, -1, (255, 0, 0), 2)
方法 2) HoughTransform 在基本预处理后检测边缘
blur = cv2.GaussianBlur(img, (9, 9), 0)
# res = cv2.equalizeHist(blur)
# res2 = np.hstack((gray, tut))
# # Apply edge detection method on the image
edges = cv2.Canny(blur,50,150,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180, 50)
for r,theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*r
y0 = b*r
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),4)
如果有人能指出我的错误并提供一种更有效的方法来获得良好的结果,那将非常有帮助。我是图像处理的初学者,所以可能不了解大多数理论,但一个很好的工作方向是非常感激。 蒂亚!
【问题讨论】:
标签: python-3.x opencv image-processing computer-vision