예제 #1
0
def recoveryA(matrixA, Ainfo):
    height, width = matrixA.shape

    k = 0
    for i in range(height):
        for j in range(width):
            if k < len(Ainfo):
                matrixA[i, j] = utils.replace_lowbit(matrixA[i, j], Ainfo[k])
                k += 1
    return matrixA
예제 #2
0
def encrypt(img_matrix, cipher):
    img_matrix_copy = img_matrix.copy()
    encrypted_img = encode.stream_cipher_encrypt(img_matrix_copy, cipher)
    flagData = [1 for i in range(15)]
    flagData += [0,0,1] # 001_bin = 1_oc
    flagData += [1,0] # [encrypt_flag, embeddinmg flag]

    if encrypted_img.shape[0]*encrypted_img.shape[1] < 20:
        print("not enough space to embedding.")
        return img_matrix

    num = 0
    for i in range(encrypted_img.shape[0]):
        for j in range(encrypted_img.shape[1]):
            encrypted_img[i,j] = utils.replace_lowbit(encrypted_img[i,j], flagData[num])
            num += 1
            if num > 19:
                break
        if num > 19:
            break

    return encrypted_img
예제 #3
0
def recoveryB(matrixB):
    height, width = matrixB.shape
    margin_info = []
    margin_info += [x for x in (matrixB[0, :] % 2)]
    margin_info += [x for x in (matrixB[-1, :] % 2)]
    margin_info += [x for x in (matrixB[1:-2, 0] % 2)]
    margin_info += [x for x in (matrixB[1:-2, -1] % 2)]

    Aindex = utils.bits2int_u32(margin_info[0:32])
    sample_lm = utils.bits2int9(margin_info[32:41])
    sample_ln = utils.bits2int9(margin_info[41:50])
    sample_rm = utils.bits2int9(margin_info[50:59])
    sample_rn = utils.bits2int9(margin_info[59:68])
    notSample_lm = utils.bits2int9(margin_info[68:77])
    notSample_ln = utils.bits2int9(margin_info[77:86])
    notSample_rm = utils.bits2int9(margin_info[86:95])
    notSample_rn = utils.bits2int9(margin_info[95:104])
    l = utils.bits2int_u32(margin_info[104:136])
    sp_size = utils.bits2int_u32(margin_info[136:168])
    np_size = utils.bits2int_u32(margin_info[168:200])

    embedRound = utils.bits2int9(margin_info[200:209])

    boundary_map = margin_info[209:]

    s_payload = []
    ns_payload = []

    # info in sample pixel
    if sp_size != 0:
        b_index = l  # This is small L, not one
        p = 0
        for i in range(1, height - 1):
            for j in range(1, width - 1):
                if utils.is_sample_pixel(i, j) and p < sp_size:
                    inter_pixel = utils.interpolation_pixel(matrixB, i, j, 0)
                    e = matrixB[i][j] - inter_pixel  #e'
                    if matrixB[i][j] > 0 and matrixB[i][j] < 255:
                        e, tmp = recover(e, sample_lm, sample_ln, sample_rm,
                                         sample_rn, s_payload)  # e
                        matrixB[i][j] = inter_pixel + e  # x
                        p += tmp
                    else:
                        if boundary_map[b_index] != 0:
                            e, tmp = recover(e, sample_lm, sample_ln,
                                             sample_rm, sample_rn, s_payload)
                            matrixB[i][j] = inter_pixel + e
                            p += tmp
                        b_index += 1

    # not sample pixel
    p = 0
    b_index = 0
    # inter_y中含有原始采样像素,和第一类非采样像素的插值值
    inter_B = utils.generate_interpolation_image(
        matrixB, 45, utils.is_non_sample_pixel_first)
    inter_B = utils.generate_interpolation_image(
        inter_B, 0, utils.is_non_sample_pixel_second)  # x'

    for i in range(1, height - 1):
        for j in range(1, width - 1):
            if (p < np_size or sp_size != 0) and utils.is_non_sample_pixel(
                    i, j):
                inter_pixel = inter_B[i][j]
                e = matrixB[i][j] - inter_pixel  # e'
                if 0 < matrixB[i][j] < 255:
                    e, tmp = recover(e, notSample_lm, notSample_ln,
                                     notSample_rm, notSample_rn,
                                     ns_payload)  # e
                    matrixB[i][j] = inter_pixel + e  # x
                    p += tmp
                else:
                    if boundary_map[b_index] != 0:
                        e, tmp = recover(e, notSample_lm, notSample_ln,
                                         notSample_rm, notSample_rn,
                                         ns_payload)
                        matrixB[i][j] = inter_pixel + e  # x
                        p += tmp
                    b_index += 1

    dataList = ns_payload + s_payload
    margin_len = (width + height) * 2 - 4
    margin_bits = dataList[0:margin_len]
    Ainfo = dataList[margin_len:]

    # recovery margin B
    k = 0
    for i in range(width):
        matrixB[0][i] = utils.replace_lowbit(matrixB[0][i], margin_bits[k])
        k += 1
    for i in range(width):
        matrixB[height - 1][i] = utils.replace_lowbit(matrixB[height - 1][i],
                                                      margin_bits[k])
        k += 1
    for i in range(1, height - 1):
        matrixB[i][0] = utils.replace_lowbit(matrixB[i][0], margin_bits[k])
        k += 1
    for i in range(1, height - 1):
        matrixB[i][width - 1] = utils.replace_lowbit(matrixB[i][width - 1],
                                                     margin_bits[k])
        k += 1

    return matrixB, Ainfo, embedRound
예제 #4
0
def encode(matrixB, infoList, Aindex, embedRound):
    sample_lm = sample_rm = sample_ln = sample_rn = 0
    dataList = get_margin_pixel(matrixB) + infoList
    dataSize = len(dataList)
    p = 0
    height, width = matrixB.shape
    boundary_map = []
    nextInfoList = []

    interpolation_matrixB = utils.generate_interpolation_image(
        matrixB, 45, utils.is_non_sample_pixel_first)
    interpolation_matrixB = utils.generate_interpolation_image(
        interpolation_matrixB, 0, utils.is_non_sample_pixel_second)
    error = matrixB - interpolation_matrixB

    notSample_lm, notSample_rm, notSample_ln, notSample_rn = findKey(
        error, utils.is_non_sample_pixel)

    for i in range(1, height - 1):
        for j in range(1, width - 1):
            # 将信息插入非采样像素
            if p < dataSize and utils.is_non_sample_pixel(i, j):
                if matrixB[i][j] == 0 or matrixB[i][j] == 255:
                    boundary_map.append(0)
                else:
                    error[i][j], flag = additive_expansion(
                        notSample_lm, notSample_ln, notSample_rm, notSample_rn,
                        dataList[p], error[i][j])
                    p += flag
                    if interpolation_matrixB[i][j] + error[i][
                            j] == 0 or interpolation_matrixB[i][j] + error[i][
                                j] == 255:
                        boundary_map.append(1)

    interpolation_matrixB += error
    l = len(boundary_map)
    # p为已插入的data bit数
    np_size = p

    if p < dataSize:
        # 使用sample pixel插值
        interpolation_matrixB_withSample = utils.generate_interpolation_image(
            interpolation_matrixB, 0, utils.is_sample_pixel)
        error = interpolation_matrixB - interpolation_matrixB_withSample
        sample_lm, sample_rm, sample_ln, sample_rn = findKey(
            error, utils.is_sample_pixel)

        for i in range(1, height - 1):
            for j in range(1, width - 1):
                if p < dataSize and utils.is_sample_pixel(i, j):
                    # ?
                    if interpolation_matrixB[i][
                            j] == 0 or interpolation_matrixB[i][j] == 255:
                        boundary_map.append(0)
                    else:
                        error[i][j], flag = additive_expansion(
                            sample_lm, sample_ln, sample_rm, sample_rn,
                            dataList[p], error[i][j])
                        p += flag

                        if interpolation_matrixB_withSample[i][j] + error[i][
                                j] == 0 or interpolation_matrixB_withSample[i][
                                    j] + error[i][j] == 255:
                            boundary_map.append(1)
        interpolation_matrixB_withSample += error
        interpolation_matrixB = interpolation_matrixB_withSample

    sp_size = p - np_size
    # 容量不足
    if p < dataSize:
        nextInfoList = dataList[p:]
        #raise Exception('容量不足')
        # print('容量不足')
        # return interpolation_matrixB_withSample

    #一轮插值完毕
    # print('encode')
    # print('ns_lm, ns_ln, ns_rm, ns_rn: {0}, {1}, {2}, {3}'.format(notSample_lm, notSample_ln, notSample_rm, notSample_rn))
    # print('s_lm, s_ln, s_rm, s_rn: {0}, {1}, {2}, {3}'.format(sample_lm, sample_ln, sample_rm, sample_rn))
    # print('np_size, sp_size: {0}, {1}'.format(np_size, sp_size))
    # print('length: {}'.format(l))
    # print('BoundaryMap: {}', boundary_map)
    # print('dataList: {}'.format(dataList))

    embedRound_bits = utils.int2bits9(embedRound)
    Aindex_bits = utils.int2bits_u32(Aindex)
    sample_lm_bits = utils.int2bits9(sample_lm)
    sample_ln_bits = utils.int2bits9(sample_ln)
    sample_rm_bits = utils.int2bits9(sample_rm)
    sample_rn_bits = utils.int2bits9(sample_rn)
    notSample_lm_bits = utils.int2bits9(notSample_lm)
    notSample_ln_bits = utils.int2bits9(notSample_ln)
    notSample_rm_bits = utils.int2bits9(notSample_rm)
    notSample_rn_bits = utils.int2bits9(notSample_rn)
    l_bits = utils.int2bits_u32(l)
    np_size_bits = utils.int2bits_u32(np_size)
    sp_size_bits = utils.int2bits_u32(sp_size)
    overhead = Aindex_bits + sample_lm_bits + sample_ln_bits + sample_rm_bits + sample_rn_bits + notSample_lm_bits + notSample_ln_bits + notSample_rm_bits + notSample_rn_bits
    overhead = overhead + l_bits + sp_size_bits + np_size_bits + embedRound_bits + boundary_map

    # 边界像素不足
    if len(overhead) > (width + height) * 2 - 4:
        raise Exception('边界像素不足')
        # print('边界像素不足')
        # return interpolation_matrixB
    i = 1
    while i <= len(overhead):
        if i <= width:
            interpolation_matrixB[0][i - 1] = utils.replace_lowbit(
                interpolation_matrixB[0][i - 1], overhead[i - 1])
        elif i <= 2 * width:
            interpolation_matrixB[height -
                                  1][i - width - 1] = utils.replace_lowbit(
                                      interpolation_matrixB[height -
                                                            1][i - width - 1],
                                      overhead[i - 1])
        elif i <= 2 * width + height - 2:
            interpolation_matrixB[i - 2 * width][0] = utils.replace_lowbit(
                interpolation_matrixB[i - 2 * width][0], overhead[i - 1])
        elif i <= 2 * width + 2 * height - 4:
            interpolation_matrixB[i - 2 * width - height +
                                  2][width - 1] = utils.replace_lowbit(
                                      interpolation_matrixB[i - 2 * width -
                                                            height + 2][width -
                                                                        1],
                                      overhead[i - 1])
        else:
            raise Exception('边缘地区容量不足')
            #print('边缘地区容量不足')
        i += 1
    return interpolation_matrixB, nextInfoList