Ejemplo n.º 1
0
def process_image(frame):
    output = {
        "buildings": []
    }
    shapes = []
    
    frame = cv2.resize(frame, IMAGE_SIZE_FOR_TRACING)
    if IGNORE_RED:
        frame = cv2.multiply(frame, unred_mask)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blurred = cv2.blur(gray, (BLUR_RADIUS, BLUR_RADIUS))
    edges = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, THRESH_RADIUS, THRESH_CONSTANT)
    retr_external = True
    
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
    hierarchy = hierarchy[0]
    top_level_contour_indices = [i for i in range(len(hierarchy)) if -1 == hierarchy[i][3]]
    biggest_top_level_contour_index = max(top_level_contour_indices, key=lambda i: cv2.contourArea(contours[i])) if len(top_level_contour_indices) else None
    shape_contour_indices = [i for i in range(len(hierarchy)) if hierarchy[i][3] == biggest_top_level_contour_index]
    contours = [contours[i] for i in shape_contour_indices]
    
    # print hierarchy
    
    # (contour, id, name, descriptor)
    
    contours = [c for c in contours if cv2.contourArea(c) > MIN_SHAPE_SIZE]
    contours = [cv2.approxPolyDP(c, cv2.arcLength(c, True) / CONTOUR_SIMPLIFICATION, True) for c in contours]
    contours.sort(key=lambda x: cv2.contourArea(x), reverse=True)
    contours = contours[:min(len(contours), MAX_CONTOURS)]
    
    if SHOW_SINGLE_CAPTURE_AND_DUMP:
        for contour in contours:
            cv2.drawContours(frame, [contour], -1, (255,0,0))
            x,y = contour[0][0]
            i = len(shapes)
            cv2.putText(frame, str(i), (x,y), cv2.FONT_HERSHEY_PLAIN, 0.7, (0, 0, 255))
            shapes.append(contour)
    else:
        global TIMESTAMP
        TIMESTAMP += 1
        for (contour, id, name, descriptor) in matcher.run_frame(contours):
            cv2.drawContours(frame, [contour], -1, (255,0,0))
            x,y = contour[0][0]
            rotation, flipped, rot_without_flipping = matcher.get_rotation(name, contour)
            label = "{0}:{1}:{2}".format(id, name, int(rotation / math.pi * 180))
            if flipped: label += '(F)'
            cv2.putText(frame, label, (x,y), cv2.FONT_HERSHEY_PLAIN, 0.7, (0, 0, 255))
            if matcher.lifetimes[id] >= MIN_SHAPE_LIFETIME:
                output['buildings'].append(shape_json(contour, id, name, rotation, flipped, rot_without_flipping))
        output['timestamp'] = str(TIMESTAMP)
        
    if SHOW_SINGLE_CAPTURE_AND_DUMP:
        f = open('last_rec.pickle', 'w')
        f.write(pickle.dumps(shapes))
        f.close()
    return frame, output
Ejemplo n.º 2
0
def features_from_contour(contour):
    # print contour
    moments = cv2.moments(contour)
    # print cv2.HuMoments(contour)
    feats = []
    feats += list([x[0] for x in cv2.HuMoments(moments)])[:-1]
    # print feats[0]
    feats.append(cv2.contourArea(contour))
    feats.append(cv2.arcLength(contour, True))

    points = points_to_np([ps[0] for ps in contour])
    # ((center_x,center_y),(w,h),rotation) = cv2.minAreaRect(points)
    # feats += [w, h, w*1.0/h]

    feats += extra_features(contour)

    feats.append(len(contour))
    return feats
Ejemplo n.º 3
0
def features_from_contour(contour):
    # print contour
    moments = cv2.moments(contour)
    # print cv2.HuMoments(contour)
    feats = []
    feats += list([x[0] for x in cv2.HuMoments(moments)])[:-1]
    # print feats[0]
    feats.append(cv2.contourArea(contour))
    feats.append(cv2.arcLength(contour, True))
    
    points = points_to_np([ps[0] for ps in contour])
    # ((center_x,center_y),(w,h),rotation) = cv2.minAreaRect(points)    
    # feats += [w, h, w*1.0/h]
    
    feats += extra_features(contour)
    
    feats.append(len(contour))
    return feats