So I have a dataset of about 3000 images and about 7% of them is poorly processed. I'm using OpenCV and Python and my algorithm is based on preprocessing with morphological erosion, Canny edge detection and Hough line transform. On some images horizon line is not detected because Canny returns a line with a gap or a horizon which isn't a straight line. Is there a way to fix it? Perhaps adding a tolerance to the angle or something like that? Probabilistic Hough transform doesn't help either.
Here is an original image and successfully processed one:
And here is a troubled pair:
import cv2
import numpy as np
img = cv2.imread(conf.data_folder + 'frame0134.jpg',0)
kernel = np.ones((9,9),np.uint8)
erosion = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
edges = cv2.Canny(erosion,50,150)
lines = cv2.HoughLines(edges,1,np.pi/180,80)
for ro, theta in lines[0]:
if theta == 0.0:
x1 = int(ro)
x2 = int(ro)
y1 = 0
y2 = img.shape[0]
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cropped = img[:,0:x1]
else:
a = -1/np.tan(theta)
b = ro/np.sin(theta)
y1 = 0 x1 = max(0, int( -b/a))
y2 = img.shape[0]
x2 = min(img.shape[1], int((y2-b)/a))
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
No comments:
Post a Comment