Beispiel #1
0
	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 #2
0
 def draw_crosshair(self):
     hcenter = self.image.width / 2 + self.yoffset
     vcenter = self.image.height / 2 + self.xoffset
     #draw horizontal Line
     cv.Line(self.image, (0, vcenter), (self.image.width, vcenter),
             cv.RGB(255, 0, 0), self.stroke, cv.CV_AA, 0)
     #draw vertical Line
     cv.Line(self.image, (hcenter, 30), (hcenter, self.image.height),
             cv.RGB(255, 0, 0), self.stroke, cv.CV_AA, 0)
     #draw upper half-circles
     for i in range(0, 4):
         cv.Ellipse(self.image, (hcenter, vcenter - self.stretch),
                    (self.dia * i, self.dia * i), 0, 180, 360,
                    cv.RGB(255, 0, 0), self.stroke, cv.CV_AA, 0)
     #draw connecting lines id streched
     if self.stretch > 0:
         for i in range(1, 4):
             cv.Line(self.image,
                     (hcenter + self.dia * i, vcenter - self.stretch),
                     (hcenter + self.dia * i, vcenter + self.stretch),
                     cv.RGB(255, 0, 0), self.stroke, cv.CV_AA, 0)
             cv.Line(self.image,
                     (hcenter - self.dia * i, vcenter - self.stretch),
                     (hcenter - self.dia * i, vcenter + self.stretch),
                     cv.RGB(255, 0, 0), self.stroke, cv.CV_AA, 0)
     #draw lower half-circles
     for i in range(0, 4):
         cv.Ellipse(self.image, (hcenter, vcenter + self.stretch),
                    (self.dia * i, self.dia * i), 180, 180, 360,
                    cv.RGB(255, 0, 0), self.stroke, cv.CV_AA, 0)
Beispiel #3
0
def fitEllipse(cvImg):
  """
  Use OpenCV to find the contours of the input cvImage and then proceed to
  find best bit ellipses around the contours.  This is a rough approach to
  identifying the primary 'objects of interest' within an image.
  Render the results as ellipses onto a copy of the input image and return
  this cvImage as the result of the method call.
  @param cvImg: input OpenCV image to find best fit ellipses.
  @return: a copy of the input image with ellipse results rendered onto it. 
  """
  def contourIterator(contour):
    """ Helper method to iterate over cvContours. """
    while contour:
      yield contour
      contour = contour.h_next()
  
  # Find all contours.
  stor = cv.CreateMemStorage()
  cont = cv.FindContours(cvImg, stor, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, (0, 0))
  
  cimg = cv.CreateImage((cvImg.width,cvImg.height), cv.IPL_DEPTH_8U, 3)
  cv.CvtColor(cvImg, cimg, cv.CV_GRAY2BGR)
  
#  clen = 0
#  for c in contourIterator(cont):
#    clen += len(c)
#  ptMat = cv.CreateMat(1, clen, cv.CV_32FC2)
  
#  ci = 0
  for c in contourIterator(cont):
#    for (i, (x, y)) in enumerate(c):
#      ptMat[0, i+ci] = (x, y)
#    ci += len(c)
    
    # 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
      ptMat = cv.CreateMat(1, len(c), cv.CV_32FC2)
      for (i, (x, y)) in enumerate(c):
        ptMat[0, i] = (x, y)
                    
      # Draw the current contour in gray
      gray = cv.CV_RGB(150, 150, 150)
      cv.DrawContours(cimg, c, gray, gray,0,1,8,(0,0))
      
      # Fits ellipse to current contour.
      (center, size, angle) = cv.FitEllipse2(ptMat)
      
      # 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))
      #angle = -angle
      
      # Draw ellipse in random color
      color = cv.CV_RGB(0,0,255)
      cv.Ellipse(cimg, center, size, angle, 0, 360, color, 1, cv.CV_AA, 0)
  
  return cimg
Beispiel #4
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 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))
                angle = -angle

                # 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)
Beispiel #6
0
    def ellipse(self,
                dx,
                dy,
                width,
                height,
                color=(0, 255, 0),
                border=-1,
                line=8):
        """Draw a ellipse on image"""
        rotation = 0
        start_angle = 0
        stop_angle = 360
        shift = 0

        cv.Ellipse(self.frame, cv.cvPoint(dx, dy), cv.cvSize(width,
                                                             height), rotation,
                   start_angle, stop_angle, color, border, line, shift)
Beispiel #7
0
    def run(self):
        prev, curr = None, None

        data_files = []
        read_path = os.path.join(self._source, "*.jpg")
        data_files = glob(read_path)
        data_files.sort()

        frame = imread(data_files[0], mode='RGB') # test data in github

        count = 0
        while True:

            print (data_files[count])
            if not self.rect_selector.dragging and not self.paused:
                count += 1
                frame = imread(data_files[count], mode='RGB') # test data in github

            prev, curr = curr, cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            if prev is not None and self._target is not None:
                tgt = self._tracker.track(self._target, prev, curr)

                if tgt is not None:
                    self._target = tgt[:]
                    color = (0, 255, 0)
                else:
                    tgt = self._target[:]
                    color = (0, 0, 255)

                center = (int(tgt[0]), int(tgt[1]))
                scale = (int(tgt[2]), int(tgt[3]))
                angle = tgt[4] * 180.0 / np.pi
                cv.Ellipse(cv.fromarray(frame), center, scale, angle, 0., 360., color, 2)


            self.rect_selector.draw(frame)

            cv2.imshow(self.win, frame)

            ch = cv2.waitKey(1)
            if ch == 27 or ch in (ord('q'), ord('Q')):
                break
            elif ch in (ord('p'), ord('P')):
                self.paused = not self.paused
Beispiel #8
0
def visualize_errors(rel_pts):
    model_file = MODELS[MODE]
    model = pickle.load(open(model_file))
    white_image = cv.CreateImage((750, 500), 8, 3)
    cv.Set(white_image, cv.CV_RGB(255, 255, 255))
    model.translate((50, 0))
    model.draw_to_image(white_image, cv.CV_RGB(0, 0, 255))
    """
    for i,pt in enumerate(model.vertices_full()):
        ctr = (pt[0] + mean_x[i],pt[1] + mean_y[i])
        y_axis = std_y[i]
        x_axis = std_x[i]
        cv.Ellipse(white_image,ctr,(x_axis,y_axis),0,0,360,cv.CV_RGB(255,0,0))
    """

    for i, pt in enumerate(model.vertices_full()):
        print "Drawing model"
        absolute_pts = [Vector2D.pt_sum(pt, rel_pt) for rel_pt in rel_pts[i]]
        #for abs_pt in absolute_pts:
        #    cv.Circle(white_image,abs_pt,2,cv.CV_RGB(0,255,0),-1)
        angle = get_angle(rel_pts[i])

        x_axis = (cos(angle), -1 * sin(angle))
        y_axis = (sin(angle), cos(angle))
        mean_x = lst_avg([x for (x, y) in rel_pts[i]]) + pt[0]
        mean_y = lst_avg([y for (x, y) in rel_pts[i]]) + pt[1]
        std_dev_x = lst_std(
            [Vector2D.dot_prod(rel_pt, x_axis) for rel_pt in rel_pts[i]])
        std_dev_y = lst_std(
            [Vector2D.dot_prod(rel_pt, y_axis) for rel_pt in rel_pts[i]])
        cv.Ellipse(white_image, (mean_x, mean_y), (std_dev_x, std_dev_y),
                   angle * 360 / (2 * pi), 0, 360, cv.CV_RGB(255, 0, 0), 2)
        """
        newmat = cv.CreateMat(1,len(absolute_pts),cv.CV_32SC2)
        for i in range(len(absolute_pts)):
            newmat[0,i] = absolute_pts[i]
        fit_ellipse = cv.FitEllipse2(newmat)
        cv.EllipseBox(white_image,fit_ellipse,cv.CV_RGB(255,0,0))
        """

    cv.SaveImage("comparison.png", white_image)
Beispiel #9
0
                        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

    # draw some polylines
    for i in range(number):
        for a in range(nb_polylines):
Beispiel #10
0
 cv.Threshold(fim, bim, 100., 255, cv.CV_THRESH_BINARY)
 cv.Erode(bim, bim, iterations=1)
 cbim = bim[mazeybb[0]:mazeybb[1], mazexbb[0]:mazexbb[1]]
 #area = cv.ContourArea(pts)
 rect = cv.BoundingRect(cbim)
 if False:  # len(pts) >= 6:
     logging.debug("Ellipse")
     pts = cv.FindContours(cbim, cv.CreateMemStorage(), \
             cv.CV_RETR_LIST, cv.CV_LINK_RUNS)
     m = cv.CreateMat(1, len(pts), cv.CV_32FC2)
     for (i, (x, y)) in enumerate(pts):
         m[0, i] = (x, y)
     e = cv.FitEllipse2(m)
     (ecenter, esize, eangle) = e
     cv.Ellipse(im, (int(ecenter[0] + mazexbb[0]), \
             int(ecenter[1] + mazeybb[0])), (int(esize[0] * 0.5), \
             int(esize[1] * 0.5)), -eangle, 0, 360, \
             (0, 0, 255), 2, cv.CV_AA, 0)
     x = ecenter[0] + mazexbb[0]
     y = ecenter[1] + mazeybb[0]
     m = 1
 else:
     #logging.debug("Rectangle")
     x, y = (int(mazexbb[0] + rect[0]), int(mazeybb[0] + rect[1]))
     w, h = (rect[2], rect[3])
     cv.Rectangle(im, (x, y), (x + w, y + h), (255, 0, 0))
     x = x + w / 2.
     y = y + h / 2.
     m = 2
     outfile.write("%i, %.3f, %.3f, %.3f, %.3f\n" % \
             (frameNumber, x, y, w, h))
 #xym.append([x,y,m])
Beispiel #11
0
def contour_iterator(contour):
    while contour:
        yield contour
        contour = contour.h_next()


for c in contour_iterator(contours):
    # Qde de pontos deve ser ≥ 6 para cv.FitEllipse2
    if len(c) >= 6:
        # Copiar o contorno em um array de (x,y)'s
        PointArray = cv.CreateMat(1, len(c), cv.CV_32FC2)

        for (i, (x, y)) in enumerate(c):
            PointArray[0, i] = (x, y)

        # Encaixar elipse ao contorno
        (center, size, angle) = cv.FitEllipse2(PointArray)

        # Converter dados float da elipse para inteiros
        center = (cv.Round(center[0]), cv.Round(center[1]))
        size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5))

        # Plot elipse
        cv.Ellipse(orig, center, size, angle, 0, 360, cv.RGB(255, 0, 0), 2,
                   cv.CV_AA, 0)

cv.ShowImage("imagem original", orig)
#cv.ShowImage("post-process", processed)
cv.WaitKey(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,