Esempio n. 1
0
def Embed_mod(test,num,directory,mod_num):
    for i in range(num):
        if not os.path.exists(test+"{:08d}".format(i)) :                            #建立路徑(資料夾)
            os.mkdir(test+"{:08d}".format(i))
        image = io.imread(directory+'\\'+test+"{:08d}.png".format(i))
        io.imsave(os.path.join(test+"{:08d}".format(i),test+'{:08d}.png'.format(i)),image)
        location_map=[]
        f = open(os.path.join(test+"{:08d}".format(i),test+'{:08d}_loaction map.txt'.format(i)),'w')                        #紀錄location.txt
        for col in range(image.shape[0]):                                           #輸入原圖,記錄location map(0:有嵌密,1:沒嵌密),(255,255,255)為白色不可嵌密處
            location_map.append([])                                                
            for row in range(image.shape[1]):
                location_map[col].append([])
                if image[col,row,0] == 255 and image[col, row,1] == 255 and image[col,row,2] == 255: 
                    location_map[col][row]= 1
                else :
                    location_map[col][row]= 0        
                f.write(str(location_map[col][row]))
            f.write('\n')
        f.close()
        image_lc = io.imread(directory+'\\'+test+"{:08d}.png".format(i))                           #標記嵌密的地方的圖(紅:有嵌密,白,沒嵌密)
        for col in range(image_lc.shape[0]):
            for row in range(image_lc.shape[1]):
                if location_map[col][row] == 0 :
                    image_lc[col,row] = [255,0,0]
                else :
                    image_lc[col,row] = [255,255,255]
        io.imsave(os.path.join(test+"{:08d}".format(i),test+'{:08d}_lc.png'.format(i)),image_lc)
        image_embed = io.imread(directory+'\\'+test+"{:08d}.png".format(i))                        #嵌密
        f = open(os.path.join(test+"{:08d}".format(i),test+'{:08d}_code.txt'.format(i)),'w')
        for col in range(image_embed.shape[0]):
            for row in range(image_embed.shape[1]):
                if image_lc[col,row,0] == 255 and image_lc[col,row,1] == 0 and image_lc[col,row,2] == 0:    #在可嵌密處嵌密
                    image_embed[col,row,0],code=Mod(image_embed[col,row,0],mod_num)
                    f.write(str(code))
                    image_embed[col,row,1],code=Mod(image_embed[col,row,1],mod_num)
                    f.write(str(code))
                    image_embed[col,row,2],code=Mod(image_embed[col,row,2],mod_num)
                    f.write(str(code))
        f.close()
        io.imsave(os.path.join(test+"{:08d}".format(i),test+'{:08d}_embed.png'.format(i)),image_embed) 
        PH(os.path.join(test+"{:08d}".format(i),test+'{:08d}.png'.format(i))    ,    os.path.join(test+"{:08d}".format(i),test+'{:08d}_embed.png'.format(i))    ,    os.path.join(test+"{:08d}".format(i),test+'{:08d}直方圖.xlsx'.format(i))  )      
Esempio n. 2
0
def Embed_mod(in_dir, out_dir, mod_num, num):
    print("embed 688 pixels")  #嵌密688pixels
    if not os.path.exists(out_dir):  #建立路徑(資料夾)
        os.mkdir(out_dir)
    if not os.path.exists(out_dir +
                          "/_Cover"):  #-----------建立路徑_Cover,用來進行IQA比較
        os.mkdir(out_dir + "/_Cover")
    if not os.path.exists(out_dir +
                          "/_Stego"):  #-----------建立路徑_Stego,用來進行IQA比較
        os.mkdir(out_dir + "/_Stego")
    for i in range(num):
        if not os.path.exists(out_dir + "/output{:08d}".format(i)
                              ):  #-----------建立路徑(各圖片子資料夾)
            os.mkdir(out_dir + "/output{:08d}".format(i))
        image = io.imread(in_dir + "/output{:08d}.png".format(i))
        io.imsave(out_dir + "/output{:08d}".format(i) +
                  "/output{:08d}.png".format(i),
                  image)  #-----------從in_dir複製原圖到out_dir
        io.imsave(out_dir + "/_Cover" + "/output{:08d}.png".format(i),
                  image)  #-----------從in_dir複製原圖到_Cover資料夾(用來進行IQA比較)
        location_map = []
        f = open(out_dir + "/output{:08d}".format(i) +
                 "/output{:08d}_lc.txt".format(i), 'w')  #-----------紀錄lc.txt
        image_lc = io.imread(in_dir + "/output{:08d}.png".format(i)
                             )  #-----------記錄lc.png(png檔)(紅:有嵌密,白:沒嵌密)
        for row in range(
                image.shape[0]
        ):  #輸入原圖,記錄lc.txt,(255,255,255)為白色不可嵌密處=1,其他地方設為紅色可嵌密處=0
            location_map.append([])
            for col in range(image.shape[1]):
                location_map[row].append([])
                if image[row, col, 0] == 255 and image[
                        row, col, 1] == 255 and image[row, col,
                                                      2] == 255:  #白色區域的lc
                    location_map[row][col] = 1
                    image_lc[row, col] = [255, 255, 255]
                else:  #其他紅色區域的lc
                    location_map[row][col] = 0
                    image_lc[row, col] = [255, 0, 0]
                f.write(str(location_map[row][col]))
        f.close()
        io.imsave(
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}_lc.png".format(i), image_lc)
        image_embed = io.imread(in_dir + "/output{:08d}.png".format(i))  #嵌密
        f = open(out_dir + "/output{:08d}".format(i) +
                 "/output{:08d}_code.txt".format(i), 'w')  #紀錄秘密訊息
        pixel_num = 0  #pixel_num用來處理嵌密率(僅在可嵌區域中遞增)
        embed_count = 0
        for row in range(image_embed.shape[0]):
            for col in range(image_embed.shape[1]):
                if location_map[row][col] != 1:  #非白區可嵌密
                    if (
                            pixel_num % 9
                    ) < 1 and embed_count < 688:  #pixel_num對9取餘數,每9嵌1,共688(確保擁有最小可嵌區域pixel為6800的影像仍能嵌滿688)
                        image_embed[row, col,
                                    0], code = Mod(image_embed[row, col, 0],
                                                   mod_num)  #回傳兩值(嵌密,祕密訊息)
                        f.write(str(code))
                        image_embed[row, col,
                                    1], code = Mod(image_embed[row, col, 1],
                                                   mod_num)
                        f.write(str(code))
                        image_embed[row, col,
                                    2], code = Mod(image_embed[row, col, 2],
                                                   mod_num)
                        f.write(str(code))
                        embed_count += 1  #embed_count紀錄實際有嵌密的pixel數量(僅檢驗用)
                    pixel_num += 1
        print(embed_count)
        f.close()
        ########################################################################################################################################################################檢查非白
        location_map2 = []
        f2 = open(out_dir + "/output{:08d}".format(i) +
                  "/output{:08d}_lc2.txt".format(i),
                  'w')  #-----------紀錄lc2.txt
        for row in range(
                image.shape[0]
        ):  #輸入原圖,記錄lc2.txt,(255,255,255)為白色不可嵌密處=1,其他地方設為紅色可嵌密處=0
            location_map2.append([])  #初始化lc2[256][256]
            for col in range(image.shape[1]):
                location_map2[row].append([])
                if image_embed[row, col, 0] == 255 and image_embed[
                        row, col,
                        1] == 255 and image_embed[row, col,
                                                  2] == 255:  #白色區域的lc2
                    location_map2[row][col] = 1
                else:  #其他紅色區域的lc2
                    location_map2[row][col] = 0
                f2.write(str(location_map2[row][col]))
        change_num = 0  #change_num紀錄修正的像素數量
        for row in range(image_embed.shape[0]):
            for col in range(image_embed.shape[1]):  #再次檢查stego
                if location_map[row][col] == 0 and location_map2[row][
                        col] == 1:  #對兩個lm進行檢查,若原本可嵌密的地方變成白區(255,255,255),就代表此處要被調整B=B-3
                    image_embed[row, col,
                                2] = image_embed[row, col, 2] - mod_num
                    change_num = change_num + 1
        io.imsave(out_dir + "/output{:08d}".format(i) +
                  "/output{:08d}_embed.png".format(i),
                  image_embed)  #------------儲存嵌密圖片
        io.imsave(out_dir + "/_Stego" + "/output{:08d}_embed.png".format(i),
                  image_embed)  #------------儲存嵌密圖片到_Stego資料夾(用來進行IQA比較)
        PH(
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}.png".format(i)  #使用畫直方圖.py
            ,
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}_embed.png".format(i),
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}直方圖.xlsx".format(i))
        f2.close()
        f3 = open(
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}_change.txt".format(i), 'w')
        f3.write(str(change_num) + '\n')
        for row in range(image_embed.shape[0]):
            for col in range(image_embed.shape[1]):  #再次檢查stego
                if location_map[row][col] == 0 and location_map2[row][col] == 1:
                    f3.write('1')
                else:
                    f3.write('0')
        f3.close()
def Embed_mod(in_dir, out_dir, edge_dir, num, mod_num):
    print("embed ratio = 100%")  #嵌密率50%
    if not os.path.exists(out_dir):  #建立路徑(資料夾)
        os.mkdir(out_dir)
    if not os.path.exists(out_dir +
                          "/_Cover"):  #-----------建立路徑_Cover,用來進行IQA比較
        os.mkdir(out_dir + "/_Cover")
    if not os.path.exists(out_dir +
                          "/_Stego"):  #-----------建立路徑_Stego,用來進行IQA比較
        os.mkdir(out_dir + "/_Stego")
    for i in range(num):
        if not os.path.exists(out_dir + "/output{:08d}".format(i)
                              ):  #-----------建立路徑(各圖片子資料夾)
            os.mkdir(out_dir + "/output{:08d}".format(i))
        image = io.imread(in_dir + "/output{:08d}.png".format(i))
        io.imsave(out_dir + "/output{:08d}".format(i) +
                  "/output{:08d}.png".format(i),
                  image)  #-----------從in_dir複製原圖到out_dir
        io.imsave(out_dir + "/_Cover" + "/output{:08d}.png".format(i),
                  image)  #-----------從in_dir複製原圖到_Cover資料夾(用來進行IQA比較)
        location_map = []
        f = open(out_dir + "/output{:08d}".format(i) +
                 "/output{:08d}_location map.txt".format(i),
                 'w')  #-----------紀錄location.txt
        image_lc = io.imread(
            in_dir + "/output{:08d}.png".format(i)
        )  #-----------記錄location map(png檔)(紅:有嵌密,白:沒嵌密,黑:框(有嵌密且解密時要用到))
        image_edge = io.imread(edge_dir)  #讀取框的資料
        for row in range(
                image.shape[0]
        ):  #輸入原圖,記錄location map(txt檔),(255,255,255)為白色不可嵌密處=1,(0,0,0)為黑框可嵌密處=2,其他地方設為紅色可嵌密處=0
            location_map.append([])  #將黑框資訊擷取出來後,可在解密時使用
            for col in range(image.shape[1]):
                location_map[row].append([])
                if image_edge[row, col, 0] == 0 and image_edge[
                        row, col, 1] == 0 and image_edge[row, col,
                                                         2] == 0:  #黑框的lc
                    location_map[row][col] = 2
                    image_lc[row, col] = [0, 0, 0]
                elif image[row, col, 0] == 255 and image[
                        row, col, 1] == 255 and image[row, col,
                                                      2] == 255:  #白色區域的lc
                    location_map[row][col] = 1
                    image_lc[row, col] = [255, 255, 255]
                else:  #其他紅色區域的lc
                    location_map[row][col] = 0
                    image_lc[row, col] = [255, 0, 0]
                f.write(str(location_map[row][col]))
        f.close()
        io.imsave(
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}_lc.png".format(i), image_lc)
        image_embed = io.imread(in_dir + "/output{:08d}.png".format(i))  #嵌密
        f = open(
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}_code.txt".format(i), 'w')
        pixel_num = 0  #pixel_num用來處理嵌密率(僅在可嵌區域中遞增)
        embed_count = 0
        for row in range(image_embed.shape[0]):
            for col in range(image_embed.shape[1]):
                if location_map[row][col] != 1:  #pixel_num對100取餘數,如果小於嵌密率,就進行嵌密
                    if (pixel_num % 2) < 2:  #紅黑可嵌密
                        image_embed[row, col,
                                    0], code = Mod(image_embed[row, col, 0],
                                                   mod_num)
                        f.write(str(code))
                        image_embed[row, col,
                                    1], code = Mod(image_embed[row, col, 1],
                                                   mod_num)
                        f.write(str(code))
                        image_embed[row, col,
                                    2], code = Mod(image_embed[row, col, 2],
                                                   mod_num)
                        f.write(str(code))
                        embed_count += 1  #embed_count紀錄實際有嵌密的pixel數量(僅檢驗用)
                    pixel_num += 1
        print(embed_count)
        f.close()
        io.imsave(out_dir + "/output{:08d}".format(i) +
                  "/output{:08d}_embed.png".format(i),
                  image_embed)  #------------儲存嵌密圖片
        io.imsave(out_dir + "/_Stego" + "/output{:08d}_embed.png".format(i),
                  image_embed)  #------------儲存嵌密圖片到_Stego資料夾(用來進行IQA比較)
        PH(
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}.png".format(i)  #使用畫直方圖.py
            ,
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}_embed.png".format(i),
            out_dir + "/output{:08d}".format(i) +
            "/output{:08d}直方圖.xlsx".format(i))