def check_pnp(H, W, rvec, tvec):

    image = numpy.full((H, W, 3), 64, dtype=numpy.uint8)
    landmarks_3d = numpy.array(
        [[-1, -1, -1], [-1, +1, -1], [+1, +1, -1], [+1, -1, -1], [-1, -1, +1],
         [-1, +1, +1], [+1, +1, +1], [+1, -1, +1]],
        dtype=numpy.float32)
    aperture_x, aperture_y = 0.5, 0.5
    mat_camera = tools_pr_geom.compose_projection_mat_3x3(
        image.shape[1], image.shape[0], aperture_x, aperture_y)
    landmarks_2d, jac = tools_pr_geom.project_points(landmarks_3d, rvec,
                                                     tvec, mat_camera,
                                                     numpy.zeros(5))

    noise = 0 * numpy.random.rand(8, 1, 2)

    colors = tools_IO.get_colors(8)
    rvec2, tvec2, landmarks_2d_check = tools_pr_geom.fit_pnp(
        landmarks_3d, landmarks_2d + noise, mat_camera)

    for i, point in enumerate(landmarks_2d_check):
        cv2.circle(image, (point[0], point[1]),
                   5,
                   colors[i].tolist(),
                   thickness=-1)

    cv2.imwrite(folder_out + 'check_pnp.png', image)
    return
Exemple #2
0
    def get_granules(self, binarized):

        granules = {}
        the_range = numpy.arange(2, 26, 1)
        colors = tools_IO.get_colors(len(the_range), colormap='viridis')
        kernel = numpy.full((3, 3), 255, numpy.uint8)
        empty = numpy.zeros((binarized.shape[0], binarized.shape[1], 3),
                            dtype=numpy.uint8)
        image_result = empty.copy()

        responce = numpy.zeros(
            (len(the_range), binarized.shape[0], binarized.shape[1]),
            dtype=numpy.uint8)

        for i in range(len(the_range)):
            n = the_range[i]
            A = tools_filter.sliding_2d(binarized, -n, n, -n,
                                        n).astype(numpy.uint8)
            A = cv2.dilate(A, kernel=kernel, iterations=n - 1)
            responce[i] = 1 * (A == 255)

        mask = 0 * responce[-1]

        for i in reversed(range(len(the_range))):
            local_responce = responce[i] ^ mask
            granules[the_range[i]] = (local_responce).sum()
            image_result[local_responce > 0] = colors[i]
            mask = mask | responce[i]
        return granules, image_result
Exemple #3
0
    def get_granules0(self, binarized):

        granules = {}
        the_range = numpy.arange(2, 26, 1)
        colors = tools_IO.get_colors(len(the_range), colormap='viridis')
        empty = numpy.zeros((binarized.shape[0], binarized.shape[1], 3),
                            dtype=numpy.uint8)
        image_result = empty.copy()
        kernel = numpy.full((3, 3), 255, numpy.uint8)

        for i in range(len(the_range)):
            n = the_range[i]
            A = tools_filter.sliding_2d(binarized, -n, n, -n,
                                        n).astype(numpy.uint8)
            A = cv2.dilate(A, kernel=kernel, iterations=n - 1)
            idx = numpy.where(A == 255)
            image_temp = empty.copy()
            image_temp[idx[0], idx[1], :] = colors[i]
            image_result = tools_image.put_layer_on_image(
                image_result, image_temp)
            cv2.imwrite(
                self.folder_out + 'grain_%02d.png' % the_range[i],
                tools_image.put_layer_on_image(tools_image.saturate(binarized),
                                               image_temp))

        for i in range(len(the_range)):
            granules[the_range[i]] = len(
                numpy.where(image_result == colors[i])[0])

        return granules, image_result
def draw_cube_numpy(img,
                    camera_matrix,
                    dist,
                    rvec,
                    tvec,
                    scale=(1, 1, 1),
                    color=(255, 128, 0)):

    points_3d = numpy.array(
        [[-1, -1, -1], [-1, +1, -1], [+1, +1, -1], [+1, -1, -1], [-1, -1, +0],
         [-1, +1, +0], [+1, +1, +0], [+1, -1, +0]],
        dtype=numpy.float32)
    colors = tools_IO.get_colors(8)

    points_3d[:, 0] *= scale[0]
    points_3d[:, 1] *= scale[1]
    points_3d[:, 2] *= scale[2]

    if len(rvec.shape) == 2 and rvec.shape[0] == 3 and rvec.shape[1] == 3:
        method = 'xx'
    else:
        method = 'cv'

    if method == 'cv':
        points_2d, jac = cv2.projectPoints(
            points_3d,
            numpy.array(rvec, dtype=float).reshape((3, 1)),
            numpy.array(tvec, dtype=float).reshape((3, 1)), camera_matrix,
            dist)
    else:
        points_2d, jac = tools_pr_geom.project_points(points_3d, rvec, tvec,
                                                      camera_matrix, dist)

    result = img.copy()
    points_2d = points_2d.reshape((-1, 2)).astype(int)
    for i, j in zip((0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3),
                    (1, 2, 3, 0, 5, 6, 7, 4, 4, 5, 6, 7)):
        cv2.line(result, (points_2d[i, 0], points_2d[i, 1]),
                 (points_2d[j, 0], points_2d[j, 1]),
                 color,
                 thickness=2)

    for i in (0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3):
        cv2.circle(result, (points_2d[i, 0], points_2d[i, 1]),
                   5,
                   colors[i].tolist(),
                   thickness=-1)

    clr = (255, 255, 255)
    for i in (0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3):
        cv2.putText(
            result, '{0}   {1} {2} {3}'.format(i, points_3d[i, 0],
                                               points_3d[i, 1], points_3d[i,
                                                                          2]),
            (points_2d[i, 0], points_2d[i, 1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
            clr, 1, cv2.LINE_AA)

    return result
Exemple #5
0
def example_segment_detection():
    image = cv2.imread(filename_in)
    segments = F.extract_segments(image, min_size=30)

    image_debug = tools_image.desaturate(image.copy())
    colors = tools_IO.get_colors(len(segments), shuffle=True)
    image_debug = F.draw_segments(image_debug,
                                  segments,
                                  colors,
                                  w=4,
                                  put_text=True)
    cv2.imwrite(folder_out + 'result.png', image_debug)

    return
def example_01_lines_cv(filename_in, folder_out):
    image = cv2.imread(filename_in)

    preprocessed = Ske.binarized_to_skeleton_ski(Ske.binarize(image))
    lines,weights = Hough.get_lines_cv(preprocessed)

    colors = tools_IO.get_colors(256)
    result = tools_image.saturate(preprocessed.copy())
    for line,w in zip(reversed(lines),reversed(weights)):
        cv2.line(result, (int(line[0]), int(line[1])), (int(line[2]), int(line[3])), color=colors[int(w)].tolist(), thickness=4)

    cv2.imwrite(folder_out + 'preprocessed.png', preprocessed)
    cv2.imwrite(folder_out + 'result.png', result)
    return
Exemple #7
0
def draw_annotation(filaname_coco_annnotation,
                    folder_images,
                    folder_out,
                    draw_binary_masks=False):
    coco = COCO(filaname_coco_annnotation)
    colors = tools_IO.get_colors(1 + len(coco.cats))
    class_names = [coco.cats[key]['name'] for key in coco.cats]

    for key in coco.imgToAnns.keys():

        annotations = coco.imgToAnns[key]
        image_id = annotations[0]['image_id']
        filename = coco.imgs[image_id]['file_name']
        if not os.path.isfile(folder_images + filename):
            continue

        boxes, category_IDs = [], []
        for annotation in annotations:
            bbox = annotation['bbox']
            boxes.append(
                [bbox[1], bbox[0], bbox[1] + bbox[3], bbox[0] + bbox[2]])
            category_IDs.append(annotation['category_id'])

        if draw_binary_masks:
            image = cv2.imread(folder_images + filename)
            result = numpy.zeros_like(image)
            for box in boxes:
                top, left, bottom, right = box
                cv2.rectangle(result, (left, top), (right, bottom),
                              (255, 255, 255),
                              thickness=-1)
                cv2.imwrite(folder_out + filename.split('.')[0] + '.jpg',
                            image)
        else:
            image = tools_image.desaturate(cv2.imread(folder_images +
                                                      filename),
                                           level=0.8)
            result = tools_YOLO.draw_objects_on_image(image, boxes,
                                                      [1] * len(boxes),
                                                      category_IDs, colors,
                                                      class_names)

        cv2.imwrite(folder_out + filename, result)
    return
def check_homography(H, W, rvec, tvec):

    image = numpy.full((H, W, 3), 64, dtype=numpy.uint8)
    landmarks_3d = numpy.array(
        [[-1, -1, -1], [-1, +1, -1], [+1, +1, -1], [+1, -1, -1], [-1, -1, 0],
         [-1, +1, +0], [+1, +1, +0], [+1, -1, +0]],
        dtype=numpy.float32)
    aperture_x, aperture_y = 0.5, 0.5
    mat_camera = tools_pr_geom.compose_projection_mat_3x3(
        image.shape[1], image.shape[0], aperture_x, aperture_y)
    landmarks_2d, jac = tools_pr_geom.project_points(
        landmarks_3d[[4, 5, 6, 7]], rvec, tvec, mat_camera, numpy.zeros(5))

    idx_selected = [4, 5, 6, 7]

    colors = tools_IO.get_colors(8)
    rvec2, tvec2, landmarks_2d_check = tools_pr_geom.fit_pnp(
        landmarks_3d[idx_selected], landmarks_2d, mat_camera)
    print('PNP R,T', rvec2, tvec2)
    for i, point in enumerate(landmarks_2d_check):
        cv2.circle(image, (point[0], point[1]),
                   5,
                   colors[idx_selected[i]].tolist(),
                   thickness=-1)
    cv2.imwrite(folder_out + 'check_pnp.png', image)

    landmarks_GT, lines_GT = soccer_data.get_GT()

    landmarks_GT[:, 0] /= (2000 / 2)
    landmarks_GT[:, 0] -= 1
    landmarks_GT[:, 1] /= (1500 / 2)
    landmarks_GT[:, 1] -= 1

    lines_GT[:, [0, 2]] /= (2000 / 2)
    lines_GT[:, [0, 2]] -= 1
    lines_GT[:, [1, 3]] /= (1500 / 2)
    lines_GT[:, [1, 3]] -= 1

    image = numpy.full((H, W, 3), 32, dtype=numpy.uint8)
    homography, result = tools_pr_geom.fit_homography(
        landmarks_GT[[19, 18, 0, 1]], landmarks_2d)
    for i, point in enumerate(result.astype(int)):
        cv2.circle(image, (point[0], point[1]),
                   5,
                   colors[idx_selected[i]].tolist(),
                   thickness=-1)
    cv2.imwrite(folder_out + 'check_homography.png', image)
    playground = draw_playground_homography(image,
                                            homography,
                                            landmarks_GT,
                                            lines_GT,
                                            w=1,
                                            R=4)
    cv2.imwrite(folder_out + 'check_playground.png', playground)

    Rs, Ts, Ns = homography_to_RT(homography, W, H)
    camera_matrix_3x3 = tools_pr_geom.compose_projection_mat_3x3(W, H)
    points_3d = numpy.array(landmarks_GT[[19, 18, 0, 1]])
    points_3d = numpy.hstack((points_3d, numpy.zeros((4, 1))))

    for (R, T, N) in zip(Rs, Ts, Ns):
        print('Decomp R,T\n', R, T, N)
        print()
        rotation = R
        translation = T
        normal = N
        points2d, jac = tools_pr_geom.project_points(points_3d, R, T,
                                                     camera_matrix_3x3,
                                                     numpy.zeros(5))

    #draw_playground_RT()

    return
Exemple #9
0
    def draw_nodes(self,
                   image_bg=None,
                   draw_thin_lines=False,
                   inverced=False,
                   valid_lengths=None,
                   valid_widths=None):
        if self.nodes is None:
            return

        color_white = (128, 128, 128)
        color_black = (32, 32, 32)
        color_edge = (0, 64, 255)

        if inverced:
            color_white, color_black = color_black, color_white

        if image_bg is None:
            image = numpy.zeros((self.H, self.W, 3), dtype=numpy.uint8)
            image[:, :] = color_white
        else:
            image = tools_image.saturate(image_bg.copy())

        pImage = Image.fromarray(image)
        draw = ImageDraw.Draw(pImage)

        dct = {}

        self.clasterize()
        n = tools_IO.max_element_by_value(self.dct_cluster_by_id)
        colors_cluster = tools_IO.get_colors(n[1] + 1, shuffle=True)

        for node in self.nodes:
            id1, x1, y1, w1 = node[0], node[1], node[2], node[3]
            #color_line = color_edge
            color_line = tuple(colors_cluster[self.dct_cluster_by_id[id1]])

            for nbr in node[4]:
                id2, x2, y2, w2 = self.nodes[nbr][0], self.nodes[nbr][
                    1], self.nodes[nbr][2], self.nodes[nbr][3]
                if (1000 * id1 + id2 not in dct) and (1000 * id2 + id1
                                                      not in dct):
                    dct[1000 * id1 + id2] = 1
                    dct[1000 * id2 + id1] = 1
                    w = int(min(w1, w2))
                    if image_bg is None:
                        if w % 2 == 1:
                            draw.line((x1 // 2, y1 // 2, x2 // 2, y2 // 2),
                                      fill=color_black,
                                      width=w)
                            draw.rectangle(
                                ((x1 - w) // 2, (y1 - w) // 2,
                                 (x1 + w) // 2 - 1, (y1 + w) // 2 - 1),
                                fill=color_black)
                            draw.rectangle(
                                ((x2 - w) // 2, (y2 - w) // 2,
                                 (x2 + w) // 2 - 1, (y2 + w) // 2 - 1),
                                fill=color_black)
                        else:
                            draw.line(
                                (x1 // 2, y1 // 2 - 1, x2 // 2, y2 // 2 - 1),
                                fill=color_black,
                                width=w)
                            draw.rectangle(
                                ((x1 - w) // 2, (y1 - w) // 2,
                                 (x1 + w) // 2 - 1, (y1 + w) // 2 - 1),
                                fill=color_black)
                            draw.rectangle(
                                ((x2 - w) // 2, (y2 - w) // 2,
                                 (x2 + w) // 2 - 1, (y2 + w) // 2 - 1),
                                fill=color_black)

                    if valid_widths is not None:
                        if w in valid_widths:
                            draw.line(
                                (x1 // 2, y1 // 2 - 1, x2 // 2, y2 // 2 - 1),
                                fill=color_line,
                                width=w)
                    elif draw_thin_lines:
                        draw.line((x1 // 2, y1 // 2 - 1, x2 // 2, y2 // 2 - 1),
                                  fill=color_line,
                                  width=1)

                        #draw.ellipse((x1 // 2 - 1, y1 // 2 - 1, x1 // 2 +1, y1 // 2 + 1 ), fill=color_line, width=1)
                        #draw.ellipse((x2 // 2 - 1, y2 // 2 - 1, x2 // 2 +1, y2 // 2 + 1 ), fill=color_line, width=1)

        image = numpy.array(pImage)
        del draw
        return image