コード例 #1
0
    def houghlinePmethod(self, edges):
        minLineLength = int(self.source_color.shape[0] / 5.0)
        maxLineGap = int(self.source_color.shape[0] / 2.0)
        linesP = cv2.HoughLinesP(edges, 1, np.pi / 180, 95, np.array([]),
                                 minLineLength, maxLineGap)
        print "linesP are:   " + str(linesP)
        for x1, y1, x2, y2 in linesP[0]:
            #cv2.circle(self.source_color, (x1, y1), 6, (0, 255, 255), 1)
            #cv2.circle(self.source_color, (x2, y2), 6, (255, 0, 255), 1)
            cv2.line(self.source_color, (x1, y1), (x2, y2), (0, 0, 255))

        #cv2.imshow("lines", self.source_color)
        #cv2.waitKey(0)

        # Initial Intersection class
        inter = Intersections()
        d = []
        for i in range(0, linesP.shape[1]):
            for j in range(i + 1, linesP.shape[1]):
                line1 = linesP[0][i]
                line2 = linesP[0][j]
                # check the lines as a pair to compute their intersections
                if inter.acceptLinesPPair(line1, line2, np.pi / 20.0):
                    intersection = inter.computeintersectP(line1, line2)
                    print intersection
                    if ((intersection[0] > 0
                         and intersection[0] < self.source_color.shape[1]) and
                        (intersection[1] > 0
                         and intersection[1] < self.source_color.shape[0])):
                        #if ((intersection[0] > 0) and (intersection[1] > 0)):
                        inter.append(intersection[0], intersection[1])
                        cv2.circle(self.source_color,
                                   (intersection[0], intersection[1]), 10,
                                   (0, 255, 0))
                        #cv2.line(self.source_color,(line1[0],line1[1]),(line1[2],line1[3]),(255, 255, 0))
                        #cv2.line(self.source_color,(line2[0],line2[1]),(line2[2],line2[3]),(255, 255, 0))

        cv2.imwrite("images/result_images/Lines_intersection.jpg",
                    self.source_color)
        #cv2.imshow("lines and intersections", self.source_color)
        #cv2.waitKey(0)

        # combination of the extracted intersection with harris corner detetction
        gray = cv2.cvtColor(self.source, cv2.COLOR_BGR2GRAY)
        '''harris corner detector and draw red circle around them'''
        self.features = cv2.goodFeaturesToTrack(gray,
                                                1000,
                                                0.001,
                                                5,
                                                None,
                                                None,
                                                2,
                                                useHarrisDetector=True,
                                                k=0.00009)
        if len(self.features.shape) == 3.0:
            assert (self.features.shape[1:] == (1, 2))
            self.features.shape = (self.features.shape[0], 2)

        for x, y in self.features:
            self.corners += [(x, y)]
            inter.append_features(x, y)
            cv2.circle(self.source_color, (x, y), 8, (255, 255, 255))

    # subpixel accuracy
        '''define the criteria to stop and refine the corners'''
        criteria = (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS, 100,
                    0.03)
        #criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
        cv2.cornerSubPix(gray, self.features, (5, 5), (-1, -1), criteria)

        for x, y in self.features:
            self.refined_corners += [(x, y)]
            inter.append_refined_features(x, y)
            cv2.circle(self.source_color, (x, y), 4, (0, 255, 0))
        '''Now draw them'''
        res = np.hstack((self.corners, self.refined_corners))
        res = np.int0(res)
        self.source_color[res[:, 1], res[:, 0]] = [0, 0, 255]
        self.source_color[res[:, 3], res[:, 2]] = [0, 255, 0]
        #cv2.imwrite(str(self.IMAGE_NAME) + '_subpixel.png', self.im_new)

        cv2.imwrite("images/result_images/Features and subpixel accuracy.jpg",
                    self.source_color)
        #cv2.imshow("Features and subpixel accuracy", self.source_color)
        #cv2.waitKey(0)

        return inter
コード例 #2
0
    def houghlinemethod(self, edges):
        lines = cv2.HoughLines(edges, 0.7, np.pi / 500, 130)
        for rho, theta in lines[0]:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a * rho
            y0 = b * rho
            x1 = int(x0 + 1000 * (-b))
            y1 = int(y0 + 1000 * (a))
            x2 = int(x0 - 1000 * (-b))
            y2 = int(y0 - 1000 * (a))
            cv2.line(self.source_color, (x1, y1), (x2, y2), (0, 255, 255), 1)

        # Initial Intersection class
        inter = Intersections()
        for i in range(0, lines.shape[1]):
            for j in range(i + 1, lines.shape[1]):
                line1 = lines[0][i]
                line2 = lines[0][j]
                # check the lines as a pair to compute their intersections
                if inter.acceptLinePair(line1, line2, np.pi / 32):
                    intersection = inter.computeintersect(line1, line2)
                    #print intersection
                    if (intersection[0] > 0 and intersection[1] > 0):
                        inter.append(intersection[0], intersection[1])
                        cv2.circle(self.source_color,
                                   (intersection[0], intersection[1]), 6,
                                   (0, 255, 255), 2)

        # combination of the extracted intersection with harris corner detetction
        gray = cv2.cvtColor(self.source, cv2.COLOR_BGR2GRAY)
        '''harris corner detector and draw red circle around them'''
        self.features = cv2.goodFeaturesToTrack(gray,
                                                1000,
                                                0.001,
                                                5,
                                                None,
                                                None,
                                                2,
                                                useHarrisDetector=True,
                                                k=0.00009)
        if len(self.features.shape) == 3.0:
            assert (self.features.shape[1:] == (1, 2))
            self.features.shape = (self.features.shape[0], 2)

        for x, y in self.features:
            self.corners += [(x, y)]
            inter.append_features(x, y)
            cv2.circle(self.source_color, (x, y), 8, (255, 255, 255))

        cv2.imwrite("images/result_images/Lines_intersection.jpg",
                    self.source_color)
        #cv2.imshow("lines and intersections", self.source_color)
        #cv2.waitKey(0)

        # subpixel accuracy
        '''define the criteria to stop and refine the corners'''
        criteria = (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS, 100,
                    0.03)
        #criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
        cv2.cornerSubPix(gray, self.features, (5, 5), (-1, -1), criteria)

        for x, y in self.features:
            self.refined_corners += [(x, y)]
            inter.append_refined_features(x, y)
            cv2.circle(self.source_color, (x, y), 4, (0, 255, 0))
        '''Now draw them'''
        res = np.hstack((self.corners, self.refined_corners))
        res = np.int0(res)
        self.source_color[res[:, 1], res[:, 0]] = [0, 0, 255]
        self.source_color[res[:, 3], res[:, 2]] = [0, 255, 0]
        #cv2.imwrite(str(self.IMAGE_NAME) + '_subpixel.png', self.im_new)

        #cv2.imshow("corners", self.source_color)
        #cv2.waitKey(0)

        return inter