二元圖資料隱藏-hw4

本文介绍了一种简单的二元图像隐藏方法,通过将Lenna图像嵌入到Boat图像中来实现。具体步骤包括:使用文件操作读取原始图像数据,通过修改Boat图像像素的最低位来隐藏Lenna图像,最后将隐藏后的图像和提取出的Lenna图像写入文件。

目的:簡單的二元圖資料隱藏,將lenna二元圖藏入到boat中,然後再把它分出來

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define sizein         512
 5 unsigned char          boat[sizein][sizein];
 6 unsigned char          lena[sizein][sizein];
 7 unsigned char          hboat[sizein][sizein]; 
 8 
 9 FILE                   *ifile1,*ifile2,*ofile,*hfile;
10 
11 
12 
13 int main(int argc,char **argv)
14 {  
15 
16     int  i,j,k,z;
17 
18     if ((ifile1=fopen("512Fishing_Boat.raw","rb"))==NULL) { printf("Unable to open input  graph file\n",argv[1]); exit(1); }
19     if ((ifile2=fopen("512Lenna_error.raw","rb"))==NULL) { printf("Unable to open input  graph file\n",argv[1]); exit(1); }
20     if ((ofile=fopen("combine_finish.raw","wb"))==NULL) { printf("Unable to open output graph file\n","file1"); exit(1); }
21     if ((hfile=fopen("divide_finish.raw","wb"))==NULL) { printf("Unable to open output graph file\n","file1"); exit(1); }
22     
23    for(i=0;i<sizein;i++)
24    {
25        for(j=0;j<sizein;j++)
26        {
27             // 輸入資料
28             boat[i][j]=fgetc(ifile1);
29             lena[i][j]=fgetc(ifile2); // 0 255
30             
31             // 用 +1 -1 的方式藏入最後一個 bit
32             if(boat[i][j]%2==lena[i][j]%2);
33             else if(lena[i][j]%2==1) // 兩者不相等  又 lena[i][j] 為 1
34                 boat[i][j]=boat[i][j]+1;
35             else                       // 兩者不相等  又 lena[i][j] 為 0
36                 boat[i][j]=boat[i][j]-1;
37             
38             
39             // 將藏好的圖還原
40             int tmp=boat[i][j]%2;
41             if(tmp==1)
42                 hboat[i][j]=255;
43             else
44                 hboat[i][j]=0;
45        } 
46    }
47    
48    
49     fwrite(boat,  sizeof(unsigned char), sizein*sizein, ofile); // 藏好圖的船
50     fwrite(hboat, sizeof(unsigned char), sizein*sizein, hfile); // 還原的被隱藏的lena圖
51     
52     fclose(ifile1);
53     fclose(ifile2);
54     fclose(ofile);
55     fclose(hfile);
56     
57     system("pause");
58     return 0;
59 }

 

輸入 : 漁船、lenna

輸出 : 藏入後的漁船、解出來的lenna

程式解說:

1.先將漁船、lenna資料輸入

27             // 輸入資料
28             boat[i][j]=fgetc(ifile1);
29             lena[i][j]=fgetc(ifile2); // 0 255

2.要將二元圖藏入灰階圖,意思就是藏到最後一個bit裡面去,在這邊可以用十進位的加減1完成這個動作,目前我把它加到原圖boat去

31             // 用 +1 -1 的方式藏入最後一個 bit
32             if(boat[i][j]%2==lena[i][j]%2);
33             else if(lena[i][j]%2==1) // 兩者不相等  又 lena[i][j] 為 1
34                 boat[i][j]=boat[i][j]+1;
35             else                       // 兩者不相等  又 lena[i][j] 為 0
36                 boat[i][j]=boat[i][j]-1;

3.最後將圖還原出來放到hboat中,因為1在MinGw代表255,所以需要用if判斷式判斷

39             // 將藏好的圖還原
40             int tmp=boat[i][j]%2;
41             if(tmp==1)
42                 hboat[i][j]=255;
43             else
44                 hboat[i][j]=0;

4.完成輸出藏入後的漁船、解出來的lenna 

49     fwrite(boat,  sizeof(unsigned char), sizein*sizein, ofile); // 藏好圖的船
50     fwrite(hboat, sizeof(unsigned char), sizein*sizein, hfile); // 還原的被隱藏的lena圖

 

转载于:https://www.cnblogs.com/zcxow/archive/2012/12/26/2833985.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值