I've been struggling with a program to get an anki Vector robot to follow lines on the ground. I've got the problem down to the fact that HoughLinesP won't detect two simple lines (see edges.jpg generated by code). I've cut the program down to basics so as to offer it for comment. Any suggestions most welcome. And yes I've read the similar posts but they don't seem to help.
import cv2
import os
import numpy as np
import time
dev = 1
img = cv2.imread('temp.png')
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(grey,50,150,apertureSize = 3)
blank = np.zeros_like(grey)
maskArea = np.array([[(150, 250), (150,160), (450, 160), (450, 250)]], dtype=np.int32)
mask = cv2.fillPoly(blank, maskArea, 255)
maskedImage = cv2.bitwise_and(edges, mask)
cv2.imwrite('maskedImage.jpg',maskedImage) #save masked edges for diag
while (True):
lines = cv2.HoughLinesP(maskedImage,rho=6,
theta=np.pi/180,
threshold=100,
lines=np.array([]),
minLineLength=10,
maxLineGap=40)
print("============")
if (lines is not None):
radAngle=0
for i in range(0, len(lines)):
for x1,y1,x2,y2 in lines[i]:
if (abs(y2-y1) > 10): #select verticals
#print("====",x1,y1,x2,y2)
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2) #add line to image
radAngle += np.arctan2(x2 - x1, y2 - y1)
#if (len(lines)>2): radAngle = radAngle/len(lines)
degAngle = int(np.rad2deg(radAngle))
if (y1>y2): degAngle -=180
print("degrees = ",degAngle)
cv2.imwrite('houghlines.jpg',img) #save image to disc
disp=cv2.imread('houghlines.jpg')
cv2.imshow('hough_lines', disp) #display overlays on laptop
cv2.waitKey(100) #refresh display
else:
print("None")
time.sleep(1)