Example #1
0
def motion_detector():
    global max_area, avg, prev_pos, largest_contour
    contour = cv.FindContours(
        temp_image,
        store,
        mode=cv.CV_RETR_EXTERNAL,
        method=cv.CV_CHAIN_APPROX_NONE)  #Findling contours
    cv.Copy(img, render_image)  #Copying for painting on the image
    if len(contour) != 0:
        temp_contour = contour
        area = 0
        max_area_test = max_area
        while temp_contour != None:  #Routine to find the largest contour
            area = cv.ContourArea(temp_contour)
            if area > max_area_test:
                largest_contour = temp_contour
                max_area_test = area
            temp_contour = temp_contour.h_next()
        rect = cv.BoundingRect(largest_contour)
        cv.DrawContours(render_image, largest_contour, (0, 255, 0),
                        (0, 0, 255), 1, 3)
        cv.Rectangle(render_image, (rect[0], rect[1]),
                     (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 0))
        avg = rect[0] + rect[2] / 2
    else:
        avg = img.width / 2
Example #2
0
    def _get_pos_countour(self, th_img):
        storage = cv.CreateMemStorage(0)
        contour = None
        ci = cv.CreateImage(cv.GetSize(th_img), 8, 1)
        ci = cv.CloneImage(th_img)

        contour = cv.FindContours(th_img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
        points = []

        while contour:
            bound_rect = cv.BoundingRect(list(contour))
            contour = contour.h_next()
            pt1 = (bound_rect[0], bound_rect[1])
            pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
            points.append(pt1)
            points.append(pt2)
            #cv.Rectangle(img, pt1, pt2, cv.CV_RGB(255,0,0), 1) 

        center_point = None

        if len(points):
            center_point = reduce(lambda a, b: ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2), points)
            #cv.Circle(depth, center_point, 10, cv.CV_RGB(255, 255, 255), 1)

        return center_point
Example #3
0
def findPoints(frame, oldRectPoints):
    imageSize = cv.GetSize(frame)
    original = cv.CloneImage(frame)
    hsv = cv.CreateImage(imageSize, 8, 3)
    threshold = cv.CreateImage(imageSize, 8, 1)

    # Do things to the image to isolate the red parts

    cv.CvtColor(original, hsv, cv.CV_RGB2HSV)

    cv.InRangeS(hsv, (110, 80, 80), (140, 255, 255), threshold)
    cv.Erode(threshold, threshold, iterations=5)
    cv.Dilate(threshold, threshold, iterations=5)
    cv.ShowImage("shit", threshold)

    memory = cv.CreateMemStorage(0)
    clone = cv.CloneImage(threshold)
    contours = cv.FindContours(clone, memory, cv.CV_RETR_LIST,
                               cv.CV_CHAIN_APPROX_SIMPLE, (0, 0))

    #   area = cv.ContourArea(contours)
    if not contours:
        # If there's no red on the screen
        rectPoints = oldRectPoints
    else:
        rectPoints = cv.BoundingRect(contours)
    # print rectPoints

    return rectPoints
Example #4
0
def dewarp(image, window, clicked_corners):
    debug = cv.CloneImage(image)

    # draw red line around edges for debug purposes
    cv.PolyLine(debug, [[
        clicked_corners[0], clicked_corners[1], clicked_corners[3],
        clicked_corners[2]
    ]], True, cv.RGB(0, 255, 0), 7)

    cv.ShowImage(window, debug)
    cv.WaitKey()

    # Assemble a rotated rectangle out of that info
    #rot_box = cv.MinAreaRect2(corners)
    enc_box = cv.BoundingRect(clicked_corners)
    new_corners = [(0, 0), (enc_box[2] - 1, 0), (0, enc_box[3] - 1),
                   (enc_box[2] - 1, enc_box[3] - 1)]

    warp_mat = cv.CreateMat(3, 3, cv.CV_32FC1)
    cv.GetPerspectiveTransform(clicked_corners, new_corners, warp_mat)

    rotated = cv.CloneImage(image)
    cv.WarpPerspective(image, rotated, warp_mat)

    cv.ShowImage(window, rotated)
    cv.WaitKey(10)

    return rotated
Example #5
0
    def getStandardizedRects(self):
        '''
        @return: the boxes centered on the target center of mass +- n_sigma*std
        @note: You must call detect() before getStandardizedRects() to see updated results.
        '''
        #create a list of the top-level contours found in the contours (cv.Seq) structure
        rects = []
        if len(self._contours) < 1: return (rects)
        seq = self._contours
        while not (seq == None):
            (x, y, w, h) = cv.BoundingRect(seq)
            if (cv.ContourArea(seq) >
                    self._minArea):  # and  self._filter(rect)
                r = pv.Rect(x, y, w, h)
                moments = cv.Moments(seq)
                m_0_0 = cv.GetSpatialMoment(moments, 0, 0)
                m_0_1 = cv.GetSpatialMoment(moments, 0, 1)
                m_1_0 = cv.GetSpatialMoment(moments, 1, 0)
                mu_2_0 = cv.GetCentralMoment(moments, 2, 0)
                mu_0_2 = cv.GetCentralMoment(moments, 0, 2)

                cx = m_1_0 / m_0_0
                cy = m_0_1 / m_0_0
                w = 2.0 * self._rect_sigma * np.sqrt(mu_2_0 / m_0_0)
                h = 2.0 * self._rect_sigma * np.sqrt(mu_0_2 / m_0_0)

                r = pv.CenteredRect(cx, cy, w, h)

                rects.append(r)
            seq = seq.h_next()

        if self._filter != None:
            rects = self._filter(rects)

        return rects
Example #6
0
    def process(self):

        def seq_to_iter(seq):
            while seq:
                yield seq
                seq = seq.h_next()

        def score_rect(r,p):
            x,y,w,h = r
            return w * h

        cv.Resize(self.frame, self.resized_frame)
        cv.CvtColor(self.resized_frame, self.hsv_frame, cv.CV_RGB2HSV)
        cv.Smooth(self.hsv_frame, self.smooth_frame, cv.CV_GAUSSIAN,
                31)

        for p in self.pompon:
            if p.calibration_done:

                self.in_range(p, self.smooth_frame, self.bin_frame)
                cv.Erode(self.bin_frame, self.mask, None, self.dilatation);

                if self.show_binary:
                    self.mask2 = cv.CloneImage(self.mask) # for miniature

                contour = seq_to_iter(cv.FindContours(self.mask,
                        cv.CreateMemStorage(), cv.CV_RETR_EXTERNAL,
                        cv.CV_CHAIN_APPROX_SIMPLE));

                rects = map((lambda c: cv.BoundingRect(c, 0)), contour)
                if rects:
                    x,y,w,h = max(rects, key=lambda r: score_rect(r,p))

                    p.pos = self.proc2sym(x+w/2,y+h/2)
Example #7
0
    def findRectPoints(self, oldRectPoints):
        hueRange = self.hueRange
        clone = cv.CloneImage(self.frame)
        hsv = cv.CloneImage(self.channels3)
        threshold = cv.CloneImage(self.channels1)
        threshold2 = cv.CloneImage(self.channels1)

        cv.CvtColor(clone, hsv, cv.CV_RGB2HSV)

        cv.InRangeS(hsv, (165, 100, 100), (180, 255, 255), threshold)
        cv.InRangeS(hsv, (0, 100, 100), (15, 255, 255), threshold2)
        cv.Add(threshold, threshold2, threshold)
        self.hue += 1
        print self.hue
        cv.Erode(threshold, threshold, iterations=5)
        cv.Dilate(threshold, threshold, iterations=5)

        cv.ShowImage(self.color, threshold)

        memory = cv.CreateMemStorage(0)
        clone2 = cv.CloneImage(threshold)
        contours = cv.FindContours(clone2, memory, cv.CV_RETR_LIST,
                                   cv.CV_CHAIN_APPROX_SIMPLE, (0, 0))
        if not contours:
            rectPoints = oldRectPoints
        else:
            rectPoints = cv.BoundingRect(contours)
        return rectPoints
Example #8
0
def compute_sample_image_coordinates(image_shape, sample_bounds):
    # TODO:  get this coefficient out of here
    # Also, if the sample is not a square, we may amplify the differences by
    # the neighborhood, unnaturally.  Include a maximum number of pixels as
    # a function of screen resolution
    neighborhood_size_coeff = Properties.neighborhood_size_coeff

    sample_roi = cv.BoundingRect(sample_bounds, 0)
    neighborhood_roi = (sample_roi[0] -
                        neighborhood_size_coeff * sample_roi[2],
                        sample_roi[1] -
                        neighborhood_size_coeff * sample_roi[3],
                        sample_roi[2] * (2 * neighborhood_size_coeff + 1),
                        sample_roi[3] * (2 * neighborhood_size_coeff + 1))

    #print cv.Get1D(backprojected_neighborhood_boundary, 0)
    neighborhood_roi = cvutils.rect_intersection(
        neighborhood_roi, (0, 0, image_shape[1], image_shape[0]))

    replicated_neighborhood_corner = np.hstack((neighborhood_roi[0] * np.ones(
        (4, 1)), neighborhood_roi[1] * np.ones((4, 1))))
    replicated_neighborhood_corner = replicated_neighborhood_corner.reshape(
        (1, 4, 2))
    sample_bounds = np.array(sample_bounds)
    local_sample_boundary = sample_bounds - replicated_neighborhood_corner

    return neighborhood_roi, cvutils.array2point_list(local_sample_boundary)
Example #9
0
def find_connected_components(frame):
    """Find connected components from an image.
    :: iplimage -> [ dict(box<CvBox2D>, rect<CvRect>) ]

    Takes as input a grayscale image that should have some blobs in
    it. Outputs a data structure containing the 'centers' of the blobs
    and minimal rectangles enclosing them. A maximum of 3 blobs are
    returned.

    The input frame is modified in place.
    """

    contours = get_contours(frame)
    #out = draw_contours(frame, contours)

    candidates = []
    for contour in contours:
        storage = cv.CreateMemStorage(0)
        minBox = cv.MinAreaRect2(contour, storage)

        boundingRect = cv.BoundingRect(contour, 0)
        candidates.append({'box': minBox, 'rect': boundingRect})

    candidates = sorted(candidates,
                        key=lambda x: getArea(x['box']),
                        reverse=True)
    return candidates
Example #10
0
    def findRectPoints(self, oldRectPoints):
        hueRange = self.hueRange
        satRange = self.satRange
        valRange = self.valRange
        clone = cv.CloneImage(self.frame)
        hsv = cv.CloneImage(self.channels3)
        threshold = cv.CloneImage(self.channels1)
        threshold2 = cv.CloneImage(self.channels1)

        cv.Smooth(clone, clone, cv.CV_GAUSSIAN, 7, 7)

        cv.CvtColor(clone, hsv, cv.CV_BGR2HSV)
        cv.InRangeS(hsv, (hueRange[0], satRange[0], valRange[0]),
                    (hueRange[1], satRange[1], satRange[1]), threshold)
        cv.InRangeS(hsv, (hueRange[2], satRange[0], satRange[0]),
                    (hueRange[3], satRange[1], valRange[1]), threshold2)
        cv.Add(threshold, threshold2, threshold)
        cv.Erode(threshold, threshold, iterations=5)
        cv.Dilate(threshold, threshold, iterations=5)

        #       cv.ShowImage(self.color, threshold)

        memory = cv.CreateMemStorage(0)
        clone2 = cv.CloneImage(threshold)
        contours = cv.FindContours(clone2, memory, cv.CV_RETR_LIST,
                                   cv.CV_CHAIN_APPROX_SIMPLE, (0, 0))
        if not contours:
            rectPoints = oldRectPoints
        else:
            rectPoints = cv.BoundingRect(list(contours))
        return rectPoints
  def find_biggest_region(self):
    """ this code should find the biggest region and
        then determine some of its characteristics, which
        will help direct the drone
    """
    # copy the thresholded image
    cv.Copy( self.threshed_image, self.copy )  # copy self.threshed_image
    # this is OpenCV's call to find all of the contours:
    contours = cv.FindContours(self.copy, self.storage, cv.CV_RETR_EXTERNAL,
                               cv.CV_CHAIN_APPROX_SIMPLE)

    # Next we want to find the *largest* contour
    if len(contours)>0:
      biggest = contours
      biggestArea=cv.ContourArea(contours)
      while contours != None:
        nextArea=cv.ContourArea(contours)
        if biggestArea < nextArea:
          biggest = contours
          biggestArea = nextArea
        contours=contours.h_next()
    
      #Use OpenCV to get a bounding rectangle for the largest contour
      self.br = cv.BoundingRect(biggest,update=0)
      
      #Publish the data.
      self.publishBoxData()
Example #12
0
def get_elements(filename, treshold=50, minheight=15, minarea=200, elements=6):
    src = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE)
    test = cv.CreateImage(cv.GetSize(src), 32, 3)
    dst = cv.CreateImage(cv.GetSize(src), 8, 1)
    storage = cv.CreateMemStorage(0)
    cv.Canny(src, dst, treshold, treshold * 3, 3)

    storage = cv.CreateMemStorage(0)
    seqs = cv.FindContours(dst, storage, cv.CV_RETR_TREE,
                           cv.CV_CHAIN_APPROX_NONE, (0, 0))

    res = []

    c = seqs.h_next()
    while True:
        if not c:
            break
        box = cv.BoundingRect(c)
        area = box[2] * box[3]
        #and (area > minarea)
        if (box[3] > minheight):
            res.append(box)
        c = c.h_next()

    if len(res) < elements:
        while len(res) < elements:
            m = 0
            c = 0
            for i, e in enumerate(res):
                if e[3] > m:
                    m = e[3]
                    c = i

            big = res.pop(c)
            res.append((big[0], big[1], int(big[2] * 1.0 / 2), big[3]))
            res.append((big[0] + int(big[2] * 1.0 / 2), big[1],
                        int(big[2] * 1.0 / 2), big[3]))

    #for box in res:
    #    cv.Rectangle(dst, (box[0],box[1]), (box[0]+box[2],box[1]+box[3]), cv.RGB(255,255,255))

    #cv.ShowImage('Preview2',dst)
    #cv.WaitKey()

    imgs = []
    print len(res)
    for box in res:
        cv.SetImageROI(src, box)

        tmp = cv.CreateImage((box[2], box[3]), 8, 1)

        cv.Copy(src, tmp)
        hq.heappush(imgs, (box[0], tmp))

        cv.ResetImageROI(src)

    res = [hq.heappop(imgs)[1] for i in xrange(len(res))]
    return res
Example #13
0
def blob_statistics(binary_image,
                    max_area=99999.0,
                    max_dim=99999.0):  #, show=False):
    statistics = []
    storage = cv.CreateMemStorage(0)
    #FindContours(image,        storage, mode=CV_RETR_LIST, method=CV_CHAIN_APPROX_SIMPLE, offset=(0, 0))
    contours = cv.FindContours(binary_image, storage, cv.CV_RETR_TREE,
                               cv.CV_CHAIN_APPROX_SIMPLE, (0, 0))
    #number_contours, contours = cv.FindContours(binary_image, storage, cv.sizeof_CvContour, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, (0,0))
    #TODO: FIGURE OUT WHAT THE EQUIV OF SIZEOF IS IN OPENCV2
    #import pdb
    #pdb.set_trace()

    original_ptr = contours
    while contours != None:
        try:
            bx, by, bwidth, bheight = cv.BoundingRect(contours, 0)
            bounding_rect = Rect(bx, by, bwidth, bheight)
            moments = cv.Moments(contours, 0)
            #area = moments.m00
            #approximation to area since cvMoments' area seem broken
            area = bounding_rect.width * bounding_rect.height
            if False:
                #TODO NOT WORKING!!
                if moments.m00 == 0.0:
                    centroid = (bounding_rect.x, bounding_rect.y)
                else:
                    centroid = (moments.m10 / moments.m00,
                                moments.m01 / moments.m00)
            else:
                if bwidth > 0:
                    cx = bx + bwidth / 2.
                else:
                    cx = bx

                if bheight > 0:
                    cy = by + bheight / 2.
                else:
                    cy = by
                centroid = (cx, cy)
                #if show:
                #    print 'areas is', area, bounding_rect.width, bounding_rect.height
                if area > max_area or bounding_rect.width > max_dim or bounding_rect.height > max_dim:
                    cv.DrawContours(binary_image, contours, cv.Scalar(0),
                                    cv.Scalar(0), 0, cv.CV_FILLED)
                else:
                    stats = {
                        'area': area,
                        'centroid': centroid,
                        'rect': bounding_rect
                    }
                    statistics.append(stats)
                contours = contours.h_next()
        except Exception, e:
            pass
            #This is due to OPENCV BUG and not being able to see inside contour object'
            break
Example #14
0
def get_blob_from_contour(contour):
    bound_rect = cv.BoundingRect(list(contour))  # (x, y, wid, hig)
    # get points of longest diagonal
    pt1 = (bound_rect[0], bound_rect[1])
    pt2 = Math.add_vectors(pt1, (bound_rect[2], bound_rect[3]))
    # get centroid by adding them and scaling the result by 1/2
    # float is used(*2.0*), otherwise scaling will be by 0
    centroid = Math.int_vec(Math.add_vectors(pt1, pt2, 1 / 2.0))
    return pt1, pt2, centroid
Example #15
0
def seqs_boxes(seqs, minsize=25):
    boxes = []
    new_seqs = []
    for seq in seqs:
        box = cv.BoundingRect(seq)
        if box[2] >= minsize and box[3] >= minsize and not is_inside(
                boxes, box):
            boxes.append(box)
            new_seqs.append(seq)
    boxes = [box for box in boxes if not is_inside(boxes, box)]
    return boxes, new_seqs
Example #16
0
def find_biggest_region():
    """ finds all the contours in threshed image, finds the largest of those,
        and then marks in in the main image
    """
    # get D so that we can change values in it
    global D

    cv.Copy(D.threshed_image, D.copy)  # copy threshed image

    # this is OpenCV's call to find all of the contours:
    contours = cv.FindContours(D.copy, D.storage, cv.CV_RETR_EXTERNAL, \
                                   cv.CV_CHAIN_APPROX_SIMPLE)

    # Next we want to find the *largest* contour
    if len(contours) > 0:
        biggest = contours
        biggestArea = cv.ContourArea(contours)
        while contours != None:
            nextArea = cv.ContourArea(contours)
            if biggestArea < nextArea:
                biggest = contours
                biggestArea = nextArea
            contours = contours.h_next()

        # Use OpenCV to get a bounding rectangle for the largest contour
        br = cv.BoundingRect(biggest, update=0)

        #print "in find_regions, br is", br

        # Example of drawing a red box
        # Variables: ulx ~ upper left x, lry ~ lower right y, etc.
        ulx = br[0]
        lrx = br[0] + br[2]
        uly = br[1]
        lry = br[1] + br[3]
        cv.PolyLine(D.image, [[(ulx, uly), (lrx, uly), (lrx, lry),
                               (ulx, lry)]], 1, cv.RGB(255, 0, 0))

        # Example of drawing a yellow circle
        # Variables: cenx, ceny
        cenx = (ulx + lrx) / 2
        ceny = (uly + lry) / 2
        cv.Circle(D.image, (cenx, ceny),
                  8,
                  cv.RGB(255, 255, 0),
                  thickness=1,
                  lineType=8,
                  shift=0)
def detecta(imagem):

    cv.Smooth(imagem, imagem, cv.CV_GAUSSIAN, 3)
    maiorArea = 0
    listaContornos = []
    listaVertices = []

    cv.AbsDiff(imagem, fundo, mascara)
    cv.CvtColor(mascara, cinza, cv.CV_BGR2GRAY)
    cv.Threshold(cinza, cinza, 50, 255, cv.CV_THRESH_BINARY)

    cv.Dilate(cinza, cinza, None, 18)
    cv.Erode(cinza, cinza, None, 18)

    armazenamento = cv.CreateMemStorage(0)
    contorno = cv.FindContours(cinza, armazenamento, cv.CV_RETR_LIST,
                               cv.CV_LINK_RUNS)

    while contorno:
        vertices_do_retangulo = cv.BoundingRect(list(contorno))
        listaVertices.append(vertices_do_retangulo)

        listaContornos.append(cv.ContourArea(contorno))
        maiorArea = max(listaContornos)
        maiorArea_index = listaContornos.index(maiorArea)
        retangulo_de_interesse = listaVertices[maiorArea_index]

        contorno = contorno.h_next()

        ponto1 = (retangulo_de_interesse[0], retangulo_de_interesse[1])
        ponto2 = (retangulo_de_interesse[0] + retangulo_de_interesse[2],
                  retangulo_de_interesse[1] + retangulo_de_interesse[3])
        cv.Rectangle(imagem, ponto1, ponto2, cv.CV_RGB(0, 0, 0), 2)
        cv.Rectangle(cinza, ponto1, ponto2, cv.CV_RGB(255, 255, 255), 1)
        largura = ponto2[0] - ponto1[0]
        altura = ponto2[1] - ponto1[1]
        cv.Line(cinza, (ponto1[0] + largura / 2, ponto1[1]),
                (ponto1[0] + largura / 2, ponto2[1]), cv.CV_RGB(255, 255,
                                                                255), 1)
        cv.Line(cinza, (ponto1[0], ponto1[1] + altura / 2),
                (ponto2[0], ponto1[1] + altura / 2), cv.CV_RGB(255, 255,
                                                               255), 1)
        global x
        x = ((640 / 2 - (ponto1[0] + (largura / 2))) * -1) / 5

    cv.ShowImage("Webcam", imagem)
    cv.ShowImage("Mascara", mascara)
    cv.ShowImage("Cinza", cinza)
Example #18
0
def motion_bbox(output, motion):
    #global muestra, prom
    global bbox_list
    #INICIO = time.time()
    contour = cv.FindContours(motion, mem_storage, cv.CV_RETR_CCOMP,
                              cv.CV_CHAIN_APPROX_SIMPLE)
    bbox_list = []  #lista para almacenar los bounding box de las manchas
    average_box_area = 0  #variable para obtener el area en promedio de las bounding box
    while contour:  #recorrido de los contornos/manchas
        bbox = cv.BoundingRect(
            list(contour))  #obtencion del bounding box del contorno actual
        pt1 = (bbox[0], bbox[1])  #punto 1 del bounding box
        pt2 = (bbox[0] + bbox[2], bbox[1] + bbox[3])  #punto 2 del bounding box
        w, h = abs(pt1[0] - pt2[0]), abs(
            pt1[1] - pt2[1])  #ancho y largo del bounding box
        #obtencion de puntos del contorno para crear un wire-frame
        polygon_points = cv.ApproxPoly(list(contour), mem_storage,
                                       cv.CV_POLY_APPROX_DP)
        #mostrar o las manchas de movimiento
        if SHOW_MOVEMENT_AREA:
            cv.FillPoly(output, [
                list(polygon_points),
            ], cv.CV_RGB(255, 255, 255), 0, 0)
        #mostrar o no los contornos de las manchas de movimiento
        if SHOW_MOVEMENT_CONTOUR and w * h > AREA * MIN_PERCENT:
            cv.PolyLine(output, [
                polygon_points,
            ], 0, cv.CV_RGB(255, 255, 255), 1, 0, 0)
        average_box_area += w * h  #acumulacion de totales de areas
        bbox_list.append((pt1, pt2))  #lista con todos los bounding box
        contour = contour.h_next()  #lectura del siguiente contorno, si hay
    if len(bbox_list) > 0:  #si hubo movimiento
        average_box_area = average_box_area / float(
            len(bbox_list))  #area promedio de bounding box
        new_bbox_list = [
        ]  #nueva lista de bounding box, eliminando los menores al area promedio
        for i in range(len(bbox_list)):  #recorrido de los bounding box
            pt1, pt2 = bbox_list[i]  #separacion en dos puntos del bounding box
            w, h = abs(pt1[0] - pt2[0]), abs(
                pt1[1] - pt2[1])  #obtencion del ancho y largo
            if w * h >= average_box_area and w * h > AREA * MIN_PERCENT:  #comparacion del area del bounding box con el promedio
                new_bbox_list.append(
                    (pt1,
                     pt2))  #si es mayor o igual, se queda en la nueva lista
    bbox_list = get_collided_bboxes(
        new_bbox_list
    )  #combinacion de varios bounding box en uno si estan en contacto
Example #19
0
 def __init__(self, bid, contour):
     self.bid = int(bid)
     self.name = self.label_text_map[bid]
     while (ishole(contour)):
         contour = contour.h_next()
     self.contour = contour
     self.area = cv.ContourArea(contour)
     self.center_of_mass = im.center_of_mass(contour)
     l, t, w, h = cv.BoundingRect(contour)
     self.bbox = (l, t, l + w, t + h)
     self.width = w
     self.height = h
     self.near_set = set([])
     self.north_set = set([])
     self.south_set = set([])
     self.east_set = set([])
     self.west_set = set([])
Example #20
0
    def _get_bounding_rects(self, phase, time):
        '''
        Return rectangle where to look for this marker
        @param phase:
        @param time:
        '''
        nrect = []
        for cor in self.corners:
            (x, y, wx, wy) = cor.get_rectangle(time)
            #            print (x, y, wx, wy)
            nrect.extend([(x, y), (x + wx, y + wy)])


#        print nrect
        rect = cv.BoundingRect(nrect)
        #        print rect
        return [rect]
Example #21
0
    def run(self):
  
        while not self._stop.isSet():
            task  = self.q.get()
            
            if task != None:
                obj, image = task
        
                rect = cv.BoundingRect(obj.cont)
                siftimage = siftfastpy.Image(rect[2], rect[3])
                cv.CvtColor(image, self.gray, cv.CV_BGR2GRAY)
                cv.SetZero(self.mask)
                cv.FillPoly(self.mask, [obj.cont], cv.Scalar(255))
                cv.And(self.gray, self.mask, self.gray)
                gnp = np.asarray(cv.GetSubRect(self.gray, rect))
                siftimage.SetData(gnp)
                t0 = time.time()

                # compute keypoints and time how long it takes
                frames,desc = siftfastpy.GetKeypoints(siftimage)
                self.stats.append((rect[2]*rect[3], time.time() - t0))

                # compute feature vector
                tmp = np.concatenate((frames[:,2:4], desc), axis=1).astype('float64')
                
                # search in the flann tree for the feature vectors
                n = 2
                thr = 1.5
                result, dists = self.flann.nn_index(tmp, n, checks=32)
                
                # ismatch contains the indices in the testset for which a match is found
                ismatch = dists[:,1] > thr * dists[:,0]
                               
                # meta contains the index to object-ID mapping
                obj.ids = []
                for i, res in enumerate(result):
                    if ismatch[i]:
                        obj.ids.append(self.meta[res[0]][0])
#                obj.ids = [self.meta[res][0] for i, res in enumerate(result) if ismatch[i]]

                
                # transfer keypoints back to full frame coordinates
                frames[:,0] += rect[0]
                frames[:,1] += rect[1] 
                obj.frames = frames
                obj.desc = desc
Example #22
0
	def movearea(self, contour, input):
		points = []
		area = 0
		while contour:
			bound_rect = cv.BoundingRect(list(contour))
			contour = contour.h_next()

			# Compute the bounding points to the boxes that will be drawn on the screen
			pt1 = (bound_rect[0], bound_rect[1])
			pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])

			# Add this latest bounding box to the overall area that is being detected as movement
			area += ((pt2[0] - pt1[0]) * (pt2[1] - pt1[1]))
			points.append(pt1)
			points.append(pt2)
			cv.Rectangle(input, pt1, pt2, cv.CV_RGB(255,0,0), 1)
		return area
def CenterFunction(R, imghsv):
	imgyellowthresh=getthresholdedimgRGeneric(R, imghsv) # creaza mastile de culoare, aici specifice robotului R4

	cv.Erode(imgyellowthresh,imgyellowthresh,None,3)#filtru 
	cv.Dilate(imgyellowthresh,imgyellowthresh,None,6)#filtru 
	img2=cv.CloneImage(imgyellowthresh)#cloneaza imaginea in img2, useless
	storage = cv.CreateMemStorage(0)#creaza un loc in memorie unde sa stocheze, necesar ptr FindContours
	contour = cv.FindContours(imgyellowthresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)#gaseste contururile robotilor
	points = []	

#	This is the new part here. ie Use of cv.BoundingRect()
	while contour:
		# Draw bounding rectangles
		bound_rect = cv.BoundingRect(list(contour)) #creaza un patratel din punctele din contur, ptr afisare/debug
		#bound_rect = cv.BoundingRect(contour)

		# for more details about cv.BoundingRect,see documentation
		pt1 = (bound_rect[0], bound_rect[1]) #
		pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
		points.append(pt1)
		points.append(pt2)
		cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1)
		#pana aici s-a desenat patratul
	#	Calculating centroids
	
		centroidx=cv.Round((pt1[0]+pt2[0])/2)
		centroidy=cv.Round((pt1[1]+pt2[1])/2)
		area = cv.ContourArea(list(contour))
		#print "CentroidXY:" + str(centroidx) +":" +str(centroidy) + "A:" + str(area)
		if(area > 100):
			print "CentroidXY:" + str(centroidx) +":" +str(centroidy) + "A:" + str(area)
			coords = pack('iiiii', 4,centroidx, centroidy, 0, int(time.time()))
			mosq.publish("coords", coords, 0)
	
		contour = contour.h_next()	
		print contour
		#	Identifying if blue or yellow blobs and adding centroids to corresponding lists	
		if (169<cv.Get2D(imghsv,centroidy,centroidx)[0]<180):
			red.append((centroidx,centroidy))
		elif (100<cv.Get2D(imghsv,centroidy,centroidx)[0]<120):
			blue.append((centroidx,centroidy))
		elif (67<cv.Get2D(imghsv,centroidy,centroidx)[0]<100):
			green.append((centroidx, centroidy))

	return
Example #24
0
def getpoints(image):
    points = []
    cv.CvtColor(image, grey, cv.CV_RGB2GRAY)
    cv.Threshold(grey, grey, THRESHOLD, 255, cv.CV_THRESH_BINARY)
    cv.Copy(grey, grey2)

    storage = cv.CreateMemStorage(0)
    contour = cv.FindContours(grey, storage, cv.CV_RETR_CCOMP,
                              cv.CV_CHAIN_APPROX_SIMPLE)
    while contour:
        bound_rect = cv.BoundingRect(list(contour))
        contour = contour.h_next()

        pt = (bound_rect[0] + bound_rect[2] / 2,
              bound_rect[1] + bound_rect[3] / 2)
        #cv.Circle(image, pt, 2, cv.CV_RGB(255,0,0), 1)
        points.append(pt)
    return points
Example #25
0
    def run(self):
        starttime = time.time()

        gray = cv.CreateMat(480, 640, cv.CV_8UC1)
        im = cv.LoadImage(self.image)
        rect = cv.BoundingRect(self.obj.cont)
        siftimage = siftfastpy.Image(rect[2], rect[3])
        cv.CvtColor(im, gray, cv.CV_BGR2GRAY)
        gnp = np.asarray(cv.GetSubRect(gray, rect))
        siftimage.SetData(gnp)

        print 'initialization in: %fs' % (time.time() - starttime)

        frames, desc = siftfastpy.GetKeypoints(siftimage)
        self.obj.frames = frames
        self.obj.desc = desc
        print '%d  keypoints found in %fs' % (frames.shape[0],
                                              time.time() - starttime)
Example #26
0
def dewarp(image,window,corners):
	
	debug = image.copy()
	
	print(corners)

	# draw red line around edges for debug purposes
	cv2.polylines(debug, np.int32([[corners[0],corners[1], corners[3],corners[2]]]),
				  True, (0,255,0),7)

	#show results
	if DISPLAY:
		cv2.imshow(window,debug)
		cv2.cv.ResizeWindow(window,960,640)
		cv2.waitKey(WAIT_TIME)

	# Assemble a rotated rectangle out of that info
	# Todo: move to cV2
	np_corners = np.array(corners)
	rot_box = cv.MinAreaRect2(corners)
	enc_box = cv.BoundingRect(corners)

	scaling = 1.0
	border = 10
	pt_x = enc_box[2]*scaling
	pt_y = enc_box[3]*scaling

	new_corners = [(border,border),(pt_x-1+border,border),
				   (border,pt_y-1+border),(pt_x-1+border,pt_y-1+border)]

	corners = np.array(corners,np.float32)
	new_corners = np.array(new_corners,np.float32)
   
	warp_mat = cv2.getPerspectiveTransform(corners, new_corners)
	rotated = cv2.warpPerspective(image, warp_mat, (int(round(pt_x+border*2)),
													int(round(pt_y+border*2))))
 
	#show results
	if DISPLAY:
		cv2.imshow(window,rotated)  
		cv2.cv.ResizeWindow(window,960,640)
		cv2.waitKey(WAIT_TIME)
   
	return rotated
Example #27
0
 def get_rectangle(self, m_d):
     '''
     Return rectangle where we will be looking for this corner
     @param time: current time
     @param size: image size
     '''
     self.get_bounding_points(m_d)
     size = m_d.size
     (x, y, wx, wy) = cv.BoundingRect(self.bounds)
     (x, y, wx,
      wy) = x - MIN_SIZE, y - MIN_SIZE, wx + 2 * MIN_SIZE, wy + 2 * MIN_SIZE
     min_wx = MIN_RECT * size[0]
     min_wy = MIN_RECT * size[1]
     if (wx < min_wx):
         x -= (min_wx - wx) / 2
         wx = min_wx
     if (wy < min_wy):
         y -= (min_wy - wy) / 2
         wy = min_wy
     return (x, y, wx, wy)
Example #28
0
    def getBoundingRects(self):
        '''
        @return: the bounding boxes of the external contours of the foreground mask.
        @note: You must call detect() before getBoundingRects() to see updated results.
        '''
        #create a list of the top-level contours found in the contours (cv.Seq) structure
        rects = []
        if len(self._contours) < 1: return (rects)
        seq = self._contours
        while not (seq == None):
            (x, y, w, h) = cv.BoundingRect(seq)
            if (cv.ContourArea(seq) > self._minArea):
                r = pv.Rect(x, y, w, h)
                rects.append(r)
            seq = seq.h_next()

        if self._filter != None:
            rects = self._filter(rects)

        return rects
Example #29
0
def find_connected_components(frame):
    """Find connected components from an image.
    :: iplimage -> [ dict(box<CvBox2D>, rect<CvRect>) ]

    Takes as input a grayscale image that should have some blobs in
    it. Outputs a data structure containing the 'centers' of the blobs
    and minimal rectangles enclosing them. A maximum of 3 blobs are
    returned.

    The input frame is modified in place.
    """

    # A workaround for OpenCV 2.0 crash on receiving a (nearly) black image
    nonzero = cv.CountNonZero(frame)
    if nonzero < 20:
        return []
    logging.debug("Segmentation got an image with %d nonzero pixels", nonzero)

    contours = get_contours(frame)
    #out = draw_contours(frame, contours)

    if contours is None:
        return []

    candidates = []
    while contours:
        storage = cv.CreateMemStorage(0)
        minBox = cv.MinAreaRect2(contours, storage)

        boundingRect = cv.BoundingRect(contours, 0)
        candidates.append({ 'box' : minBox, 'rect' : boundingRect })

        contours = contours.h_next()

    candidates = sorted( candidates,
                         key=lambda x:getArea(x['box']),
                         reverse=True )
    return candidates
Example #30
0
def get_top(shape, center, theta):
    pt = center
    EPSILON = 1.0
    angle = theta
    scale = 0
    (r_x, r_y, r_w, r_h) = cv.BoundingRect(shape)
    if SCALE_METHOD == AXIS:
        top_pt = center
        best_scale = 1
        while (pt[0] > r_x and pt[0] < r_x + r_w and pt[1] > r_y
               and pt[1] < r_y + r_w):
            print
            (x, y) = pt
            new_x = x + EPSILON * sin(angle)
            new_y = y - EPSILON * cos(angle)
            pt = (new_x, new_y)
            scale += EPSILON
            if cv.PointPolygonTest(shape, pt, 0) > 0:
                top_pt = pt
                best_scale = scale
        return (top_pt, best_scale)
    else:
        return ((r_x + r_w / 2, r_y), max(r_w, r_h))