示例#1
0
def align_two_images_homography(img1, img2,detector='SIFT',matchtype='knn'):

    img1_gray_rgb = tools_image.desaturate(img1)
    img2_gray_rgb = tools_image.desaturate(img2)

    points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector)
    points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector)

    match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc(points1, des1, points2, des2, matchtype)

    homography= None
    if match1.size != 0:
        homography= get_homography_by_keypoints_desc(points1, des1, points2, des2, matchtype)
        if homography is None:
            return img1,img2
    else:
        return img1, img2


    for each in match1:
        cv2.circle(img1_gray_rgb, (int(each[0]), int(each[1])), 3, [0, 0, 255],thickness=-1)
    for each in match2:
        cv2.circle(img2_gray_rgb, (int(each[0]), int(each[1])), 3, [255, 255, 0], thickness=-1)


    result_image1, result_image2 = get_stitched_images_using_homography(img2_gray_rgb, img1_gray_rgb, homography, borderMode=cv2.BORDER_REPLICATE,background_color=(255, 255, 255))
    q = cv2.matchTemplate(result_image1, result_image2, method=cv2.TM_CCOEFF_NORMED)[0, 0]
    q = int((1 + q) * 128)

    return result_image1,result_image2, q
示例#2
0
def demo_stereo_05_matchTemplate():
    folder_input = 'images/ex_stereo/'
    folder_output = 'images/output/'

    filenameL = '1L.png'
    filenameR = '1R.png'
    disp_v1, disp_v2, disp_h1, disp_h2 = -2, +2, -70, -10

    if not os.path.exists(folder_output):
        os.makedirs(folder_output)
    else:
        tools_IO.remove_files(folder_output)

    imgL = cv2.imread(folder_input + filenameL)
    imgR = cv2.imread(folder_input + filenameR)
    imgL_gray_rgb = tools_image.desaturate(imgL)
    imgR_gray_rgb = tools_image.desaturate(imgR)

    coord1, coord2, quality = tools_alg_match.get_best_matches(imgL,
                                                               imgR,
                                                               disp_v1,
                                                               disp_v2,
                                                               disp_h1,
                                                               disp_h2,
                                                               window_size=15,
                                                               step=5)

    dispL, dispR = tools_alg_match.get_disparity_from_matches(
        imgL.shape[0], imgL.shape[1], coord1, coord2, disp_v1, disp_v2,
        disp_h1, disp_h2)
    cv2.imwrite(folder_output + 'L_disp.png', dispL)
    cv2.imwrite(folder_output + 'R_disp.png', dispR)

    return
示例#3
0
def example_01_lines_ski(filename_in, folder_out):

    angle_bound1 = 30
    angle_bound2 =-30

    image = cv2.imread(filename_in)
    H,W = image.shape[:2]

    #skeleton = cv2.Canny(image, 20, 80)
    skeleton = Ske.binarized_to_skeleton_ski(Ske.binarize(image))

    lines_vert, weights_vert = Hough.get_lines_ski(skeleton, max_count=5, min_weight=50,the_range=Hough.get_angle_range(angle_bound1, 20, 1))
    lines_horz, weights_horz = Hough.get_lines_ski(skeleton, max_count=5, min_weight=50,the_range =Hough.get_angle_range(angle_bound2,20, 1))

    lines_vert = numpy.array([tools_render_CV.line_box_intersection(line, (0, 0, W - 1, H - 1)) for line in lines_vert])
    lines_horz = numpy.array([tools_render_CV.line_box_intersection(line, (0, 0, W - 1, H - 1)) for line in lines_horz])

    image_lines = 1 * tools_image.desaturate(image.copy())
    image_lines = draw_lines(image_lines, lines_horz, color=(0, 128, 255), w=4)
    image_lines = draw_lines(image_lines, lines_vert, color=(0, 0, 255), w=4)

    alpha = 0.5
    gray3d = tools_image.desaturate(image)
    result = alpha * image_lines + (1 - alpha) * gray3d
    cv2.imwrite(folder_out + 'skeleton.png', skeleton)
    cv2.imwrite(folder_out + 'lines.png', result)

    return
示例#4
0
def analyze_matches(folder_in,
                    filename_in,
                    folder_out,
                    disp_v1,
                    disp_v2,
                    disp_h1,
                    disp_h2,
                    window_size=5,
                    step=10):

    #if not os.path.exists(folder_out):
    #    os.makedirs(folder_out)
    #else:
    #    tools_IO.remove_files(folder_out)

    image1 = cv2.imread(folder_in + filename_in)
    pattern_width = 200

    print('Calculating matches ...')
    start_time = time.time()
    coord1, coord2, quality = tools_alg_match.get_best_matches(
        image1, image1, disp_v1, disp_v2, disp_h1, disp_h2, window_size, step)
    print('%s sec\n\n' % (time.time() - start_time))

    quality_image1 = tools_alg_match.interpolate_image_by_matches(
        image1.shape[0], image1.shape[1], coord2, quality)
    cv2.imwrite(folder_out + '%d-Q1.png' % window_size, quality_image1)
    quality_image2 = tools_alg_match.interpolate_image_by_matches(
        image1.shape[0], image1.shape[1], coord1, quality)
    cv2.imwrite(folder_out + '%d-Q2.png' % window_size, quality_image2)

    idx = numpy.where(quality >= 240)
    coord1, coord2 = coord1[idx], coord2[idx]

    idx = numpy.random.choice(coord1.shape[0], int(coord1.shape[0] * 0.3))
    gray1_rgb = tools_image.desaturate(image1)
    gray2_rgb = tools_image.desaturate(image1)
    gray1_rgb, gray2_rgb = tools_alg_match.visualize_matches_coord(
        coord1[idx, :], coord2[idx, :], gray1_rgb, gray2_rgb)
    cv2.imwrite(folder_out + '%d-points1.png' % window_size, gray1_rgb)
    cv2.imwrite(folder_out + '%d-points2.png' % window_size, gray2_rgb)

    blend1 = blend_by_coord(image1,
                            coord1,
                            coord2,
                            window_size,
                            fill_declines=False,
                            background_color=(128, 128, 128))
    blend2 = blend_by_coord(image1,
                            coord2,
                            coord1,
                            window_size,
                            fill_declines=False,
                            background_color=(128, 128, 128))
    cv2.imwrite(folder_out + '%d-blend1.png' % window_size, blend1)
    cv2.imwrite(folder_out + '%d-blend2.png' % window_size, blend2)

    return
示例#5
0
def example_find_matches_for_homography(detector='SIFT', matchtype='knn'):
	folder_input = './images/ex_keypoints/'
	img1 = cv2.imread(folder_input + 'left.jpg')
	img2 = cv2.imread(folder_input + 'rght.jpg')
	img1_gray_rgb = tools_image.desaturate(img1)
	img2_gray_rgb = tools_image.desaturate(img2)

	folder_output = './images/output/'

	if not os.path.exists(folder_output):
		os.makedirs(folder_output)
	#else:
	#	tools_IO.remove_files(folder_output)

	points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector)
	points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector)

	match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc(points1, des1, points2, des2, matchtype)
	homography = tools_calibrate.get_homography_by_keypoints_desc(points1, des1, points2, des2, matchtype)

	if homography is None:
		return

	match2_cand = cv2.perspectiveTransform(match1.reshape(-1, 1, 2).astype(numpy.float32), homography)

	hit = numpy.zeros(match1.shape[0])

	for i in range(0, match1.shape[0]):
		if numpy.linalg.norm(match2_cand[i] - match2[i]) <= 5:
			hit[i] = 1
			img1_gray_rgb = tools_draw_numpy.draw_circle(img1_gray_rgb, int(match1[i][1]), int(match1[i][0]), 3,[32, 192, 0])
			img2_gray_rgb = tools_draw_numpy.draw_circle(img2_gray_rgb, int(match2[i][1]), int(match2[i][0]), 3,[32, 192, 0])
		else:
			img1_gray_rgb = tools_draw_numpy.draw_circle(img1_gray_rgb, int(match1[i][1]), int(match1[i][0]), 3,[0, 64, 255])
			img2_gray_rgb = tools_draw_numpy.draw_circle(img2_gray_rgb, int(match2[i][1]), int(match2[i][0]), 3,[0, 64, 255])

	img2_gray_rgb, img1_gray_rgb = tools_calibrate.get_stitched_images_using_homography(img2_gray_rgb, img1_gray_rgb,homography)

	fig = plt.figure(figsize=(12, 6))
	fig.subplots_adjust(hspace=0.01)

	roc_auc = 0
	if hit.size >= 2:
		fpr, tpr, thresholds = metrics.roc_curve(hit, -distance)
		roc_auc = auc(fpr, tpr)
		filename_out = folder_output + ('_ROC_%s_%s_auc_%1.3f.png' % (detector, matchtype, roc_auc))
		tools_plot.plot_tp_fp(plt.subplot(1, 1, 1), fig, tpr, fpr, roc_auc)
		plt.savefig(filename_out)


	cv2.imwrite(folder_output + ('%s_%s_auc_%1.3f_left_matches.png' % (detector, matchtype, roc_auc)), img1_gray_rgb)
	cv2.imwrite(folder_output + ('%s_%s_auc_%1.3f_rght_matches.png' % (detector, matchtype, roc_auc)), img2_gray_rgb)

	return
示例#6
0
def example_03_find_homography_by_keypoints(detector='SIFT', matchtype='knn'):

    folder_input = 'images/ex_keypoints/'
    img1 = cv2.imread(folder_input + 'left.jpg')
    img2 = cv2.imread(folder_input + 'rght.jpg')

    img1_gray_rgb = tools_image.desaturate(img1)
    img2_gray_rgb = tools_image.desaturate(img2)

    folder_output = 'images/output/'
    output_filename1 = folder_output + 'left_transformed_homography.png'
    output_filename2 = folder_output + 'rght_transformed_homography.png'
    output_filename = folder_output + 'blended_homography.png'

    if not os.path.exists(folder_output):
        os.makedirs(folder_output)
    else:
        tools_IO.remove_files(folder_output)

    points1, des1 = tools_alg_match.get_keypoints_desc(img1, detector)
    points2, des2 = tools_alg_match.get_keypoints_desc(img2, detector)

    homography = tools_calibrate.get_homography_by_keypoints_desc(
        points1, des1, points2, des2, matchtype)
    match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc(
        points1, des1, points2, des2, matchtype)

    for each in match1:
        img1_gray_rgb = tools_draw_numpy.draw_circle(img1_gray_rgb,
                                                     int(each[1]),
                                                     int(each[0]), 3,
                                                     [0, 0, 255])
    for each in match2:
        img2_gray_rgb = tools_draw_numpy.draw_circle(img2_gray_rgb,
                                                     int(each[1]),
                                                     int(each[0]), 3,
                                                     [255, 255, 0])

    result_image1, result_image2 = tools_calibrate.get_stitched_images_using_homography(
        img1_gray_rgb,
        img2_gray_rgb,
        homography,
        background_color=(255, 255, 255))
    result_image = tools_image.blend_avg(result_image1,
                                         result_image2,
                                         background_color=(255, 255, 255))
    # result_image = tools_calibrate.blend_multi_band(result_image1, result_image2)

    cv2.imwrite(output_filename1, result_image1)
    cv2.imwrite(output_filename2, result_image2)
    cv2.imwrite(output_filename, result_image)
    return
示例#7
0
def demo_stereo_03_keypoints():
    folder_input = 'images/ex_stereo/'
    folder_output = 'images/output/'

    filenameL = '1L.png'
    filenameR = '1R.png'
    disp_v1, disp_v2, disp_h1, disp_h2 = 0, 1, -70, -10

    if not os.path.exists(folder_output):
        os.makedirs(folder_output)
    else:
        tools_IO.remove_files(folder_output)

    imgL = cv2.imread(folder_input + filenameL)
    imgR = cv2.imread(folder_input + filenameR)
    imgL_gray_rgb = tools_image.desaturate(imgL)
    imgR_gray_rgb = tools_image.desaturate(imgR)

    points1, des1 = tools_alg_match.get_keypoints_desc(imgL)
    points2, des2 = tools_alg_match.get_keypoints_desc(imgR)

    match1, match2, distance = tools_alg_match.get_matches_from_keypoints_desc(
        points1, des1, points2, des2)

    idx = []

    for i in range(0, match1.shape[0]):
        row1, col1 = match1[i, 1], match1[i, 0]
        row2, col2 = match2[i, 1], match2[i, 0]
        if (col2 - col1 >= disp_h1) and (col2 - col1 < disp_h2) and (
                row2 - row1 >= disp_v1) and (row2 - row1 < disp_v2):
            idx.append(i)

    match1, match2, distance = match1[idx], match2[idx], distance[idx]

    for i in range(0, match1.shape[0]):
        r = int(255 * numpy.random.rand())
        color = cv2.cvtColor(
            numpy.array([r, 255, 225], dtype=numpy.uint8).reshape(1, 1, 3),
            cv2.COLOR_HSV2BGR)
        imgL_gray_rgb = tools_draw_numpy.draw_circle(imgL_gray_rgb, match1[i,
                                                                           1],
                                                     match1[i, 0], 4, color)
        imgR_gray_rgb = tools_draw_numpy.draw_circle(imgR_gray_rgb, match2[i,
                                                                           1],
                                                     match2[i, 0], 4, color)

    cv2.imwrite(folder_output + 'matches03_L.png', imgL_gray_rgb)
    cv2.imwrite(folder_output + 'matches03_R.png', imgR_gray_rgb)

    return
示例#8
0
    def process_file_debug(self, filename_in, folder_out):
        image = cv2.imread(filename_in)

        image_resized = tools_image.smart_resize(image,
                                                 self.input_image_size[0],
                                                 self.input_image_size[1])
        image_resized = numpy.expand_dims(image_resized / 255.0, axis=0)

        u_boxes, u_scores, u_classes = detector_YOLO3_core.get_tensors_box_score_class_unfilter(
            self.model.output,
            self.anchors,
            self.anchor_mask,
            len(self.class_names),
            self.input_tensor_shape,
            score_threshold=0.01,
            iou_threshold=self.nms_threshold)

        boxes_yxyx, classes, scores = self.sess.run(
            [u_boxes, u_classes, u_scores],
            feed_dict={
                self.model.input: image_resized,
                self.input_tensor_shape: [image.shape[0], image.shape[1]],
                K.learning_phase(): 0
            })

        self.process_file(filename_in, folder_out + filename_in.split('/')[-1])

        total_image = tools_image.desaturate(image)

        for c in list(set(classes)):
            idx = numpy.where(classes == c)
            temp_image = tools_YOLO.draw_classes_on_image(
                tools_image.desaturate(image),
                boxes_yxyx[idx], [1] * len(idx[0]),
                self.colors[c],
                draw_score=False)
            total_image = tools_YOLO.draw_classes_on_image(total_image,
                                                           boxes_yxyx[idx],
                                                           scores[idx],
                                                           self.colors[c],
                                                           draw_score=True)
            cv2.imwrite(
                folder_out + 'class_%02d-%s-p%02d.png' %
                (c, self.class_names[c], 100 * scores[idx].max()), temp_image)

        cv2.imwrite(folder_out + 'all_boxes.png', total_image)

        return
示例#9
0
def get_proj_dist_mat_for_images(folder_in,
                                 chess_rows,
                                 chess_cols,
                                 folder_out=None):

    x, y = numpy.meshgrid(range(chess_rows), range(chess_cols))
    world_points = numpy.hstack(
        (x.reshape(chess_rows * chess_cols,
                   1), y.reshape(chess_rows * chess_cols, 1),
         numpy.zeros((chess_rows * chess_cols, 1)))).astype(numpy.float32)

    _3d_points = []
    _2d_points = []

    for image_name in fnmatch.filter(listdir(folder_in), '*.jpg'):
        im = cv2.imread(folder_in + image_name)
        im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        im_gray_RGB = tools_image.desaturate(im)
        ret, corners = cv2.findChessboardCorners(im_gray,
                                                 (chess_rows, chess_cols))

        if ret:
            corners = cv2.cornerSubPix(im_gray, corners, (11, 11), (-1, -1),
                                       (cv2.TERM_CRITERIA_EPS +
                                        cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
            _2d_points.append(corners)
            _3d_points.append(world_points)
            corners = corners.reshape(-1, 2)
            for i in range(0, corners.shape[0]):
                im_gray_RGB = tools_draw_numpy.draw_circle(im_gray_RGB,
                                                           corners[i, 1],
                                                           corners[i, 0],
                                                           3, [0, 0, 255],
                                                           alpha_transp=0)

        if folder_out != None:
            cv2.imwrite(folder_out + image_name, im_gray_RGB)

    camera_matrix = numpy.array([[im.shape[1], 0, im.shape[0]],
                                 [0, im.shape[0], im.shape[1]],
                                 [0, 0, 1]]).astype(numpy.float64)
    dist = numpy.zeros((1, 5))

    flag = cv2.CALIB_USE_INTRINSIC_GUESS + cv2.CALIB_ZERO_TANGENT_DIST + cv2.CALIB_RATIONAL_MODEL

    matrix_init = numpy.zeros((3, 3), numpy.float32)
    matrix_init[0][0] = im.shape[0] / 2
    matrix_init[0][2] = im.shape[1] / 2
    matrix_init[1][1] = matrix_init[0][0]
    matrix_init[1][2] = matrix_init[0][2]
    matrix_init[2][2] = 1.0
    dist_init = numpy.zeros((1, 4), numpy.float32)
    ret, camera_matrix, dist, rvecs, tvecs = cv2.calibrateCamera(
        _3d_points,
        _2d_points, (im.shape[1], im.shape[0]),
        matrix_init,
        dist_init,
        flags=flag)

    return camera_matrix, dist
示例#10
0
    def draw_landmarks(self, image):

        gray = tools_image.desaturate(image)
        landmarks = self.get_landmarks(image)
        if len(landmarks) != 68 or numpy.sum(landmarks) == 0:
            return gray

        del_triangles = Delaunay(landmarks).vertices
        for landmark in landmarks:
            cv2.circle(gray, (int(landmark[0]), int(landmark[1])), 2,
                       (0, 128, 255), -1)
            #gray=tools_draw_numpy.draw_circle(gray, landmark[1],landmark[0], 3, (0,128,255),alpha_transp=0.7)

        for t in del_triangles:
            p0 = (int(landmarks[t[0], 0]), int(landmarks[t[0], 1]))
            p1 = (int(landmarks[t[1], 0]), int(landmarks[t[1], 1]))
            p2 = (int(landmarks[t[2], 0]), int(landmarks[t[2], 1]))
            cv2.line(gray, p0, p1, (0, 0, 255))
            cv2.line(gray, p0, p2, (0, 0, 255))
            cv2.line(gray, p2, p1, (0, 0, 255))
            #gray = tools_draw_numpy.draw_line(gray, p0[1], p0[0], p1[1], p1[0], (0, 0, 255), alpha_transp=0.7)
            #gray = tools_draw_numpy.draw_line(gray, p0[1], p0[0], p2[1], p2[0], (0, 0, 255), alpha_transp=0.7)
            #gray = tools_draw_numpy.draw_line(gray, p2[1], p2[0], p1[1], p1[0], (0, 0, 255), alpha_transp=0.7)

        return gray
示例#11
0
def example_01(filename_in):

    sobel = numpy.zeros((8, 17), dtype=numpy.float32)
    sobel[:, sobel.shape[1] // 2:] = +1
    sobel[:, :sobel.shape[1] // 2] = -1
    sobel = sobel / sobel.sum()

    image = cv2.imread(filename_in)
    gray = cv2.imread(filename_in, 0)

    result = cv2.filter2D(gray, 0, sobel)
    result2 = numpy.maximum(255 - result, result)
    result2 = exposure.adjust_gamma(result2, 6)

    image_bin = Ske.binarize(result2)
    image_ske = Ske.binarized_to_skeleton(image_bin)
    segemnts = Ske.skeleton_to_segments(image_ske)

    colors = tools_draw_numpy.get_colors(len(segemnts), shuffle=True)
    image_segm = tools_draw_numpy.draw_segments(tools_image.desaturate(image),
                                                segemnts,
                                                colors,
                                                w=1)

    cv2.imwrite(folder_out + 'filtered.png', result)
    cv2.imwrite(folder_out + 'filtered2.png', result2)
    cv2.imwrite(folder_out + 'ske.png', image_ske)
    cv2.imwrite(folder_out + 'segm.png', image_segm)
    return
def example_calibrate_folder(folder_in, folder_out,marker_length_mm = 3.75, marker_space_mm = 0.5,dct = aruco.DICT_6X6_1000):
    tools_IO.remove_files(folder_out)

    num_cols, num_rows = 4, 5
    board = aruco.GridBoard_create(num_cols, num_rows, marker_length_mm, marker_space_mm,aruco.getPredefinedDictionary(dct))
    filenames = numpy.unique(tools_IO.get_filenames(folder_in, '*.jpg,*.png'))[:3]

    counter, corners_list, id_list, first = [], [], [], True
    for filename_in in filenames:
        base_name, ext = filename_in.split('/')[-1].split('.')[0], filename_in.split('/')[-1].split('.')[1]
        image = cv2.imread(folder_in + filename_in)
        img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
        corners, ids, rejectedImgPoints = aruco.detectMarkers(img_gray, aruco.getPredefinedDictionary(dct))
        if first == True:
            corners_list = corners
            id_list = ids
            first = False
        else:
            corners_list = numpy.vstack((corners_list, corners))
            id_list = numpy.vstack((id_list,ids))
        counter.append(len(ids))

        image_temp = tools_image.desaturate(image.copy())
        aruco.drawDetectedMarkers(image_temp, corners)
        cv2.imwrite(folder_out+base_name+'.png',image_temp)
        print(base_name)


    counter = numpy.array(counter)
    ret, camera_matrix, dist, rvecs, tvecs = aruco.calibrateCameraAruco(corners_list, id_list, counter, board, img_gray.shape, None, None )

    print(camera_matrix)

    return
示例#13
0
    def process_file(self, filename_in, filename_out,draw_spline=True):

        if not os.path.isfile(filename_in):
            return []

        image = cv2.imread(filename_in)
        image = tools_image.rgb2bgr(image)
        if image is None:
            return []

        if draw_spline==False:
            boxes_yxyx, scores, classes  = self.process_image(image)
            markup = tools_YOLO.get_markup(filename_in, boxes_yxyx, scores, classes)
            tools_YOLO.draw_and_save(filename_out, image, boxes_yxyx, scores, classes,self.colors, self.class_names)
        else:
            r = self.model.detect([image], verbose=0)[0]
            boxes_yxyx, scores, classes,masks = r['rois'], r['scores'], r['class_ids'],r['masks']
            class_ID = tools_IO.smart_index(self.class_names,'person')[0]
            msk = (scores > self.obj_threshold).tolist()

            #msk = msk and (classes == class_ID)

            msk = numpy.array(msk)
            boxes_yxyx, scores, classes, masks = boxes_yxyx[msk], scores[msk], classes[msk], masks[:,:,msk]
            markup = tools_YOLO.get_markup(filename_in, boxes_yxyx, scores, classes)

            detector_Mask_RCNN_core.display_instances(tools_image.desaturate(image), boxes_yxyx, masks, classes, self.class_names,filename_out,scores,self.colors)

        return markup
示例#14
0
def merge_images_in_folders(path_input1,
                            path_input2,
                            path_output,
                            mask='*.png,*.jpg',
                            rotate_first=False):
    tools_IO.remove_files(path_output, create=True)

    fileslist1 = tools_IO.get_filenames(path_input1, mask)
    fileslist2 = tools_IO.get_filenames(path_input2, mask)

    fileslist1.sort()
    fileslist2.sort()

    for filename1, filename2 in zip(fileslist1, fileslist2):
        image1 = cv2.imread(path_input1 + filename1)
        image2 = cv2.imread(path_input2 + filename2)
        if image1 is None or image2 is None: continue

        if rotate_first:
            image1 = numpy.transpose(image1, [1, 0, 2])

        shape1 = image1.shape
        shape2 = image2.shape

        image2_resized = cv2.resize(
            image2, (int(shape1[0] * shape2[1] / shape2[0]), shape1[0]))
        image = numpy.zeros(
            (shape1[0], shape1[1] + image2_resized.shape[1], shape1[2]),
            dtype=numpy.uint8)

        image[:, :shape1[1]] = tools_image.desaturate(image1)
        image[:, shape1[1]:] = image2_resized
        cv2.imwrite(path_output + filename1, image)

    return
示例#15
0
def example_board_pose_estimation(folder_input, folder_output, chess_rows,
                                  chess_cols, cameraMatrix, dist):

    if not os.path.exists(folder_output):
        os.makedirs(folder_output)
    else:
        tools_IO.remove_files(folder_output)

    R = 1

    axis_3d_end = numpy.array([[R, 0, 0], [0, R, 0], [0, 0, -R]],
                              dtype=numpy.float32)
    axis_3d_start = numpy.array([[0, 0, 0]], dtype=numpy.float32)

    points_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)
    points_3d[:, [0, 1, 2]] *= 0.5
    points_3d[:, [0, 1, 2]] += 0.5
    points_3d[:, [2]] *= -1

    points_3d[:, [0]] += 3
    points_3d[:, [1]] += 4

    for image_name in fnmatch.filter(listdir(folder_input), '*.jpg'):

        img = cv2.imread(folder_input + image_name)
        image_AR = tools_image.desaturate(img)
        rvecs, tvecs = tools_calibrate.pose_estimation_chessboard(
            img, chess_rows, chess_cols, cameraMatrix, dist)

        if rvecs.size != 0:

            axis_2d_end, jac = cv2.projectPoints(axis_3d_end, rvecs, tvecs,
                                                 cameraMatrix, dist)
            axis_2d_start, jac = cv2.projectPoints(axis_3d_start, rvecs, tvecs,
                                                   cameraMatrix, dist)
            cv2.line(image_AR,
                     (axis_2d_start[0, 0, 0], axis_2d_start[0, 0, 1]),
                     (axis_2d_end[0, 0, 0], axis_2d_end[0, 0, 1]), (0, 0, 255),
                     thickness=3)
            cv2.line(image_AR,
                     (axis_2d_start[0, 0, 0], axis_2d_start[0, 0, 1]),
                     (axis_2d_end[1, 0, 0], axis_2d_end[1, 0, 1]), (0, 255, 0),
                     thickness=3)
            cv2.line(image_AR,
                     (axis_2d_start[0, 0, 0], axis_2d_start[0, 0, 1]),
                     (axis_2d_end[2, 0, 0], axis_2d_end[2, 0, 1]), (255, 0, 0),
                     thickness=3)
            #image_AR = tools_render_CV.draw_cube_numpy(image_AR, camera_matrix, numpy.zeros(4), rvecs.flatten(), tvecs.flatten(),(0.5, 0.5, 0.5),points_3d=points_3d)
            image_AR = tools_render_CV.draw_compass(
                image_AR, camera_matrix, dist,
                numpy.array(rvecs).flatten(),
                numpy.array(tvecs).flatten(), R * 5)

        cv2.imwrite(folder_output + image_name, image_AR)

    return
def test_cascade():
    image = tools_image.desaturate(cv2.imread(filename_in))
    objects, rejectLevels, levelWeights = \
        object_detector.detectMultiScale3(image, scaleFactor=1.05, minSize=(20, 20), outputRejectLevels=True)
    for (x, y, w, h) in objects:cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.imwrite(filename_out,image)

    return
示例#17
0
def draw_and_save(filename_out, image, boxes_yxyx, scores, classes, colors,
                  class_names):
    if filename_out is not None:
        res_image = draw_objects_on_image(tools_image.desaturate(image, 1.0),
                                          boxes_yxyx, scores, classes, colors,
                                          class_names)
        cv2.imwrite(filename_out, res_image)
    return
示例#18
0
def example_shi_tomasi_corner_detector(filename_in, filename_out, R=2):
	img = cv2.imread(filename_in)
	gray_rgb = tools_image.desaturate(img)

	for each in tools_alg_match.get_corners_Shi_Tomasi(img):
		gray_rgb = tools_draw_numpy.draw_circle(gray_rgb, each[1], each[0], R, [0, 0, 255])

	cv2.imwrite(filename_out, gray_rgb)
	return
示例#19
0
def demo_stereo_04_keypoints_limited():
    folder_input = 'images/ex_stereo/'
    folder_output = 'images/output/'

    filenameL = '1L.png'
    filenameR = '1R.png'
    disp_v1, disp_v2, disp_h1, disp_h2 = 0, 1, -70, -10

    if not os.path.exists(folder_output):
        os.makedirs(folder_output)
    else:
        tools_IO.remove_files(folder_output)

    imgL = cv2.imread(folder_input + filenameL)
    imgR = cv2.imread(folder_input + filenameR)
    imgL_gray_rgb = tools_image.desaturate(imgL)
    imgR_gray_rgb = tools_image.desaturate(imgR)

    points1, des1 = tools_alg_match.get_keypoints_desc(imgL)
    points2, des2 = tools_alg_match.get_keypoints_desc(imgR)

    match1, match2, distance = tools_alg_match.get_matches_from_desc_limit_by_disp(
        points1, des1, points2, des2, disp_v1, disp_v2, disp_h1, disp_h2,
        'ccc')

    R = 4

    for i in range(0, match1.shape[0]):
        r = int(255 * numpy.random.rand())
        color = cv2.cvtColor(
            numpy.array([r, 255, 225], dtype=numpy.uint8).reshape(1, 1, 3),
            cv2.COLOR_HSV2BGR)
        imgL_gray_rgb = tools_draw_numpy.draw_circle(imgL_gray_rgb, match1[i,
                                                                           1],
                                                     match1[i, 0], R, color)
        imgR_gray_rgb = tools_draw_numpy.draw_circle(imgR_gray_rgb, match2[i,
                                                                           1],
                                                     match2[i, 0], R, color)

    cv2.imwrite(folder_output + 'matches03_L.png', imgL_gray_rgb)
    cv2.imwrite(folder_output + 'matches03_R.png', imgR_gray_rgb)

    return
示例#20
0
def example_ORB(filename_in, filename_out, R=2):
	img = cv2.imread(filename_in)
	gray_rgb = tools_image.desaturate(img)

	points, desc = tools_alg_match.get_keypoints_desc(img, detector='ORB')

	for each in points:
		gray_rgb = tools_draw_numpy.draw_circle(gray_rgb, each[1], each[0], R, [0, 0, 255])

	cv2.imwrite(filename_out, gray_rgb)
	return
示例#21
0
def example_Fast_Corner_Detector(filename_in, filename_out, R=2):
	img = cv2.imread(filename_in)
	gray_rgb = tools_image.desaturate(img)

	points = tools_alg_match.get_corners_Fast(img)

	for each in points:
		gray_rgb = tools_draw_numpy.draw_circle(gray_rgb, each[1], each[0], R, [0, 0, 255])

	cv2.imwrite(filename_out, gray_rgb)
	return
示例#22
0
    def segments_to_ellipse(self, image, segments, do_debug=False):

        self.H, self.W = image.shape[:2]
        weights = numpy.array([len(s) for s in segments], dtype=numpy.float)
        weights = weights / weights.sum()

        Q, E, Cands = {}, {}, {}
        for s1 in range(len(segments) - 1):
            for s2 in range(s1 + 1, len(segments)):
                ellipse, idx_match = self.estimate_ellipse(segments, s1, s2)
                if ellipse is None: continue

                E[(s1, s2)] = ellipse
                Q[(s1, s2)] = weights[idx_match].sum()
                Cands[(s1, s2)] = idx_match

                if do_debug:
                    image_debug = tools_image.desaturate(image)
                    image_debug = self.draw_segments(image_debug,
                                                     [segments[s1]],
                                                     color=(90, 0, 255),
                                                     w=4)
                    image_debug = self.draw_segments(image_debug,
                                                     [segments[s2]],
                                                     color=(0, 90, 255),
                                                     w=4)

                    center = (int(ellipse[0][0]), int(ellipse[0][1]))
                    axes = (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2))
                    rotation_angle = ellipse[2]
                    cv2.ellipse(image_debug,
                                center,
                                axes,
                                rotation_angle,
                                startAngle=0,
                                endAngle=360,
                                color=(0, 0, 190),
                                thickness=1)
                    cv2.imwrite(
                        self.folder_out + 'ellips_%03d_%03d.png' % (s1, s2),
                        image_debug)

        ellipse, cands = None, None
        if len(Q) > 0:
            key = tools_IO.max_element_by_value(Q)[0]
            cands = Cands[key]
            #ellipse = E[key]
            ellipse = self.refine_ellipse(segments, cands)

        return ellipse, cands


# ----------------------------------------------------------------------------------------------------------------
示例#23
0
    def draw_face(self,image):
        gray = tools_image.desaturate(image)
        objects = self.detector(gray)
        if len(objects) == 1:
            for face in objects:
                xmin = face.left()
                ymin = face.top()
                xmax = face.right()
                ymax = face.bottom()
                cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)

        return image
示例#24
0
    def detect_face(self, image):
        gray = tools_image.desaturate(image)
        objects = self.detector(gray)
        if len(objects) == 1:
            for face in objects:
                xmin = face.left()
                ymin = face.top()
                xmax = face.right()
                ymax = face.bottom()
                return image[ymin:ymax, xmin:xmax, :]

        return None
示例#25
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
示例#26
0
    def get_landmarks(self, image):
        res = numpy.zeros((68, 2), dtype=numpy.float)
        if image is None:
            return res
        gray = tools_image.desaturate(image)
        objects = self.detector(gray)

        if len(objects) == 1:
            landmarks = self.predictor(gray, objects[0])
            res = []
            for n in range(0, 68):
                res.append([landmarks.part(n).x, landmarks.part(n).y])
            res = numpy.array(res)

        return res.astype(numpy.float)
示例#27
0
    def draw_landmarks_v2(self, image,landmarks,del_triangles=None,color=(0, 128, 255)):

        gray = tools_image.desaturate(image,level=0)

        for landmark in landmarks:
            cv2.circle(gray, (int(landmark[0]), int(landmark[1])), 2, color, -1)

        if del_triangles is not None:
            for t in del_triangles:
                p0 = (int(landmarks[t[0], 0]), int(landmarks[t[0], 1]))
                p1 = (int(landmarks[t[1], 0]), int(landmarks[t[1], 1]))
                p2 = (int(landmarks[t[2], 0]), int(landmarks[t[2], 1]))
                cv2.line(gray, p0, p1, (0, 0, 255))
                cv2.line(gray, p0, p2, (0, 0, 255))
                cv2.line(gray, p2, p1, (0, 0, 255))

        return gray
示例#28
0
def demo_aruco(image=None):




    marker_length  = 0.5

    if USE_CAMERA:
        cap = cv2.VideoCapture(0)
    else:
        cap = []
        frame = image.copy()
        camera_matrix = numpy.array([[frame.shape[0], 0, frame.shape[1]], [0, frame.shape[0], frame.shape[1]], [0, 0, 1]]).astype(numpy.float64)
        dist = numpy.zeros((1, 5))



    aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)

    while (True):
        if USE_CAMERA:
            ret, frame = cap.read()
            camera_matrix = numpy.array([[frame.shape[0], 0, frame.shape[1]], [0, frame.shape[0] , frame.shape[1] ], [0, 0, 1]]).astype(numpy.float64)
            dist = numpy.zeros((1, 5))

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray_rgb = tools_image.desaturate(frame)
        corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict)

        if len(corners) > 0:
            gray_rgb = aruco.drawDetectedMarkers(gray_rgb, corners)
            for each in corners:
                rvec, tvec, _ = aruco.estimatePoseSingleMarkers(each, marker_length, camera_matrix, dist)
                aruco.drawAxis(gray_rgb, camera_matrix, dist, rvec[0], tvec[0], marker_length/2)

        cv2.imshow('frame', gray_rgb)
        if cv2.waitKey(1) & 0xFF == 27:
            break

    if USE_CAMERA:
        cap.release()

    cv2.destroyAllWindows()
    cv2.imwrite(filename_out,gray_rgb)

    return
示例#29
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
示例#30
0
def get_proj_dist_mat_for_images(folder_in,chess_rows,chess_cols,cell_size=1,folder_out=None):

    x, y = numpy.meshgrid(range(chess_rows), range(chess_cols))
    world_points = numpy.hstack((x.reshape(chess_rows*chess_cols, 1), y.reshape(chess_rows*chess_cols, 1), numpy.zeros((chess_rows*chess_cols, 1)))).astype(numpy.float32)

    _3d_points = []
    _2d_points = []


    for local_filename in tools_IO.get_filenames(folder_in,'*.png,*.jpg'):
        image = cv2.imread(folder_in+local_filename)
        #image = tools_image.do_rescale(image, 0.5)
        gray = tools_image.desaturate(image)
        ret, corners = cv2.findChessboardCorners(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), (chess_rows, chess_cols),cv2.CALIB_CB_ADAPTIVE_THRESH)

        if ret:
            corners = cv2.cornerSubPix(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), corners, (11, 11), (-1, -1),(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
            _2d_points.append(corners)
            _3d_points.append(world_points)
            corners = corners.reshape(-1, 2)
            for i in range(0,corners.shape[0]):gray = tools_draw_numpy.draw_circle(gray, corners[i, 1], corners[i, 0], 3, [0, 0, 255], alpha_transp=0)

        print(local_filename,len(corners))
        if folder_out!=None:
            cv2.imwrite(folder_out + local_filename, gray)

    #camera_matrix = numpy.array([[im.shape[1], 0, im.shape[0]], [0, im.shape[0], im.shape[1]], [0, 0, 1]]).astype(numpy.float64)
    #dist=numpy.zeros((1,5))

    flag = cv2.CALIB_USE_INTRINSIC_GUESS + cv2.CALIB_ZERO_TANGENT_DIST + cv2.CALIB_RATIONAL_MODEL

    matrix_init = numpy.zeros((3, 3), numpy.float32)
    matrix_init[0][0] = image.shape[1]
    matrix_init[1][1] = image.shape[0]
    matrix_init[0][2] = matrix_init[0][0]/2
    matrix_init[1][2] = matrix_init[1][1]/2
    matrix_init[2][2] = 1.0
    dist_init = numpy.zeros((1, 4), numpy.float32)
    ret, camera_matrix, dist, rvecs, tvecs = cv2.calibrateCamera(1.0 * numpy.array(_3d_points), _2d_points,(image.shape[1], image.shape[0]), matrix_init,dist_init, flags=flag)
    print(camera_matrix)
    ret, camera_matrix, dist, rvecs, tvecs = cv2.calibrateCamera(cell_size*numpy.array(_3d_points), _2d_points, (image.shape[1], image.shape[0]), matrix_init, dist_init,flags=flag)

    return camera_matrix,dist,rvecs, tvecs