0

测试图像 - Vector 的眼睛视图

我一直在努力开发一个让 anki Vector 机器人跟随地面线的程序。我将问题归结为 HoughLinesP 不会检测到两条简单的线(参见代码生成的 edges.jpg)。我已将程序简化为基础,以便提供评论。欢迎提出任何建议。是的,我读过类似的帖子,但它们似乎没有帮助。

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)
4

0 回答 0