line_detector.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:line-follower 作者: iseikr 项目源码 文件源码
def __findLine(self):
        self.__grabImage();

        if(self.currentImage is None):
            #grabbing image failed
            return -2.0

        #Convert to Grayscale
        img = cv2.cvtColor(self.currentImage, cv2.COLOR_BGR2GRAY)

        #Blur to reduce noise
        img = cv2.medianBlur(img,25)

        #Do Thresholding
        h,img = cv2.threshold(img, self.thresh, self.maxValue, cv2.THRESH_BINARY_INV)

        img = cv2.blur(img,(2,2))

        #Make image smaller
        img = cv2.resize(img, (self.horizontalRes, self.verticalRes))
        #org_img = cv2.resize(org_img, (self.horizontalRes, self.verticalRes))

        #Create skeleton
        size = np.size(img)
        skel = np.zeros(img.shape,np.uint8)
        element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
        done = False
        while( not done):
            eroded = cv2.erode(img,element)
            temp = cv2.dilate(eroded,element)
            temp = cv2.subtract(img,temp)
            skel = cv2.bitwise_or(skel,temp)
            img = eroded.copy()
            zeros = size - cv2.countNonZero(img)
            if zeros==size:
                done = True

        #Do Line Detection
        lines = cv2.HoughLinesP(skel,1,np.pi/180,2,
                self.hough_minLineLength,self.hough_maxLineGap)


        #get minimum and maximum x-coordinate from lines
        x_min = self.horizontalRes+1.0
        x_max = -1.0;
    if(lines != None and len(lines[0]) > 0):
        for x1,y1,x2,y2 in lines[0]:
            x_min = min(x_min, x1, x2)
            x_max = max(x_max, x1, x2)
            #cv2.line(org_img,(x1,y1),(x2,y2),(0,255,0),2)

        #write output visualization
        #cv2.imwrite("output-img.png",org_img);

        #find the middle point x of the line and return
        #return -1.0 if no lines found
        if(x_max == -1.0 or x_min == (self.horizontalRes+1.0) ):
            return -1.0 #no line found
        else:
            return (x_min + x_max) / 2.0
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号