示例#1
0
def main(num=18, name='prtn', focal_name='pano.txt', output_name='out.jpg'):
    imgs_cylindral = CC(num, name, focal_name)
    #convert to cylindral coordinate
    imgs_cylindral_gray = ToGray(imgs_cylindral)  #to gray scale
    total_shift_list = []
    panorama_h_len = 0
    panorama_w_len = 0
    for i in xrange(len(imgs_cylindral_gray) - 1):  #pairwise matching
        feature_points1 = feature.harris_detector(imgs_cylindral_gray[i])
        feature_description1 = feature.harris_descriptor(
            imgs_cylindral_gray[i], feature_points1)
        feature_points2 = feature.harris_detector(imgs_cylindral_gray[i + 1])
        feature_description2 = feature.harris_descriptor(
            imgs_cylindral_gray[i + 1], feature_points2)
        matches = feature.matching(feature_description1, feature_description2)
        match_points1 = feature_points1[matches[:, 0],
                                        0:2]  #only xy coordinate
        match_points2 = feature_points2[matches[:, 1], 0:2]
        match_list = np.hstack((match_points1, match_points2))
        [shift_x, shift_y, matches_ransac] = RANSAC(match_list)
        total_shift_list.append([shift_x, shift_y])
    total_shift_list = np.array(total_shift_list).astype(int)
    print 'total_shift_list size:', len(total_shift_list)  #should be num - 1
    print total_shift_list  #shift_x is horizontal direction
    panorama_h_len = imgs_cylindral[-1].shape[0]
    panorama_w_len = imgs_cylindral[-1].shape[1]
    '''
    start stitch
    '''
    panorama_w_len += int(np.ceil(np.sum(total_shift_list, axis=0)[0]))
    panorama_h_len += abs(int(2 *
                              np.ceil(np.sum(total_shift_list, axis=0)[1])))
    print 'start stitching, panorama size(h,w):', panorama_h_len, panorama_w_len
    panorama = np.zeros(
        (panorama_h_len, panorama_w_len, 3))  #determine size of panorama
    accumulate_shift_w = 0
    accumulate_shift_h = -abs(int(np.ceil(np.sum(total_shift_list,
                                                 axis=0)[1])))
    overlap_width = 0
    #test = np.zeros((800,800,3))
    for i in xrange(len(imgs_cylindral)):
        if i >= 1:
            overlap_width += imgs_cylindral[i - 1].shape[1] - total_shift_list[
                i - 1][0]
        for j in xrange(int(imgs_cylindral[i].shape[0])):
            for k in xrange(int(imgs_cylindral[i].shape[1])):
                if np.array_equal(
                        panorama[accumulate_shift_h - j,
                                 accumulate_shift_w - k], np.zeros(3)):
                    panorama[accumulate_shift_h - j, \
                             accumulate_shift_w - k, :] = imgs_cylindral[i][-j, -k, :]
                    '''
                    if i == 1:
                        test[-j, -k, :] = imgs_cylindral[i][-j, -k, :]
                    '''
                elif np.array_equal(imgs_cylindral[i][-j, -k], np.zeros(3)):
                    continue
                else:
                    if overlap_width == 0:
                        print i, k, overlap_width
                        assert overlap_width != 0
                    t = k / float(overlap_width)
                    if not t <= 1:
                        print i, k, overlap_width
                        assert t <= 1
                    panorama[accumulate_shift_h - j, \
                             accumulate_shift_w - k, :] = t*imgs_cylindral[i][-j, -k, :] + (1-t)*panorama[accumulate_shift_h - j, accumulate_shift_w - k, :]
                    #test[-j, -k, :] = t*imgs_cylindral[i][-j, -k, :]
                    #print k, overlap_width, t
        if i == len(imgs_cylindral) - 1:
            break
        accumulate_shift_h -= total_shift_list[i][
            1]  #I think that vertical shift should be less than 4 pixels
        accumulate_shift_w -= total_shift_list[i][0]

    imsave(output_name, panorama)
    #imsave('t.jpg', test)
    print 'finish'
            corner_response2,
            kernel=const.DESCRIPTOR_SIZE,
            threshold=const.FEATURE_THRESHOLD)
        print(str(len(descriptors2)) + ' features extracted.')

        cache_feature = [descriptors2, position2]

        if const.DEBUG:
            cv2.imshow('cr1', corner_response1)
            cv2.imshow('cr2', corner_response2)
            cv2.waitKey(0)

        print(' - Feature matching .... ', end='', flush=True)
        matched_pairs = feature.matching(descriptors1,
                                         descriptors2,
                                         position1,
                                         position2,
                                         pool,
                                         y_range=const.MATCHING_Y_RANGE)
        print(str(len(matched_pairs)) + ' features matched.')

        if const.DEBUG:
            utils.matched_pairs_plot(img1, img2, matched_pairs)

        print(' - Finding best shift using RANSAC .... ', end='', flush=True)
        shift = stitch.RANSAC(matched_pairs, shifts[-1])
        shifts += [shift]
        print('best shift ', shift)

        print(' - Stitching image .... ', end='', flush=True)
        stitched_image = stitch.stitching(stitched_image,
                                          img2,