def blend_color(self,
                    refered_pixel_coordi_lst,
                    blending_area_mask,
                    sigma1=0.5,
                    sigma2=0.5):
        overlap_slic = MaskedSLIC(self.tar_img,
                                  blending_area_mask,
                                  region_size=20)
        sampling_pixel_coordi_lst = np.array([
            (rows[len(rows) // 2], cols[len(rows) // 2])
            for idx, (rows, cols) in enumerate(overlap_slic.labels_position)
            if idx != 0
        ])
        color_compensation_lst = self.__calc_color_compensation(
            sampling_pixel_coordi_lst, refered_pixel_coordi_lst, sigma1,
            sigma2)

        ref_img = np.copy(self.ref_img)
        blended_img = np.zeros(self.ref_img.shape, dtype=np.float64)
        tar_img_norm = self.tar_img / 255

        for idx, color_compensation in enumerate(color_compensation_lst):
            super_pixel_coordis = overlap_slic.labels_position[idx + 1]
            blended_img[super_pixel_coordis[0], super_pixel_coordis[1]] = (
                (tar_img_norm[super_pixel_coordis[0], super_pixel_coordis[1]] +
                 color_compensation) * 255)

        blended_img[blended_img < 0] = 0
        blended_img[blended_img > 255] = 255
        ref_img[blended_img > 0] = blended_img[blended_img > 0].astype(
            np.uint8)

        return ref_img
def main():
    '''
    Initialization
    '''
    start = time.time()

    tar_4_corners_xy = None
    ref_4_corners_xy = None
    homoMat = np.load('image/save_H.npy')
    shiftMat = np.load('image/save_Shift.npy')
    ref_4_corners_xy = np.load('image/save_ref_4_corners_xy.npy')
    tar_4_corners_xy = np.load('image/save_tar_4_corners_xy.npy')
    warp_tar_img = cv2.imread('image/warped_target.png')
    warp_ref_img = cv2.imread('image/warped_reference.png')
    seam_mask = cv2.imread('image/seam_mask.png', cv2.IMREAD_GRAYSCALE)

    mask = Mask(warp_tar_img, warp_ref_img)
    ref_region_mask = cv2.imread('image/result_from_reference.png',
                                 cv2.IMREAD_GRAYSCALE)
    tar_region_mask = cv2.bitwise_and(cv2.bitwise_not(ref_region_mask),
                                      mask.tar)

    mask.tar_result = tar_region_mask
    mask.ref_result = ref_region_mask
    '''
    Blend color
    '''

    CB = CBlender(warp_tar_img, warp_ref_img, seam_mask, tar_4_corners_xy,
                  ref_4_corners_xy, homoMat, shiftMat, mask)

    devided_tar_nonoverlap_mask, devided_tar_edge_mask = CB.split_tar_nonoverlap_area_and_edge(
        ref_4_corners_xy, tar_4_corners_xy, shiftMat, homoMat)
    devided_tar_edge_mask_1 = np.copy(devided_tar_edge_mask)
    devided_tar_edge_mask_1[devided_tar_edge_mask_1 == 127] = 0
    devided_tar_edge_mask_2 = np.copy(devided_tar_edge_mask)
    devided_tar_edge_mask_2[devided_tar_edge_mask_1 == 255] = 0

    # refered_pixel_coordi_lst = np.vstack( (\
    #     np.array( np.nonzero(seam_mask) ).T, \
    #     np.array( np.where(devided_tar_edge_mask==255) ).T, \
    #     np.array( np.where(devided_tar_edge_mask==127) ).T \
    #     ))

    slic = MaskedSLIC(warp_tar_img,
                      np.bitwise_and(mask.tar_result, mask.overlap),
                      region_size=20,
                      compactness=5)

    seam_superpixel_idx_lst = np.array([
        idx for idx, (rows, cols) in enumerate(slic.labels_position)
        if np.sum(seam_mask[rows, cols]) > 0
    ])
    tar_edge_1_superpixel_idx_lst = np.array([
        idx for idx, (rows, cols) in enumerate(slic.labels_position)
        if np.sum(devided_tar_edge_mask_1[rows, cols]) > 0
    ])
    tar_edge_2_superpixel_idx_lst = np.array([
        idx for idx, (rows, cols) in enumerate(slic.labels_position)
        if np.sum(devided_tar_edge_mask_2[rows, cols]) > 0
    ])

    refered_idx_lst = np.hstack(
        (seam_superpixel_idx_lst, tar_edge_1_superpixel_idx_lst,
         tar_edge_2_superpixel_idx_lst))

    refered_pixel_coordi_lst = np.array([
        (rows[len(rows) // 2], cols[len(rows) // 2])
        for idx, (rows, cols) in enumerate(slic.labels_position)
        if (idx != 0) and (np.isin(idx, refered_idx_lst))
    ])
    fuck_this_shit = CB.blend_color(refered_pixel_coordi_lst,
                                    np.bitwise_and(CB.mask.overlap,
                                                   CB.mask.tar_result),
                                    sigma1=0.3,
                                    sigma2=0.2)

    cv2.imwrite('overlap_blending_only.png', fuck_this_shit)

    CB.ref_img = fuck_this_shit

    refered_pixel_coordi_lst = np.array([
        (rows[len(rows) // 2], cols[len(rows) // 2])
        for idx, (rows, cols) in enumerate(slic.labels_position)
        if (idx != 0) and (np.isin(idx, tar_edge_1_superpixel_idx_lst))
    ])
    mask = np.zeros(devided_tar_nonoverlap_mask.shape)
    mask[devided_tar_nonoverlap_mask == 255] = 255
    fuck_this_shit = CB.blend_color(refered_pixel_coordi_lst,
                                    mask,
                                    sigma1=0.1,
                                    sigma2=0.05)
    cv2.imwrite('nonoverlap_blending_only1.png', fuck_this_shit)
    CB.ref_img = fuck_this_shit
    refered_pixel_coordi_lst = np.array([
        (rows[len(rows) // 2], cols[len(rows) // 2])
        for idx, (rows, cols) in enumerate(slic.labels_position)
        if (idx != 0) and (np.isin(idx, tar_edge_2_superpixel_idx_lst))
    ])
    mask = np.zeros(devided_tar_nonoverlap_mask.shape)
    mask[devided_tar_nonoverlap_mask == 127] = 255
    fuck_this_shit = CB.blend_color(refered_pixel_coordi_lst,
                                    mask,
                                    sigma1=0.1,
                                    sigma2=0.05)
    cv2.imwrite('nonoverlap_blending_only2.png', fuck_this_shit)
    for idx in tar_edge_2_superpixel_idx_lst:
        fuck_this_shit[slic.labels_position[idx][0],
                       slic.labels_position[idx][1]] = np.random.randint(
                           0, 255, 3)
    cv2.imwrite('araara.png', fuck_this_shit)

    warp_ref_img[slic.contour_mask > 0] = (0, 255, 0)
    warp_tar_img[slic.contour_mask > 0] = (0, 255, 0)
    cv2.imwrite('target_image_slic.png', warp_ref_img)
    cv2.imwrite('reference_image_slic.png', warp_tar_img)

    print(f'time: {time.time() - start}')
    

start = time.time()

warp_tar_img = cv2.cvtColor(cv2.imread('image/warped_target.png'), cv2.COLOR_BGR2YUV)

warp_ref_img = cv2.cvtColor(cv2.imread('image/warped_reference.png'), cv2.COLOR_BGR2YUV)

mask = Mask(warp_tar_img, warp_ref_img)
seam_mask = cv2.imread('image/seam_mask.png',cv2.IMREAD_GRAYSCALE)
ref_region_mask =  cv2.imread('image/result_from_reference.png',cv2.IMREAD_GRAYSCALE)
tar_region_mask = cv2.bitwise_and( cv2.bitwise_not(ref_region_mask) , mask.tar )
mask.tar_result = tar_region_mask
mask.ref_result = ref_region_mask
slic = MaskedSLIC(cv2.cvtColor(warp_tar_img, cv2.COLOR_YUV2BGR), np.bitwise_and(mask.tar_result, mask.overlap) ,region_size=20, compactness=5)

seam_superpixel_idx_lst = np.array([idx for idx, (rows, cols) in enumerate(slic.labels_position) if np.sum(seam_mask[rows, cols]) > 0])
# print(seam_superpixel_idx_lst)

median_tar_img = np.copy(warp_tar_img)
for i in range(len(slic.labels_position)):
    if i != 0:
        rows, cols = slic.labels_position[i]
        median_tar_img[rows, cols] = np.mean(median_tar_img[rows, cols], axis=0)

median_ref_img = np.copy(warp_ref_img)
for i in range(len(slic.labels_position)):
    if i != 0:
        rows, cols = slic.labels_position[i]
        median_ref_img[rows, cols] = np.mean(median_ref_img[rows, cols], axis=0)
Esempio n. 4
0

start = time.time()

warp_tar_img = cv2.imread('image/warped_target.png')

warp_ref_img = cv2.imread('image/warped_reference.png')
mask = Mask(warp_tar_img, warp_ref_img)
seam_mask = cv2.imread('image/seam_mask.png', cv2.IMREAD_GRAYSCALE)
ref_region_mask = cv2.imread('image/result_from_reference.png',
                             cv2.IMREAD_GRAYSCALE)
tar_region_mask = cv2.bitwise_and(cv2.bitwise_not(ref_region_mask), mask.tar)
mask.tar_result = tar_region_mask
mask.ref_result = ref_region_mask
slic = MaskedSLIC(warp_tar_img,
                  np.bitwise_and(mask.tar_result, mask.overlap),
                  region_size=20,
                  compactness=3)

CB = CBlender(warp_tar_img, warp_ref_img, seam_mask, mask)

refered_pixel_coordi_lst = np.array([
    (rows[len(rows) // 2], cols[len(rows) // 2])
    for idx, (rows, cols) in enumerate(slic.labels_position) if idx != 0
])
sigma1 = 0.1
sigma2 = 0.25
# print(refered_pixel_cooordi_lst)
a = CB.blend_color(refered_pixel_coordi_lst, mask.tar_result, sigma1, sigma2)
# a[refered_pixel_cooordi_lst[0], refered_pixel_cooordi_lst[1],:] = (255,255,255)
# a[slic.labels_position[1][0], slic.labels_position[1][1]] = (0,0,255)
# a[slic.contour_mask>0] = (0,255,0)
Esempio n. 5
0
    d = labels.ravel()
    f = lambda x: np.unravel_index(x.index, labels.shape)
    return pd.Series(d).groupby(d).apply(f)


start_time = time.time()
tar_img = cv2.imread('./data/processed_image/warped_target.png')
ref_img = cv2.imread('./data/processed_image/warped_reference.png')
mask = Mask(tar_img, ref_img)
# segmentation = slic(tar_img, n_segments=9)#, mask=mask.overlap)
# g = graph.rag_mean_color(tar_img, segmentation)
# lc = graph.show_rag(segmentation, g ,tar_img)
# cbar = plt.colorbar(lc)
# numOfPixel = len(np.unique(segmentation))

maskedSLIC = MaskedSLIC(tar_img, mask.overlap, region_size=200)
tar_img[maskedSLIC.contour_mask > 0] = (0, 255, 0)
cv2.imwrite('slic_contour.png', tar_img)
g = graph.rag_mean_color(tar_img, maskedSLIC.labels)

fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(6, 8))

ax[0].set_title('RAG drawn with default settings')
lc = graph.show_rag(labels, g, img, ax=ax[0])
# specify the fraction of the plot area that will be used to draw the colorbar
fig.colorbar(lc, fraction=0.03, ax=ax[0])

ax[1].set_title('RAG drawn with grayscale image and viridis colormap')
lc = graph.show_rag(labels,
                    g,
                    img,
Esempio n. 6
0
from cv2 import cv2
import numpy as np
from mask import Mask
import time
from SLIC import MaskedSLIC
import pickle

start_time = time.time()

tar_img = cv2.imread('./data/processed_image/warped_target.png')
ref_img = cv2.imread('./data/processed_image/warped_reference.png')
mask = Mask(tar_img, ref_img)
maskedSLIC = MaskedSLIC(ref_img, mask.overlap, region_size=20)

# for i in maskedSLIC.adjacent_pairs[0]:
#     print(i)

# for (rows, cols) in maskedSLIC.labels_position:
#     tar_img[rows, cols] = np.random.randint(0,256,size=(1,3))

# cv2.imwrite('./slic_pandas.png',tar_img)
print(time.time() - start_time)
print(f'number of pixel is :{maskedSLIC.numOfPixel}')

with open('./data/SLIC/maskedSLIC.pkl', 'wb') as output:
    pickle.dump(maskedSLIC, output, pickle.HIGHEST_PROTOCOL)
Esempio n. 7
0
        return ref_img


start = time.time()

warp_tar_img = cv2.imread('image/warped_target.png')
warp_ref_img = cv2.imread('image/warped_reference.png')
seam_mask = cv2.imread('image/seam_mask.png', cv2.IMREAD_GRAYSCALE)

mask = Mask(warp_tar_img, warp_ref_img)

ref_region_mask = cv2.imread('image/result_from_reference.png',
                             cv2.IMREAD_GRAYSCALE)
tar_region_mask = cv2.bitwise_and(cv2.bitwise_not(ref_region_mask), mask.tar)

maskSLIC = MaskedSLIC(warp_tar_img, tar_region_mask, region_size=20)
CB = CBlender(warp_tar_img, warp_ref_img, maskSLIC, seam_mask, tar_region_mask)
blended_img = CB.blend_color()

print(CB.maskSLIC.numOfPixel)
print(np.sum(CB.tar_mask) / 255)

print(f'time: {time.time()-start}')
cv2.imwrite('blended_img.png', blended_img)
blended_img[maskSLIC.contour_mask > 0] = (0, 255, 0)
cv2.imwrite('pixel_blended_img.png', blended_img)
blended_img[seam_mask > 0] = (0, 0, 255)
cv2.imwrite('seam_pixel_blended_img.png', blended_img)
# cv2.imwrite('mask.png', mask.tar_nonoverlap)
# print(CB.seam_color_diff_lst.shape)