Example #1
0
def main():
    global name, scale_factor, SAVE_DIR

    img_left = cv2.imread(args.input_left)
    img_right = cv2.imread(args.input_right)

    max_disp = check_file(img_left, img_right)
    print(max_disp)
    name = 'test'

    SAVE_DIR = "./real_result"
    print(SAVE_DIR)
    scale_factor = 4
    labels = computeDisp(img_left, img_right, max_disp)
    labels = labels.astype(np.float32)
    if DEBUG:
        writePFM(args.output, labels)
    else:
        writePFM(args.output, labels)

    if DEBUG:
        with open(os.path.join(SAVE_DIR, 'time_stamp.json'.format(name)), 'w') as f:
            json.dump(TIME, f, indent=4, sort_keys=False)
    else:
        with open('time_stamp.json', 'w') as f:
            json.dump(TIME, f, indent=4, sort_keys=False)
Example #2
0
def main():

    print('output file path:', args.output)
    print('Compute disparity for %s' % args.input_left)

    isgray = is_gray_img(args.input_left)

    if isgray:
        imgL_o = cv2.cvtColor(cv2.imread(args.input_left, 0),
                              cv2.COLOR_GRAY2RGB)
        imgR_o = cv2.cvtColor(cv2.imread(args.input_right, 0),
                              cv2.COLOR_GRAY2RGB)
    else:
        imgL_o = cv2.imread(args.input_left)
        imgR_o = cv2.imread(args.input_right)

    imgL_o = cv2.cvtColor(imgL_o, cv2.COLOR_BGR2GRAY)
    imgR_o = cv2.cvtColor(imgR_o, cv2.COLOR_BGR2GRAY)

    imgL_o = cv2.cvtColor(imgL_o, cv2.COLOR_GRAY2RGB)
    imgR_o = cv2.cvtColor(imgR_o, cv2.COLOR_GRAY2RGB)

    tic = time.time()
    disp = np.float32(computeDisp(imgL_o, imgR_o))
    toc = time.time()

    print('dispmap: {}'.format(disp))
    writePFM(args.output, disp)
    print('Elapsed time: %f sec.' % (toc - tic))
Example #3
0
def main():
    total_loss = 0
    for batch_idx, (imgL, imgR, disp_L) in enumerate(TestImgLoader):
        pred_disp = test(imgL, imgR)
        disp_GT = disp_L.cpu().numpy().squeeze(0)
        loss = cal_avgerr(disp_GT, pred_disp)
        total_loss += loss
        print(loss)
        writePFM('output/TL' + str(batch_idx) + '.pfm', pred_disp)
    print(total_loss / 10)
Example #4
0
def main():
    args = parser.parse_args()

    print(args.output)
    print('Compute disparity for %s' % args.input_left)
    img_left = cv2.imread(args.input_left)
    img_right = cv2.imread(args.input_right)
    tic = time.time()
    disp = computeDisp(args, img_left, img_right)
    toc = time.time()
    writePFM(args.output, disp)
    print('Elapsed time: %f sec.' % (toc - tic))
Example #5
0
def main():
    DEBUG = True
    args = parser.parse_args()

    print(args.output)
    print("Compute disparity for %s" % args.input_left)
    img_left = cv2.imread(args.input_left)
    img_right = cv2.imread(args.input_right)
    tic = time.time()
    disp = computeDisp(img_left, img_right)
    toc = time.time()
    writePFM(args.output, disp)
    print("Elapsed time: %f sec." % (toc - tic))
Example #6
0
def main():
    processed = preprocess.get_transform(augment=False)
    padsize = 64
    if args.isgray:
        imgL_o = cv2.cvtColor(cv2.imread(args.leftimg,0), cv2.COLOR_GRAY2RGB)
        imgR_o = cv2.cvtColor(cv2.imread(args.rightimg,0), cv2.COLOR_GRAY2RGB)
    else:
        # imgL_o = cv2.cvtColor(cv2.imread(args.leftimg), cv2.COLOR_BGR2RGB)
        # imgR_o = cv2.cvtColor(cv2.imread(args.rightimg), cv2.COLOR_BGR2RGB)
        imgL_o = skimage.io.imread(args.leftimg)
        imgR_o = skimage.io.imread(args.rightimg)
        # imgL_o = Image.open(args.leftimg)
        # imgR_o = Image.open(args.rightimg)
    if real:
        imgL_o = np.pad(imgL_o,((0,0),(padsize,0),(0,0)))
        imgR_o = np.pad(imgR_o,((0,0),(0,padsize),(0,0)))
    imgL = processed(imgL_o).numpy()
    imgR = processed(imgR_o).numpy()
    imgL = np.reshape(imgL,[1,3,imgL.shape[1],imgL.shape[2]])
    imgR = np.reshape(imgR,[1,3,imgR.shape[1],imgR.shape[2]])

    # pad to width and hight to 16 times
    if imgL.shape[2] % 16 != 0:
        times = imgL.shape[2]//16       
        top_pad = (times+1)*16 -imgL.shape[2]
    else:
        top_pad = 0
    if imgL.shape[3] % 16 != 0:
        times = imgL.shape[3]//16                       
        left_pad = (times+1)*16-imgL.shape[3]
    else:
        left_pad = 0     
    imgL = np.lib.pad(imgL,((0,0),(0,0),(top_pad,0),(0,left_pad)),mode='constant',constant_values=0)
    imgR = np.lib.pad(imgR,((0,0),(0,0),(top_pad,0),(0,left_pad)),mode='constant',constant_values=0)

    start_time = time.time()
    pred_disp = test(imgL,imgR)


    print('time = %.2f' %(time.time() - start_time))
    if top_pad !=0 or left_pad != 0:
        img = pred_disp[top_pad:,:]
    else:
        img = pred_disp

    img = img[:,padsize:]
    writePFM(args.output, img.astype(np.float32))
Example #7
0
def main():
    total_loss = 0
    for batch_idx, (imgL, imgR, disp_L) in enumerate(TestImgLoader):
        pred_disp = 0
        min_loss = 10
        disp_GT = disp_L.cpu().numpy().squeeze(0)
        for i in range(len(epoch_list)):
            model = model_list[i]
            temp_disp = test(imgL, imgR, model)
            loss = cal_avgerr(disp_GT, temp_disp)
            if loss < min_loss:
                pred_disp = temp_disp
                min_loss = loss
        total_loss += min_loss
        print(min_loss)
        writePFM('output/TL' + str(batch_idx) + '.pfm', pred_disp)
    print('total loss: ', total_loss / 10)
Example #8
0
def main():
    args = parser.parse_args()

    print('Compute disparity for %s' % args.input_left)
    # read images
    img_left = cv2.imread(args.input_left,cv2.IMREAD_GRAYSCALE).astype(np.float32)
    img_right = cv2.imread(args.input_right,cv2.IMREAD_GRAYSCALE).astype(np.float32)
        
    # start computing disparity map
    tic = time.time()
    disp = computeDisp(img_left, img_right)
    toc = time.time()
    """
    plt.figure(0)
    plt.imshow(disp,cmap='gray')
    plt.show()"""
    
    writePFM(args.output, disp.astype('float32'))
    print('Elapsed time: %f sec.' % (toc - tic))
Example #9
0
def computeDisp(Il, Ir):
    h, w, ch = Il.shape
    disp = np.zeros((h, w), dtype=np.int32)

    # TODO: Some magic
    if if_syn:
        print('Synthesis')

        cmd = 'python submission.py --maxdisp 192 --model stackhourglass --KITTI cv --left_datapath ' + args.input_left + ' --right_datapath ' + args.input_right + ' --output ' + "ori_" + args.output + ' --loadmodel synthesis_tune_best_for_end.tar'
        print(cmd)
        retcode = subprocess.call(cmd, shell=True)
        print(retcode)

        cmd = 'python submission.py --maxdisp 192 --model stackhourglass --KITTI cv --left_datapath ' + args.input_left + ' --right_datapath ' + args.input_right + ' --output ' + "resolu_" + args.output + ' --loadmodel aug_resolutionfinetune_146.tar'
        print(cmd)
        retcode = subprocess.call(cmd, shell=True)
        print(retcode)

        cmd = 'python submission.py --maxdisp 192 --model stackhourglass --KITTI cv --left_datapath ' + args.input_left + ' --right_datapath ' + args.input_right + ' --output ' + "color_" + args.output + ' --loadmodel aug_colorfinetune_169.tar'
        print(cmd)
        retcode = subprocess.call(cmd, shell=True)
        print(retcode)

        p1 = readPFM("ori_" + args.output)
        p2 = readPFM("resolu_" + args.output)
        p3 = readPFM("color_" + args.output)
        final_pfm = (p1 + p2 + p3) / 3
        writePFM(args.output, final_pfm)

    else:
        print('Real')
        scale_factor = getlrdisp(Il, Ir)
        print(scale_factor)
        #if scale_factor in [14,56,75]:
        #    cmd = 'python cencus_quick.py '+ args.input_left +' '+args.input_right+' '+ args.output + ' ' +
        cmd = 'python cencus_transform.py ' + args.input_left + ' ' + args.input_right + ' ' + args.output
        print(cmd)
        retcode = subprocess.call(cmd, shell=True)
        print(retcode)

    return disp
Example #10
0
def main():
    
    parser = argparse.ArgumentParser(description='Disparity Estimation')
    parser.add_argument('--inputDir', default='./data', type=str, help='input data folder')
    parser.add_argument('--outputDir', default='./output', type=str, help='output folder')
    parser.add_argument('--dispModel', default='./pretrained_model_KITTI2012.tar', type=str, help='disparity model')
    parser.add_argument('--classifier', default='./classifier.pth', type=str, help='disparity model')
    args = parser.parse_args()
    
    # check dir
    out_syn_path = os.path.join(args.outputDir, "Synthetic")
    out_real_path = os.path.join(args.outputDir, "Real")
    os.makedirs(out_syn_path, exist_ok=True)
    os.makedirs(out_real_path, exist_ok=True)

    # collect images
    in_syn_path = os.path.join(args.inputDir, "Synthetic")
    in_real_path = os.path.join(args.inputDir, "Real")
    images_fn = []
    
    # syn : 0 ~ 9
    for i in range(10):
        l_file_path = os.path.join(in_syn_path, 'TL{}.png'.format(i))
        r_file_path = os.path.join(in_syn_path, 'TR{}.png'.format(i))
        images_fn.append((l_file_path, r_file_path))
    
    # real : 10 ~ 19
    for i in range(10):
        l_file_path = os.path.join(in_real_path, 'TL{}.bmp'.format(i))
        r_file_path = os.path.join(in_real_path, 'TR{}.bmp'.format(i))
        images_fn.append((l_file_path, r_file_path))
    
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda")
    
    # load classifier model
    tic = time.time()
    classifier_model = classifier.ImageClassifier()
    classifier_model.load_state_dict(torch.load(args.classifier))
    if use_cuda:
        classifier_model.cuda()
    
    toc = time.time()
    print('load classifier model time: %f sec.' % (toc - tic))
    
    # load PSMNet model
    tic = time.time()
    maxdisp = 192
    disp_model = stackhourglass.PSMNet(maxdisp)
    disp_model = nn.DataParallel(disp_model, device_ids=[0])
    
    if use_cuda:
        disp_model.cuda()
    state_dict = torch.load(args.dispModel)
    disp_model.load_state_dict(state_dict['state_dict'])
    toc = time.time()
    print('load PSMNet model time: %f sec.' % (toc - tic))
    
    # main algo.
    suffix = '.pfm'
    for idx, (l_fn, r_fn) in enumerate(images_fn):
        
        
        tic = time.time()
        label = imgPreprocess(l_fn, classifier_model)
        isReverse = True if label >= 10 else False
        disp = computeDisp(l_fn, r_fn, disp_model, label, isReverse)
        toc = time.time()
        print('process ' + l_fn  + ' time: %f sec.' % (toc - tic))
        
        basefn = os.path.basename(l_fn)
        base = os.path.splitext(basefn)[0]
        
        # syn : 0 ~ 9, real : 10 ~ 19
        pfm_fn = os.path.join(out_syn_path, base + suffix) if idx < 10 else os.path.join(out_real_path, base + suffix)
        writePFM(pfm_fn, disp)
Example #11
0
    # if t_wta is None:
    # wtal = fillpixel(wta, int(np.max(wta)) + 1) #far
    
    # wtar = fillpixel_I(wta, int(np.max(wta)) + 1) #near
    # show([wtal, wtar, (wtal + wtar)//2, goodl, goodr])
    
    wta = cv2.medianBlur(wta.astype(np.uint8), 5)
    # cv2.imwrite('weighted_median_filter'+n+'.png', wta)
    # from roll_bi import *
    # guide = rolling_bilateral(imgL_o, 4)
    sss = wta.copy()
    ooo = sss.copy()
    ooo = cv2.normalize(imgL_o[:,:,0], ooo, 0,255,cv2.NORM_MINMAX)/255.
    # print(np.sum(ooo))
    # print(np.mean(fx_l))
    ooo = ((ooo**2)*255).astype(np.uint8)
    guide = imgL_o.copy()
    guide[:,:,0] = (cv2.normalize(wta.copy(),sss, 0,255,cv2.NORM_MINMAX).astype(np.uint8) + cv2.resize(fx_l, (fw, fh)))/2
    guide[:,:,1] = ooo
    guide[:,:,2] = (imgL_o[:,:,0]).astype(np.uint8)
    # print(np.mean(guide[:,:,0]),np.mean(guide[:,:,1]),np.mean(guide[:,:,2]))
    guide = cv2.cvtColor(guide, cv2.COLOR_HSV2RGB_FULL)
    wta = weighted_median_filter(wta.astype(np.uint8), guide, [i for i in range(int(np.max(wta)+1))], ri, epsi)
    wta = cv2.medianBlur(wta.astype(np.uint8), 5)
    cv2.imwrite(Opath+".png", cv2.normalize(wta,wta,0,255, cv2.NORM_MINMAX))
    from util import writePFM
    writePFM(Opath, wta.astype(np.float32))
    print(begin - now())

    exit()