python - Detecting vertical lines using Hough transforms in opencv -
i'm trying remove square boxes(vertical , horizontal lines) using hough transform in opencv (python). problem none of vertical lines being detected. i've tried looking through contours , hierarchy there many contours in image , i'm confused how use them.
after looking through related posts, i've played threshold , rho parameters didn't help. i've attached code more details. why hough transform not find vertical lines in image?. suggestions in solving task welcome. thanks.
import cv2 import numpy 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) x1,y1,x2,y2 in lines[0]: cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2) cv2.imwrite('probhough.jpg',img)
to honest, rather looking lines, i'd instead white boxes.
preparation
import cv2 import numpy np
load image
img = cv2.imread("digitbox.jpg", 0)
binarize it, both boxes , digits black, rest white
_, thresh = cv2.threshold(img, 200, 255, cv2.thresh_binary) cv2.imwrite('digitbox_step1.png', thresh)
find contours. in example image, it's fine external contours.
_, contours, hierarchy = cv2.findcontours(thresh, cv2.retr_external, cv2.chain_approx_simple)
process contours, filtering out small area. find convex hull of each contour, create mask of areas outside contour. store bounding boxes of each found contour, sorted x coordinate.
mask = np.ones_like(img) * 255 boxes = [] 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)
dilate mask (to shrink black parts), clip off remains the gray frames.
mask = cv2.dilate(mask, np.ones((5,5),np.uint8)) cv2.imwrite('digitbox_step3.png', mask)
fill masked pixels white, erase frames.
img[mask != 0] = 255 cv2.imwrite('digitbox_step4.png', img)
process digits desire -- i'll draw bounding boxes.
result = cv2.cvtcolor(img, cv2.color_gray2bgr) 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)
the whole script in 1 piece:
import cv2 import numpy 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 = [] 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) 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)
Comments
Post a Comment