我正试图在opencv(Python)中使用Hough变换删除方框(垂直和水平线).问题是没有检测到垂直线.我已经尝试了查看轮廓和层次结构,但是这个图像中有太多的轮廓,我很困惑如何使用它们.
在查看相关帖子后,我玩了阈值和rho参数,但这没有帮助.我已附上代码以获取更多详细信息.为什么Hough变换找不到图像中的垂直线?欢迎任何解决此任务的建议.谢谢.
import cv2
import numpy as np
import pdb
img = cv2.imread('/home/user/Downloads/cropped/robust_blaze_cpp-300-0000046A-02-HW.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 140, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,0,255), 2)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 5
maxLineGap = 100
lines = cv2.HoughLinesP(edges,rho=1,theta=np.pi/180,threshold=100,minLineLength=minLineLength,maxLineGap=maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imwrite('probHough.jpg',img)
Run Code Online (Sandbox Code Playgroud)
Dan*_*šek 14
说实话,我不是寻找线条,而是寻找白色的盒子.
制备
import cv2
import numpy as np
Run Code Online (Sandbox Code Playgroud)加载图像
img = cv2.imread("digitbox.jpg", 0)
Run Code Online (Sandbox Code Playgroud)将其二值化,使盒子和数字都是黑色,休息是白色
_, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
cv2.imwrite('digitbox_step1.png', thresh)
Run Code Online (Sandbox Code Playgroud)
查找轮廓.在此示例图像中,只需查找外部轮廓即可.
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)处理轮廓,过滤掉任何面积太小的轮廓.找到每个轮廓的凸包,创建轮廓外的所有区域的蒙版.存储每个找到的轮廓的边界框,按x坐标排序.
mask = np.ones_like(img) * 255
boxes = []
for contour in contours:
if cv2.contourArea(contour) > 100:
hull = cv2.convexHull(contour)
cv2.drawContours(mask, [hull], -1, 0, -1)
x,y,w,h = cv2.boundingRect(contour)
boxes.append((x,y,w,h))
boxes = sorted(boxes, key=lambda box: box[0])
cv2.imwrite('digitbox_step2.png', mask)
Run Code Online (Sandbox Code Playgroud)
扩大面罩(收缩黑色部分),剪掉任何残留的灰色框架.
mask = cv2.dilate(mask, np.ones((5,5),np.uint8))
cv2.imwrite('digitbox_step3.png', mask)
Run Code Online (Sandbox Code Playgroud)
用白色填充所有蒙版像素,以擦除帧.
img[mask != 0] = 255
cv2.imwrite('digitbox_step4.png', img)
Run Code Online (Sandbox Code Playgroud)
根据需要处理数字 - 我只需绘制边界框.
result = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
for n,box in enumerate(boxes):
x,y,w,h = box
cv2.rectangle(result,(x,y),(x+w,y+h),(255,0,0),2)
cv2.putText(result, str(n),(x+5,y+17), cv2.FONT_HERSHEY_SIMPLEX, 0.6,(255,0,0),2,cv2.LINE_AA)
cv2.imwrite('digitbox_step5.png', result)
Run Code Online (Sandbox Code Playgroud)
整个剧本是一体的:
import cv2
import numpy as np
img = cv2.imread("digitbox.jpg", 0)
_, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mask = np.ones_like(img) * 255
boxes = []
for contour in contours:
if cv2.contourArea(contour) > 100:
hull = cv2.convexHull(contour)
cv2.drawContours(mask, [hull], -1, 0, -1)
x,y,w,h = cv2.boundingRect(contour)
boxes.append((x,y,w,h))
boxes = sorted(boxes, key=lambda box: box[0])
mask = cv2.dilate(mask, np.ones((5,5),np.uint8))
img[mask != 0] = 255
result = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
for n,box in enumerate(boxes):
x,y,w,h = box
cv2.rectangle(result,(x,y),(x+w,y+h),(255,0,0),2)
cv2.putText(result, str(n),(x+5,y+17), cv2.FONT_HERSHEY_SIMPLEX, 0.6,(255,0,0),2,cv2.LINE_AA)
cv2.imwrite('digitbox_result.png', result)
Run Code Online (Sandbox Code Playgroud)