def decode_with_seed(o_image, bwm_image, seed, is_align=False):

    name = bwm_image

    bwm_image = it.load_image(pt.join_path(basic_path, bwm_image))
    o_image = it.load_image(pt.join_path(basic_path, o_image))

    # align
    if is_align:
        bwm_image, h = it.alignImages(bwm_image, o_image)

    bwm_bgr = it.split(bwm_image)
    wm = []
    for tube in bwm_bgr:
        t = 20 * it.log(
            it.real(
                it.shuffle_image(
                    (it.shift(it.fft(tube)[:, :, 0])), seed=seed)))
        # t = it.shuffle_image(t, seed=seed)
        wm.append(t)
    wm = it.merge(wm[0], wm[1], wm[2])

    new_name = it.save_image_with_new_suffix(
        wm, pt.join_path(basic_path, "dwm_" + name), "png")

    return new_name, p_media + new_name
def save_fft_and_dfft(s_img, path):
    for i, tube in enumerate(s_img):
        ft = fft(tube)
        dft = shift(ft)[:, :, 0]
        ft = ft[:, :, 0]
        save_image(ft, pt.join_path(path, "ft_" + str(i) + ".png"))
        save_image(dft, pt.join_path(path, "dft_" + str(i) + ".png"))
def encode(o_image, wm):

    # name of original image
    name = o_image

    o_image = it.optimal_shape(it.load_image(pt.join_path(basic_path, name)))
    wm = it.load_image_grey(pt.join_path(basic_path, wm))
    wm = it.complement(wm)

    # Reshape size of watermark and flip it
    wm_shape = ((int)(o_image.shape[1] / 2), (int)(o_image.shape[0] / 2))
    r_wm = it.resize(wm, wm_shape)

    s_wm = it.fill_image(r_wm, [o_image.shape[0], o_image.shape[1]])
    f_wm = it.flip(s_wm) + s_wm

    #
    s_image = it.split(
        o_image)  # s_image[0] = b; s_image[1] = g; s_image[2] = r
    f_image = []
    sum_image = []
    final_img = []

    for tube in s_image:
        dft = it.shift(it.fft(tube))
        f_image.append(dft)

    for tube in f_image:
        print(tube.shape)
        tube[:, :, 0] = tube[:, :, 0] + f_wm * alpha
        tube[:, :, 1] = tube[:, :, 1] + f_wm * alpha
        sum_image.append(tube)

    # it.show_image(it.merge(sum_image[0], sum_image[1], sum_image[2]))
    # sum_image.append(tube)

    # test = it.merge(sum_image[0], sum_image[1], sum_image[2])
    # test = it.merge(f_image[0], f_image[1], f_image[2])
    # it.show_image(test)

    for tube in sum_image:
        idft = it.ifft(it.ishift(tube))
        final_img.append(idft)
    final_img = it.merge(final_img[0], final_img[1], final_img[2])

    new_name = it.save_image_with_new_suffix(
        final_img, pt.join_path(basic_path, "bwm_" + name), "png")

    return new_name, p_media + new_name
def alignImages(img1, img2):
    # change images into grayscale
    img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

    # extract ORB features and get descriptors.
    orb = cv2.ORB_create(MAX_FEATURES)  # using max feature
    feature1, descriptors1 = orb.detectAndCompute(img1_gray, None)
    feature2, descriptors2 = orb.detectAndCompute(img2_gray, None)

    # match features.
    matcher = cv2.DescriptorMatcher_create(
        cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
    matches = matcher.match(descriptors1, descriptors2, None)

    # sort matches by score
    matches.sort(key=lambda x: x.distance, reverse=False)

    # remove some bad matches
    bad_matches = int(len(matches) * GOOD_MATCH_PERCENT)
    matches = matches[:bad_matches]

    # connect good matches
    img_matches = cv2.drawMatches(img1, feature1, img2, feature2, matches,
                                  None)
    basic_path = pt.join_path(pt.get_cwd(), 'catalog', 'media')
    final_path = pt.join_path(basic_path, "matches.png")
    save_image(img_matches, final_path)
    #cv2.imwrite("matches.png", img_matches)

    # extract location of good matches
    points1 = np.zeros((len(matches), 2), dtype=np.float32)
    points2 = np.zeros((len(matches), 2), dtype=np.float32)

    for i, match in enumerate(matches):
        points1[i, :] = feature1[match.queryIdx].pt
        points2[i, :] = feature2[match.trainIdx].pt

    # find homography
    h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

    # use homography
    height, width, channels = img2.shape
    img1_reg = cv2.warpPerspective(img1, h, (width, height))

    return img1_reg, h
from catalog import image_tool as it
from catalog import path_tool as pt

alpha = 40.0
basic_path = pt.join_path(pt.get_cwd(), 'catalog', 'media')
p_media = "/catalog/media/"


def encode(o_image, wm):

    # name of original image
    name = o_image

    o_image = it.optimal_shape(it.load_image(pt.join_path(basic_path, name)))
    wm = it.load_image_grey(pt.join_path(basic_path, wm))
    wm = it.complement(wm)

    # Reshape size of watermark and flip it
    wm_shape = ((int)(o_image.shape[1] / 2), (int)(o_image.shape[0] / 2))
    r_wm = it.resize(wm, wm_shape)

    s_wm = it.fill_image(r_wm, [o_image.shape[0], o_image.shape[1]])
    f_wm = it.flip(s_wm) + s_wm

    #
    s_image = it.split(
        o_image)  # s_image[0] = b; s_image[1] = g; s_image[2] = r
    f_image = []
    sum_image = []
    final_img = []