我想检测扫描文档中的分割线,而不是使用cv2.Canny
,而是使用cv2.threshold
来获得一个非常干净的预处理图像。但是,我的cv2.HoughLines
参数可能不正确,并且在最终输出中出现了混乱。而线条并没有以设定的颜色出现。你知道吗
我的代码是:
import cv2
import numpy as np
from matplotlib import pyplot as plt
## (1) read
img = cv2.imread("q11.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.figure(figsize=(17,17))
plt.imshow(gray,cmap='gray')
## (2) threshold
th, threshed = cv2.threshold(gray, 200, 20, cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
plt.figure(figsize=(15,15))
plt.imshow(threshed,cmap='gray')
## (3) HoughLines
lines = cv2.HoughLines(threshed,rho=1,theta=np.pi/180,threshold = 800)
for i in range(len(lines)):
for rho,theta in lines[i]:
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(threshed,(x1,y1),(x2,y2),(0,0,255),2)
plt.figure(figsize=(10, 10))
plt.subplot(111),plt.imshow(threshed)
plt.title('hough'), plt.xticks([]), plt.yticks([])
经过cv2.threshold
和cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU
我得到:
我得到的是:
这里有一个简单的方法
门槛
下一步,我们扩展以增强轮廓,然后使用^{} 来检测线条。你提到
这是因为您试图在二值图像(阈值图像)上绘制线。因为它只有一个通道,所以像素只有白色或黑色。因此它不会显示颜色。你必须把它画在彩色图像上
相关问题 更多 >
编程相关推荐