【发布时间】:2018-06-19 10:27:23
【问题描述】:
【问题讨论】:
-
我使用 Contours 和精明的边缘检测,但无法正确获得结果
标签: python python-3.x opencv ocr
【问题讨论】:
标签: python python-3.x opencv ocr
使用膨胀和制作轮廓。只需调整各种功能的参数即可获得所需的输出。
import matplotlib.pyplot as plt
import cv2
import numpy as np
img = cv2.imread('pan2.jpg')
img1 = cv2.GaussianBlur(img,(5,5),0)
img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
f1 = cv2.cvtColor(img1,cv2.COLOR_RGB2GRAY)
f1 = 255 - cv2.threshold(f1, 0,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
fgdilated = cv2.dilate(f1, kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)) , iterations = 1)
fgclosing = cv2.morphologyEx(fgdilated, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2)))
plt.imshow(fgclosing)
plt.show()
img2, contours, hierarchy = cv2.findContours(fgdilated, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
#print(cnt)
#print(cv2.contourArea(cnt))
if cv2.contourArea(cnt) > 200:
#hull = cv2.convexHull(cnt)
#print(hull)
#cv2.drawContours(img, [hull], -1, (255, 255, 255), 1)
(x,y,w,h) = cv2.boundingRect(cnt)
if h >7:
cv2.rectangle(img, (x,y), (x+w,y+h), (255, 0, 255), 1)
plt.imshow(img)
plt.show()
输入:
这是我使用代码得到的二进制图像。
输出:
【讨论】:
首先将GaussianBlur 应用于您的图像,例如。 G。内核大小为 3。
使用Canny 找到具有适当值的边缘后(由于您拍摄图像 [扫描] 的条件差别不大,您应该能够在 canny 中找到这样的最小值/最大值),您可以找到边缘点使用findContours
使用minAreaRect 拟合矩形。然后裁剪这部分图像。
【讨论】: