Пример #1
0
def process_and_visualize(image,
                          idx,
                          title=None,
                          process_raw=True,
                          draw=True,
                          method=1,
                          invert=False):
    height, width, _ = image.shape
    image = cv2.resize(image, (int(width / 10), int(height / 10)))
    original = image.copy()
    if process_raw:
        image = process_image(image)
    else:
        image = adjust_gamma(image, gamma=1)
    # rgb = img
    gt = gts[idx - 1]
    corrected = color_correct_single(image, gt, 1)
    foreground, mask, ident = cv2_contours(corrected,
                                           upper=np.array([160, 255, 255]),
                                           method=method,
                                           invert=invert)

    ill1, ill2 = random_colors()
    relighted = color_correct(image,
                              mask=mask,
                              ill1=1 / ill1,
                              ill2=1 / ill2,
                              c_ill=1)
    colored_mask = np.array(
        [[ill1 * pixel + ill2 * (1 - pixel) for pixel in row] for row in mask])
    if draw:
        visualize([image, corrected, relighted, colored_mask], title=title)
    return relighted, colored_mask, ill1, ill2
Пример #2
0
def main_process(data):
    img, gtLoader = data
    folder_step = 200
    draw = False
    save = True
    use_edges = True
    use_grad = True
    succ, image, colored_mask, relighted, relighted1, mask = process_with_edges(
        img, gtLoader, folder_step, use_edges, use_grad)
    if succ:
        if draw:
            pu.visualize([image, relighted, colored_mask, mask], title=img)
        if save:
            name = f'{img}-9{"-grad" if use_grad else ""}{"-edg" if use_edges else ""}.png'
            cv2.imwrite(f'./data/relighted/images/{name}',
                        cv2.cvtColor(relighted, cv2.COLOR_RGB2BGR))
            cv2.imwrite(
                f'./data/relighted/gt/{name}',
                cv2.cvtColor((colored_mask * 255).astype(np.uint8),
                             cv2.COLOR_RGB2BGR))
            cv2.imwrite(
                f'./data/relighted/gt_mask/{name}',
                cv2.cvtColor((mask * 255).astype(np.uint8), cv2.COLOR_RGB2BGR))
            cv2.imwrite(f'./data/relighted/img_corrected_1/{name}',
                        cv2.cvtColor(relighted1, cv2.COLOR_RGB2BGR))
            print(f'Saved {img}')
    else:
        if draw:
            pu.visualize([image, mask], title=img)
Пример #3
0
def process_and_visualize(image,
                          idx,
                          gts1,
                          gts2,
                          mask,
                          title=None,
                          draw=True,
                          use_estimation=False):
    # image = cv2.resize(image, (0, 0), fx=1 / 5, fy=1 / 5)
    # mask = cv2.resize(mask, (0, 0), fx=1 / 5, fy=1 / 5)
    mask = gaussian(mask, 3)
    if use_estimation:
        image1 = iu.mask_image(image, mask)
        image2 = iu.mask_image(image, 1 - mask)
        gt2 = ru.gray_world_estimation(image1, mask) / 255
        gt1 = ru.gray_world_estimation(image2, 1 - mask) / 255
        corrected1 = ru.white_balance(image1, gt2, mask)
        corrected2 = ru.white_balance(image2, gt1, 1 - mask)
        # corrected1 = np.where(image1 == [0, 0, 0], (gt2 * 255).astype(np.uint8), corrected1)
        # corrected2 = np.where(image2 == [0, 0, 0], (gt1 * 255).astype(np.uint8), corrected2)
        corrected = iu.combine_images_with_mask(corrected1, corrected2, mask)
        gt = ru.gray_world_estimation(image) / 255
        corrected1 = ru.white_balance(image, gt)
        corrected2 = ru.white_balance(image, gt2)
    else:
        gt1 = gts1[idx - 1]

        gt2 = gts2[idx - 1]

        def sigmoid(x):
            return 1 / (1 + np.exp(-x / 255))

        # sig_mask = sigmoid(mask)

        # gt = gt2 / gt1
        corrected = iu.color_correct(image, mask, gt2, gt1, c_ill=1 / 3)
        corrected1 = iu.color_correct_single(image, gt2, c_ill=1 / 3)
        corrected2 = iu.color_correct_single(image, gt1, c_ill=1 / 3)
    # corrected = iu.adjust_gamma(corrected, 0.9)
    # corrected_sig = iu.color_correct(image, sig_mask, gt, np.ones(3), c_ill=1)

    if draw:
        colored_mask = np.where(mask > 0.5, (gt2 * 255).astype(np.uint8),
                                (gt1 * 255).astype(np.uint8))
        path = 'images/model_corrected_unet_cube_comb/'
        if not os.path.exists(path):
            os.mkdir(path)
        pu.visualize(
            [image, mask, colored_mask, corrected1, corrected],
            titles=['a)', 'b)', 'c)', 'd)', 'e)'],
            in_line=True,
            out_file=f'{path}{idx}',
            # custom_transform=lambda x: cv2.flip(x.transpose(1, 0, 2), 1),
            # title=title
        )
    return corrected
Пример #4
0
def process_and_visualize(image, idx, gts, title=None, draw=True):
    # rgb = img
    gt = gts[idx - 1]
    gt = gt / np.linalg.norm(gt)
    corrected = iu.color_correct_single(image, gt, 1 / np.sqrt(3))

    if draw:
        pu.visualize([
            image,
            corrected,
        ], title=title)
    return corrected
Пример #5
0
def process_and_visualize(
    image,
    masks,
    gts,
    idx,
    title=None,
    draw=True,
):
    ttls = ['a)', 'b)', 'c)', 'd)', 'e)', 'f)', 'g)', 'h)']
    gt1, gt2 = gts[0]
    image = cv2.resize(image, (0, 0), fx=1 / 5, fy=1 / 5)
    corrected2 = iu.color_correct_single(image, gt1, c_ill=1 / np.sqrt(3))

    out_imgs = [image, corrected2]
    for i, mask in enumerate(masks):
        gt1, gt2 = gts[i]
        # rgb = img

        mask = gaussian(mask, 5)

        def sigmoid(x):
            return 1 / (1 + np.exp(-x / 255))

        mask = cv2.resize(mask, (0, 0), fx=1 / 5, fy=1 / 5)
        corrected = iu.color_correct(image,
                                     mask,
                                     gt1,
                                     gt2,
                                     c_ill=1 / np.sqrt(3))
        out_imgs.append(mask)
        out_imgs.append(corrected)

    if draw:
        pu.visualize(out_imgs,
                     titles=list(map(lambda x: ttls[x], range(len(out_imgs)))),
                     in_line=False,
                     out_file=f'./images/res{idx}-{i}.png')
    return corrected
Пример #6
0
import cv2
import utils.file_utils as fu
import utils.plotting_utils as pu

names = [17, 18, 3, 5, 7, 10, 21, 9, 2]
names = [2]
names = list(map(lambda x: str(x) + '.jpg', names))
for name in names:
    image = fu.load_png(name, './data', 'custom_mask_nocor')

    rotated = cv2.flip(image, 0)
    pu.visualize([image, rotated])
    cv2.imwrite(f'./data/custom_mask_nocor/{name}', rotated)
exit(0)
Пример #7
0
def process_with_real_and_predicted(image, idx, mask, gt_mask, gt):
    mask = gaussian(mask, 3)
    image1 = iu.mask_image(image, mask)
    image2 = iu.mask_image(image, 1 - mask)
    gt2 = ru.gray_world_estimation(image1, mask) / 255
    gt1 = ru.gray_world_estimation(image2, 1 - mask) / 255
    corrected1 = ru.white_balance(image1, gt2, mask)
    corrected2 = ru.white_balance(image2, gt1, 1 - mask)
    # corrected1 = np.where(image1 == [0, 0, 0], (gt2 * 255).astype(np.uint8), corrected1)
    # corrected2 = np.where(image2 == [0, 0, 0], (gt1 * 255).astype(np.uint8), corrected2)
    corrected = iu.combine_images_with_mask(corrected1, corrected2, mask)
    # gt1 = ru.gray_world_estimation(image)
    # gt2 = gt1
    colored_mask = np.where(mask > 0.5, (gt2 * 255).astype(np.uint8),
                            (gt1 * 255).astype(np.uint8))

    gt_mask = gt_mask / 255
    gti2 = ru.gray_world_estimation(gt, gt_mask, mask_hsv=False) / 255
    gti1 = ru.gray_world_estimation(gt, 1 - gt_mask, mask_hsv=False) / 255

    def pp_angular_difference(cmask, gt):
        gt, cmask = gt.clip(1, 254), cmask.clip(1, 254)
        cmask, gt = cmask / 255, gt / 255
        dis = np.array([
            ru.angular_distance(cmask[i, j, :], gt[i, j, :])
            for i in range(cmask.shape[0]) for j in range(cmask.shape[1])
        ])
        return dis.mean()

    def pc_angular_difference(gt1, gt2, gti1, gti2):
        d1 = (ru.angular_distance(gt1, gti1) +
              ru.angular_distance(gt2, gti2)) / 2
        d2 = (ru.angular_distance(gt2, gti1) +
              ru.angular_distance(gt1, gti2)) / 2
        return np.minimum(d1, d2)

    print(
        f'{idx}, {pp_angular_difference(colored_mask, gt)}, {pc_angular_difference(gt1, gt2, gti1, gti2)}'
    )
    colored_mask2 = np.where(gt_mask > 0.5, (gti2 * 255).astype(np.uint8),
                             (gti1 * 255).astype(np.uint8))

    # path = 'images/model_corrected_custom_cube6/'
    # if not os.path.exists(path):
    #     os.mkdir(path)
    pu.visualize(
        [
            image,
            mask,
            colored_mask,
            colored_mask2,
            corrected,
        ],
        titles=['a)', 'b)', 'c)', 'd)', 'e)'],
        in_line=True,
        # out_file=f'{path}{idx}',
        # custom_transform=lambda x: cv2.flip(x.transpose(1, 0, 2), 1),
        # title=title
    )
    return

    image1 = iu.mask_image(image, gt_mask)
    image2 = iu.mask_image(image, 1 - gt_mask)
    gt2 = ru.gray_world_estimation(gt, gt_mask) / 255
    gt1 = ru.gray_world_estimation(gt, 1 - gt_mask) / 255
    gt2 = ru.white_patch_estimation(gt, gt_mask) / 255
    gt1 = ru.white_patch_estimation(gt, 1 - gt_mask) / 255
    # gt1 /= 2
    # gt2 /= 2
    # corrected1 = ru.white_balance(image1, gt2, gt_mask)
    # corrected2 = ru.white_balance(image2, gt1, 1 - gt_mask)
    # corrected1 = iu.color_correct_single(image1, gt2, c_ill=1/3)
    # corrected2 = iu.color_correct_single(image2, gt1, c_ill=1/3)
    # corrected1 = np.where(image1 == [0, 0, 0], (gt2 * 255).astype(np.uint8), corrected1)
    # corrected2 = np.where(image2 == [0, 0, 0], (gt1 * 255).astype(np.uint8), corrected2)
    corrected_real = iu.combine_images_with_mask(corrected1, corrected2,
                                                 gt_mask)
    colored_mask2 = np.where(gt_mask > 0.5, (gt2 * 255).astype(np.uint8),
                             (gt1 * 255).astype(np.uint8))