Ejemplo n.º 1
0
    def contourmethod(self, edges):
        hull = None
        contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL,
                                               cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(self.source_color, contours, -1, (0, 0, 255), 1)
        #cv2.imshow("contours", self.source_color)
        print hierarchy
        c = max(contours, key=cv2.contourArea)
        cv2.drawContours(self.source_color, c, -1, (0, 0, 0), 3)
        #cv2.imshow("max_contour", self.source_color)
        cv2.waitKey(0)
        for i, cnt in enumerate(contours):
            if hierarchy[
                    0, i,
                    3] == -1 and cv2.contourArea(cnt) >= cv2.contourArea(c):
                hull = cv2.convexHull(cnt, returnPoints=True)
                break
        print hull
        cv2.drawContours(self.source_color, [hull], -1, (255, 255, 0), 1)
        #cv2.imshow("hull", self.source_color)
        cv2.waitKey(0)
        length = len(hull)

        M = cv2.moments(hull)
        if M['m00'] == 0:
            M['m00'] = 0.01
        self.cx = int(M['m10'] / M['m00'])
        self.cy = int(M['m01'] / M['m00'])
        center = [self.cx, self.cy]
        #cv2.circle(self.source_color, (self.cx, self.cy), 5, (0, 255, 255), 2)

        print length
        coord = Coordinates()
        for i in xrange(0, length):
            if (i + 3) < length:
                [x, y] = coord.intersection(
                    (hull[i][0][0], hull[i][0][1]),
                    (hull[i + 1][0][0], hull[i + 1][0][1]),
                    (hull[i + 2][0][0], hull[i + 2][0][1]),
                    (hull[i + 3][0][0], hull[i + 3][0][1]))
                coord.append(x, y)
                cv2.circle(self.source_color, (x, y), 6, (255, 255, 255), 2)
        #cv2.imshow("coordinates", self.source_color)
        #cv2.waitKey(0)

        return coord, center
    def get_coordinates_houghP(self, edges):
        constant = 100
        minLineLength = 10
        maxLineGap = 5
        coord = Coordinates()
        lines = cv2.HoughLinesP(edges, 1, np.pi / 180, constant, minLineLength, maxLineGap)
        print lines, type(lines)
        points = []
        for x1, y1, x2, y2 in lines[0]:
            points.append([x1, y1])
            points.append([x2, y2])

        [x, y] = coord.intersection(points[0], points[1], points[2], points[3])
        coord.append(x, y)
        # cv2.line(edges, (x1, y1), (x2, y2), (0, 255, 0), 2)
        # cv2.imshow("line", edges)
        # cv2.waitKey(0)
        return coord
Ejemplo n.º 3
0
    def get_coordinates_houghP(self, edges):
        constant = 100
        minLineLength = 10
        maxLineGap = 5
        coord = Coordinates()
        lines = cv2.HoughLinesP(edges, 1, np.pi / 180, constant, minLineLength,
                                maxLineGap)
        print lines, type(lines)
        points = []
        for x1, y1, x2, y2 in lines[0]:
            points.append([x1, y1])
            points.append([x2, y2])

        [x, y] = coord.intersection(points[0], points[1], points[2], points[3])
        coord.append(x, y)
        # cv2.line(edges, (x1, y1), (x2, y2), (0, 255, 0), 2)
        # cv2.imshow("line", edges)
        # cv2.waitKey(0)
        return coord
Ejemplo n.º 4
0
    def contourmethod(self, edges):
        hull = None
        contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL,
                                               cv2.CHAIN_APPROX_SIMPLE)
        print hierarchy
        for i, cnt in enumerate(contours):
            if hierarchy[0, i, 3] == -1 and cv2.contourArea(cnt) > 5000:
                hull = cv2.convexHull(cnt, returnPoints=True)
                break
        print hull
        length = len(hull)

        print length
        coord = Coordinates()
        for i in xrange(0, length):
            if (i + 3) < length:
                [x, y] = coord.intersection(
                    (hull[i][0][0], hull[i][0][1]),
                    (hull[i + 1][0][0], hull[i + 1][0][1]),
                    (hull[i + 2][0][0], hull[i + 2][0][1]),
                    (hull[i + 3][0][0], hull[i + 3][0][1]))
                coord.append(x, y)
        return coord
    def contourmethod(self, edges):
        hull = None
        contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        print hierarchy
        for i, cnt in enumerate(contours):
            if hierarchy[0, i, 3] == -1 and cv2.contourArea(cnt) > 5000:
                hull = cv2.convexHull(cnt, returnPoints=True)
                break
        print hull
        length = len(hull)

        print length
        coord = Coordinates()
        for i in xrange(0, length):
            if (i + 3) < length:
                [x, y] = coord.intersection(
                    (hull[i][0][0], hull[i][0][1]),
                    (hull[i + 1][0][0], hull[i + 1][0][1]),
                    (hull[i + 2][0][0], hull[i + 2][0][1]),
                    (hull[i + 3][0][0], hull[i + 3][0][1]),
                )
                coord.append(x, y)
        return coord