0

我正在尝试在 Raspberry 上制作车道保持算法并使用 Python 和 OpenCV。我使用以下管道 Image(RGB)(resolution 320x240) -> Image (Gray) -> Region Growing algorithm -> Canny Edges -> Cropping the ROI -> HoughLinesP -> 根据线条的斜率计算转向角。

我最近刚刚将区域增长算法添加到管道中,从那时起,单个图像的处理时间上升到 4 秒,汽车只是等待指令并且看起来非常糟糕的时间让汽车变得滞后。但是将算法添加到管道中会使汽车做出正确的决定,因此我无法删除它。

我怎样才能使处理速度更快?

    def get_gray_diff(self, img, currentPoint, tmpPoint):
        aux1 = int(img[currentPoint.x, currentPoint.y])
        aux2 = int(img[tmpPoint.x, tmpPoint.y])
        aux3 = abs(aux1 - aux2)
        return aux3

    def select_connects(self, p):
        if p != 0:
            connects = [Point(-1, -1), Point(0, -1), Point(1, -1), Point(1, 0), Point(1, 1),
                        Point(0, 1), Point(-1, 1), Point(-1, 0)]
        else:
            connects = [Point(0, -1),  Point(1, 0), Point(0, 1), Point(-1, 0)]
        return connects


    def region_grow(self, frame):
        seeds = [Point(230, 160)]
        thresh = 6
        p = 1
        height, weight = frame.shape
        seedMark = np.zeros(frame.shape)
        seedList = []
        for seed in seeds:
            seedList.append(seed)
        label = 1
        connects = self.select_connects(p)
        while len(seedList)>0:
            currentPoint = seedList.pop(0)
            seedMark[currentPoint.x, currentPoint.y] = label
            for i in range(8):
                tmpX = currentPoint.x + connects[i].x
                tmpY = currentPoint.y + connects[i].y
                if tmpX < 0 or tmpY < 0 or tmpX >= height or tmpY >= weight:
                    continue
                grayDiff = self.get_gray_diff(frame, currentPoint, Point(tmpX, tmpY))
                if grayDiff < thresh and seedMark[tmpX, tmpY] == 0:
                    seedMark[tmpX, tmpY] = label
                    seedList.append(Point(tmpX, tmpY))
        return seedMark

4

0 回答 0