Beispiel #1
0
def findImageContour(img, frame):
    storage = cv.CreateMemStorage()
    cont = cv.FindContours(img, storage, cv.CV_RETR_EXTERNAL,
                           cv.CV_CHAIN_APPROX_NONE, (0, 0))
    max_center = [None, 0]
    for c in contour_iterator(cont):
        # Number of points must be more than or equal to 6 for cv.FitEllipse2
        # Use to set minimum size of object to be tracked.
        if len(c) >= 60:
            # Copy the contour into an array of (x,y)s
            PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2)
            for (i, (x, y)) in enumerate(c):
                PointArray2D32f[0, i] = (x, y)
                # Fits ellipse to current contour.
                (center, size, angle) = cv.FitEllipse2(PointArray2D32f)
                # Only consider location of biggest contour  -- adapt for multiple object tracking
            if size > max_center[1]:
                max_center[0] = center
                max_center[1] = size
                angle = angle

            if True:
                # Draw the current contour in gray
                gray = cv.CV_RGB(255, 255, 255)
                cv.DrawContours(img, c, gray, gray, 0, 1, 8, (0, 0))

    if max_center[1] > 0:
        # Convert ellipse data from float to integer representation.
        center = (cv.Round(max_center[0][0]), cv.Round(max_center[0][1]))
        size = (cv.Round(max_center[1][0] * 0.5),
                cv.Round(max_center[1][1] * 0.5))
        color = cv.CV_RGB(255, 0, 0)

        cv.Ellipse(frame, center, size, angle, 0, 360, color, 3, cv.CV_AA, 0)
    def draw_face(self, frame):
        x, y, w, h = self.face

        # Center of the face
        x += w / 2
        y += h / 2

        # Slightly narrow the elipse to fit most faces better
        w *= Annotator.HEAD_WIDTH_SCALE

        c = Annotator.COLOUR_FACE

        cv.Ellipse(frame, (int(x), int(y)), (int(w / 2), int(h / 2)), 0, 0,
                   360, c, Annotator.THIN)
    def draw_fft(self, frame, fft_data, min_bpm, max_bpm):
        w = frame.width
        h = int(frame.height * Annotator.FFT_HEIGHT)
        x = 0
        y = frame.height

        max_magnitude = max(d[1][0] for d in fft_data)

        def get_position(i):
            point_x = int(
                w *
                (float(fft_data[i][0] - min_bpm) / float(max_bpm - min_bpm)))
            point_y = int(y - ((h * fft_data[i][1][0]) / max_magnitude))
            return point_x, point_y

        line = [get_position(i) for i in range(len(fft_data))]

        cv.PolyLine(frame, [line], False, self.get_colour()[0], 3)

        # Label the largest bin
        max_bin = max(range(len(fft_data)), key=(lambda i: fft_data[i][1][0]))

        x, y = get_position(max_bin)
        c = self.get_colour()
        text = "%0.1f" % fft_data[max_bin][0]

        cv.PutText(frame, text, (x, y), self.small_font_outline, c[1])
        cv.PutText(frame, text, (x, y), self.small_font, c[0])

        # Pulse ring
        r = Annotator.SMALL_PULSE_SIZE
        phase = int(
            ((fft_data[max_bin][1][1] % (2 * numpy.pi)) / numpy.pi) * 180)
        cv.Ellipse(frame, (int(x - (r * 1.5)), int(y - r)), (int(r), int(r)),
                   0, 90, 90 - phase, c[1], Annotator.THIN + Annotator.BORDER)
        cv.Ellipse(frame, (int(x - (r * 1.5)), int(y - r)), (int(r), int(r)),
                   0, 90, 90 - phase, c[0], Annotator.THIN)
Beispiel #4
0
    def process_image(self, slider_pos):
        """
        This function finds contours, draws them and their approximation by ellipses.
        """
        stor = cv.CreateMemStorage()

        # Create the destination images
        image02 = cv.CloneImage(self.source_image)
        cv.Zero(image02)
        image04 = cv.CreateImage(cv.GetSize(self.source_image),
                                 cv.IPL_DEPTH_8U, 3)
        cv.Zero(image04)

        # Threshold the source image. This needful for cv.FindContours().
        cv.Threshold(self.source_image, image02, slider_pos, 255,
                     cv.CV_THRESH_BINARY)

        # Find all contours.
        cont = cv.FindContours(image02, stor, cv.CV_RETR_LIST,
                               cv.CV_CHAIN_APPROX_NONE, (0, 0))

        for c in contour_iterator(cont):
            # Number of points must be more than or equal to 6 for cv.FitEllipse2
            if len(c) >= 6:
                # Copy the contour into an array of (x,y)s
                PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2)
                for (i, (x, y)) in enumerate(c):
                    PointArray2D32f[0, i] = (x, y)

                # Draw the current contour in gray
                gray = cv.CV_RGB(100, 100, 100)
                cv.DrawContours(image04, c, gray, gray, 0, 1, 8, (0, 0))

                # Fits ellipse to current contour.
                (center, size, angle) = cv.FitEllipse2(PointArray2D32f)

                # Convert ellipse data from float to integer representation.
                center = (cv.Round(center[0]), cv.Round(center[1]))
                size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5))

                # Draw ellipse in random color
                color = cv.CV_RGB(random.randrange(256), random.randrange(256),
                                  random.randrange(256))
                cv.Ellipse(image04, center, size, angle, 0, 360, color, 2,
                           cv.CV_AA, 0)

        # Show image. HighGUI use.
        cv.ShowImage("Result", image04)
    def process_image(self, slider_pos):
        global cimg, source_image1, ellipse_size, maxf, maxs, eoc, lastcx, lastcy, lastr
        """
        This function finds contours, draws them and their approximation by ellipses.
        """
        stor = cv.CreateMemStorage()

        # Create the destination images
        cimg = cv.CloneImage(self.source_image)
        cv.Zero(cimg)
        image02 = cv.CloneImage(self.source_image)
        cv.Zero(image02)
        image04 = cv.CreateImage(cv.GetSize(self.source_image),
                                 cv.IPL_DEPTH_8U, 3)
        cv.Zero(image04)

        # Threshold the source image. This needful for cv.FindContours().
        cv.Threshold(self.source_image, image02, slider_pos, 255,
                     cv.CV_THRESH_BINARY)

        # Find all contours.
        cont = cv.FindContours(image02, stor, cv.CV_RETR_LIST,
                               cv.CV_CHAIN_APPROX_NONE, (0, 0))

        maxf = 0
        maxs = 0
        size1 = 0

        for c in contour_iterator(cont):
            if len(c) > ellipse_size:
                PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2)
                for (i, (x, y)) in enumerate(c):
                    PointArray2D32f[0, i] = (x, y)

                # Draw the current contour in gray
                gray = cv.CV_RGB(100, 100, 100)
                cv.DrawContours(image04, c, gray, gray, 0, 1, 8, (0, 0))

                if iter == 0:
                    strng = segF + '/' + 'contour1.png'
                    cv.SaveImage(strng, image04)
                color = (255, 255, 255)

                (center, size, angle) = cv.FitEllipse2(PointArray2D32f)

                # Convert ellipse data from float to integer representation.
                center = (cv.Round(center[0]), cv.Round(center[1]))
                size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5))

                if iter == 1:
                    if size[0] > size[1]:
                        size2 = size[0]
                    else:
                        size2 = size[1]

                    if size2 > size1:
                        size1 = size2
                        size3 = size

                # Fits ellipse to current contour.
                if eoc == 0 and iter == 2:
                    rand_val = abs((lastr - ((size[0] + size[1]) / 2)))
                    if rand_val > 20 and float(max(size[0], size[1])) / float(
                            min(size[0], size[1])) < 1.5:
                        lastcx = center[0]
                        lastcy = center[1]
                        lastr = (size[0] + size[1]) / 2

                    if rand_val > 20 and float(max(size[0], size[1])) / float(
                            min(size[0], size[1])) < 1.4:
                        cv.Ellipse(cimg, center, size, angle, 0, 360, color, 2,
                                   cv.CV_AA, 0)
                        cv.Ellipse(source_image1, center, size, angle, 0, 360,
                                   color, 2, cv.CV_AA, 0)

                elif eoc == 1 and iter == 2:
                    (int, cntr, rad) = cv.MinEnclosingCircle(PointArray2D32f)
                    cntr = (cv.Round(cntr[0]), cv.Round(cntr[1]))
                    rad = (cv.Round(rad))
                    if maxf == 0 and maxs == 0:
                        cv.Circle(cimg, cntr, rad, color, 1, cv.CV_AA, shift=0)
                        cv.Circle(source_image1,
                                  cntr,
                                  rad,
                                  color,
                                  2,
                                  cv.CV_AA,
                                  shift=0)
                        maxf = rad
                    elif (maxf > 0 and maxs == 0) and abs(rad - maxf) > 30:
                        cv.Circle(cimg, cntr, rad, color, 2, cv.CV_AA, shift=0)
                        cv.Circle(source_image1,
                                  cntr,
                                  rad,
                                  color,
                                  2,
                                  cv.CV_AA,
                                  shift=0)
                        maxs = len(c)
        if iter == 1:
            temp3 = 2 * abs(size3[1] - size3[0])
            if (temp3 > 40):
                eoc = 1
Beispiel #6
0

if __name__ == '__main__':

    # create the image where we want to display results
    image = cv.CreateImage((_SIZE, _SIZE), 8, 1)

    # start with an empty image
    cv.SetZero(image)

    # draw the original picture
    for i in range(6):
        dx = (i % 2) * 250 - 30
        dy = (i / 2) * 150

        cv.Ellipse(image, (dx + 150, dy + 100), (100, 70), 0, 0, 360, _white,
                   -1, 8, 0)
        cv.Ellipse(image, (dx + 115, dy + 70), (30, 20), 0, 0, 360, _black, -1,
                   8, 0)
        cv.Ellipse(image, (dx + 185, dy + 70), (30, 20), 0, 0, 360, _black, -1,
                   8, 0)
        cv.Ellipse(image, (dx + 115, dy + 70), (15, 15), 0, 0, 360, _white, -1,
                   8, 0)
        cv.Ellipse(image, (dx + 185, dy + 70), (15, 15), 0, 0, 360, _white, -1,
                   8, 0)
        cv.Ellipse(image, (dx + 115, dy + 70), (5, 5), 0, 0, 360, _black, -1,
                   8, 0)
        cv.Ellipse(image, (dx + 185, dy + 70), (5, 5), 0, 0, 360, _black, -1,
                   8, 0)
        cv.Ellipse(image, (dx + 150, dy + 100), (10, 5), 0, 0, 360, _black, -1,
                   8, 0)
        cv.Ellipse(image, (dx + 150, dy + 150), (40, 10), 0, 0, 360, _black,
Beispiel #7
0
def ellipsedraw():
    cv.Ellipse(img, (50, 150), (40, 80), 100.0, 0.0, 360.0, (0, 0, 0), 8, 2)
    display(img, "Destination")
    cv.WaitKey(0)
               random.randrange(-height, 2 * height))
        pt2 = (random.randrange(-width, 2 * width),
               random.randrange(-height, 2 * height))
        cv.Rectangle(image, pt1, pt2, random_color(random),
                     random.randrange(-1, 9), line_type, 0)

        cv.ShowImage(window_name, image)
        cv.WaitKey(delay)

    # draw some ellipes
    for i in range(number):
        pt1 = (random.randrange(-width, 2 * width),
               random.randrange(-height, 2 * height))
        sz = (random.randrange(0, 200), random.randrange(0, 200))
        angle = random.randrange(0, 1000) * 0.180
        cv.Ellipse(image, pt1, sz, angle, angle - 100, angle + 200,
                   random_color(random), random.randrange(-1, 9), line_type, 0)

        cv.ShowImage(window_name, image)
        cv.WaitKey(delay)

    # init the list of polylines
    nb_polylines = 2
    polylines_size = 3
    pt = [
        0,
    ] * nb_polylines
    for a in range(nb_polylines):
        pt[a] = [
            0,
        ] * polylines_size