Пример #1
0
def resize_img(image, crop_size=(128, 128), algo=None):
    w = crop_size[0]
    h = crop_size[1]
    newdim = (w, h)

    if algo is None:
        scale_algos = [
            cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC,
            cv2.INTER_AREA, cv2.INTER_LANCZOS4, cv2.INTER_LINEAR_EXACT
        ]  #scaling interpolation options
        interpol = random.choice(scale_algos)
    else:
        if isinstance(algo, list):
            interpol = random.choice(algo)
        elif isinstance(algo, int):
            interpol = algo

    if interpol == 777:  #'matlab_bicubic'
        resized = util.imresize_np(image, 1 / scale, True)
        # force to 3 channels
        # if resized.ndim == 2:
        # resized = np.expand_dims(resized, axis=2)
    else:
        # use the provided OpenCV2 algorithms
        resized = cv2.resize(image, newdim, interpolation=interpol)

    return resized, interpol
Пример #2
0
def scale_img(image, scale, algo=None):
    h, w, c = image.shape
    newdim = (int(w / scale), int(h / scale))

    # randomly use OpenCV2 algorithms if none are provided
    if algo is None:
        scale_algos = [
            cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC,
            cv2.INTER_AREA, cv2.INTER_LANCZOS4, cv2.INTER_LINEAR_EXACT
        ]  #scaling interpolation options
        interpol = random.choice(scale_algos)
        resized = cv2.resize(image, newdim, interpolation=interpol)
    # using matlab imresize
    else:
        if isinstance(algo, list):
            interpol = random.choice(algo)
        elif isinstance(algo, int):
            interpol = algo

        if interpol == 777:  #'matlab_bicubic'
            resized = util.imresize_np(image, 1 / scale, True)
            # force to 3 channels
            # if resized.ndim == 2:
            # resized = np.expand_dims(resized, axis=2)
        else:
            # use the provided OpenCV2 algorithms
            resized = cv2.resize(image, newdim, interpolation=interpol)

    #resized = np.clip(resized, 0, 1)
    return resized, interpol
def generate_LR(img_HR, scale=4):
    """generate LR image,
    size of img_HR should be multiple of scale"""
    assert scale > 1, "scale should be greater than 1!"
    H_s, W_s, _ = img_HR.shape
    # maybe the assert is not necessary
    temp = (H_s % scale == 0) and (W_s % scale == 0)
    assert temp, "img size should be multiple of {}".format(scale)
    # using matlab imresize
    img_LR = imresize_np(img_HR, 1 / scale, True)
    return img_LR
Пример #4
0
def resize_img(image, out_size=(128, 128), algo=None):
    interpol = select_algo(algo)

    if interpol == 777:
        # using matlab imresize
        resized = util.imresize_np(image, out_size[1] / image.shape[0], True)
        # force to 3 channels
        # if resized.ndim == 2:
        # resized = np.expand_dims(resized, axis=2)
    else:
        # use the provided OpenCV2 algorithms
        resized = cv2.resize(image, out_size, interpolation=interpol)

    return resized, interpol
Пример #5
0
def scale_img(image, scale, algo=None):
    h, w, c = image.shape
    newdim = (int(w / scale), int(h / scale))

    interpol = select_algo(algo)

    if interpol == 777:
        # using matlab imresize
        resized = util.imresize_np(image, 1 / scale, True)
        # force to 3 channels
        # if resized.ndim == 2:
        # resized = np.expand_dims(resized, axis=2)
    else:
        # use the provided OpenCV2 algorithms
        resized = cv2.resize(image, newdim, interpolation=interpol)

    #resized = np.clip(resized, 0, 1)
    return resized, interpol
def generate_mod_LR_bic():
    # set parameters
    up_scale = 4
    mod_scale = 4
    # set data dir
    sourcedir = '/content/own_dataset-20190716T130232Z-001/training_tiles/'
    savedir = '/content/own_dataset-20190716T130232Z-001/training_tiles_LQ/'

    saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale))
    saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale))
    saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale))

    if not os.path.isdir(sourcedir):
        print('Error: No source data found')
        exit(0)
    if not os.path.isdir(savedir):
        os.mkdir(savedir)

    if not os.path.isdir(os.path.join(savedir, 'HR')):
        os.mkdir(os.path.join(savedir, 'HR'))
    if not os.path.isdir(os.path.join(savedir, 'LR')):
        os.mkdir(os.path.join(savedir, 'LR'))
    if not os.path.isdir(os.path.join(savedir, 'Bic')):
        os.mkdir(os.path.join(savedir, 'Bic'))

    if not os.path.isdir(saveHRpath):
        os.mkdir(saveHRpath)
    else:
        print('It will cover ' + str(saveHRpath))

    if not os.path.isdir(saveLRpath):
        os.mkdir(saveLRpath)
    else:
        print('It will cover ' + str(saveLRpath))

    if not os.path.isdir(saveBicpath):
        os.mkdir(saveBicpath)
    else:
        print('It will cover ' + str(saveBicpath))

    filepaths = [f for f in os.listdir(sourcedir) if f.endswith('.png')]
    num_files = len(filepaths)

    # prepare data with augementation
    for i in range(num_files):
        filename = filepaths[i]
        print('No.{} -- Processing {}'.format(i, filename))
        # read image
        image = cv2.imread(os.path.join(sourcedir, filename))

        width = int(np.floor(image.shape[1] / mod_scale))
        height = int(np.floor(image.shape[0] / mod_scale))
        # modcrop
        if len(image.shape) == 3:
            image_HR = image[0:mod_scale * height, 0:mod_scale * width, :]
        else:
            image_HR = image[0:mod_scale * height, 0:mod_scale * width]
        # LR
        image_LR = imresize_np(image_HR, 1 / up_scale, True)
        # bic
        image_Bic = imresize_np(image_LR, up_scale, True)

        cv2.imwrite(os.path.join(saveHRpath, filename), image_HR)
        cv2.imwrite(os.path.join(saveLRpath, filename), image_LR)
        cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)