VQ压缩与信息隐藏——Matlab和Python实现

系列文章目录

仿射密码实验-Python实现
仿射密码实验——Python实现(完整解析版)
DES密码实验-C语言实现
MD5密码实验——Python实现(完整解析版)
LSB信息隐藏——Python实现(完整解析版)


前言

1)了解VQ压缩
2)掌握VQ压缩步骤与信息隐藏的实现


实验内容与操作步骤

1.根据VQ算法,完成VQ算法的实现。

包括:VQ压缩、VQ解压缩、PSNR计算。

2.基于VQ的隐藏方法,思考在VQ压缩中可进行的信息隐藏方法。

描述相关方法,并实现。包括:信息嵌入、信息取出、图像还原、PSNR计算。

实验步骤

1. VQ压缩

Matlab代码实现VQ压缩、VQ解压缩、PSNR计算
读取图像,将图像分割成4*4的小方块(block)
在这里插入图片描述

Size函数读入矩阵的行和列,读取编码簿以便后面的计算
在这里插入图片描述

预定义常量和变量

在这里插入图片描述

For循环来计算欧几里得距离,每个行列式与编码簿里的值进行计算,距离最小的进行保存
在这里插入图片描述
在这里插入图片描述

运行后写入到vq的txt文件中
在这里插入图片描述

读入写好的vq的txt文件
在这里插入图片描述

通过编写函数来计算PSNR信噪比
在这里插入图片描述

2.基于VQ的隐藏方法

Python实现
先生成后缀为bmp的灰度图文件
在这里插入图片描述

对于密文的处理方式,最终要转换成二进制才能进行嵌入隐藏,
所以要先将密文进行ascii编码,然后转换成二进制流写入;
将codebook分成两组:0偶数组,1奇数组;
Secret 0:用第一组中最小ED值的码数进行编码;
Secret 1:用第二组中最小ED值的码数进行编码。
核心代码
在这里插入图片描述
在这里插入图片描述

实验结果

VQ压缩、VQ解压缩、PSNR计算。

运行代码,得到vq图片,与原图进行对比,
在这里插入图片描述

计算得出的PSNR为30.2056

在这里插入图片描述

基于vq的隐藏方法

生成bmp文件
在这里插入图片描述
在这里插入图片描述

运行后得出结果

在这里插入图片描述
在这里插入图片描述

可以看出信噪比的值也在30左右的范围,可以接受

实验心得

在vq解压缩实验中,通过查阅资料了解到,在有损图像和视频压缩中,PSNR的典型值在30到50 dB之间,前提是位深度为8位,位深度越高越好。
而本次实验的PSNR值处于范围内,算是可接受值。而在隐藏信息的算法中,使用python代码进行处理的PSNR值与matlab的代码相差无几,但处理时间上python所花费的时间比matlab超出很多,说明了在此次处理图像上matlab更具优越性,希望以后改进python的vq压缩算法,提升数据处理的速度。

附录代码及文件

codebook.txt

21 21 21 22 21 21 21 22 21 21 21 22 22 21 21 22 
22 22 22 29 22 23 23 31 28 29 30 39 44 46 47 58 
32 32 32 33 32 32 32 33 32 32 32 32 32 32 32 33 
59 46 43 41 42 32 30 29 32 25 25 24 29 25 24 24 
42 42 42 43 41 41 41 42 41 41 41 41 41 41 41 41 
53 35 38 34 58 37 37 33 67 42 38 34 77 50 42 37 
57 53 51 48 55 52 50 48 47 44 43 43 38 36 36 36 
46 45 44 45 46 45 45 46 47 47 47 47 48 48 47 48 
66 70 74 77 50 52 55 59 35 35 37 41 29 28 29 32 
33 34 45 65 33 34 46 72 32 35 51 82 34 39 58 89 
51 51 52 54 49 49 50 52 49 48 49 51 48 48 49 50 
80 60 47 42 75 54 43 39 67 48 40 37 63 47 41 37 
31 31 31 31 40 39 39 41 58 59 60 61 76 77 78 79 
42 48 56 63 41 49 58 64 42 51 60 66 45 53 60 65 
52 51 51 50 54 54 53 53 56 56 55 55 58 58 57 57 
61 60 58 58 60 59 58 56 58 57 55 54 57 55 53 53 
137 100 57 46 107 64 42 42 73 43 39 41 55 39 40 42 
62 52 48 47 71 59 50 47 83 69 56 50 89 78 63 54 
60 61 62 64 59 60 62 64 59 60 62 64 59 60 62 64 
89 78 53 38 90 79 51 36 89 77 50 36 85 73 50 38 
68 67 66 66 68 68 66 66 68 68 67 67 69 68 67 67 
54 81 84 50 53 82 84 49 53 82 84 49 54 82 84 53 
77 80 85 89 69 71 73 75 62 61 61 62 56 54 52 53 
62 59 59 62 67 63 61 63 80 70 67 67 96 85 77 74 
60 71 79 82 57 68 78 82 55 66 76 81 54 63 74 80 
46 55 77 107 45 54 78 109 45 55 78 105 47 59 81 101 
92 97 87 74 87 86 74 64 73 67 60 56 63 56 54 54 
48 40 36 35 84 76 67 62 98 91 87 82 92 90 91 91 
89 72 62 63 70 61 62 68 59 62 74 94 59 67 92 117 
75 75 74 73 75 75 74 73 73 74 74 73 71 72 73 73 
53 57 62 66 60 67 73 77 70 79 85 86 77 86 90 90 
117 92 70 65 94 79 67 63 72 70 66 64 65 64 66 65 
85 78 72 67 86 78 70 65 87 77 68 64 85 75 68 63 
103 74 61 59 102 71 59 59 102 71 62 60 105 76 67 62 
77 76 66 54 84 85 74 58 87 90 80 63 82 87 83 67 
73 57 53 53 94 65 53 52 128 90 61 55 146 114 75 62 
72 77 80 83 71 76 80 83 72 76 79 81 73 76 78 80 
78 78 77 77 79 78 77 77 81 79 77 75 82 79 77 75 
121 127 129 127 80 82 83 84 57 55 55 57 51 50 50 51 
63 64 67 70 61 60 62 66 73 73 75 79 113 116 117 119 
116 75 71 93 116 64 62 89 111 60 58 81 100 59 57 75 
77 79 81 82 79 81 83 84 81 82 84 85 82 83 84 86 
97 92 89 87 88 84 82 81 81 78 77 77 76 74 75 75 
44 43 44 48 51 51 55 61 82 86 96 103 124 134 145 148 
76 85 101 117 71 77 90 106 67 70 79 92 66 68 72 82 
89 82 76 73 92 84 78 75 94 87 79 75 96 88 81 77 
141 136 93 53 140 118 69 43 121 87 49 43 100 65 45 45 
103 98 68 46 102 106 74 47 103 110 83 51 102 112 92 61 
82 95 91 72 90 104 92 69 93 102 85 64 93 94 75 60 
84 85 85 86 84 86 88 89 84 86 89 91 83 86 89 91 
66 70 77 87 66 74 85 100 69 81 100 119 73 90 112 128 
94 89 87 85 93 90 87 84 91 88 86 84 90 87 85 82 
87 87 95 106 87 78 79 88 97 82 74 79 112 94 81 79 
55 87 118 101 72 109 113 76 101 119 90 50 115 101 62 39 
143 117 87 75 113 92 76 75 87 77 77 82 74 75 81 90 
76 72 71 71 88 83 79 77 104 98 92 88 113 110 103 97 
77 86 96 105 77 87 97 103 79 87 95 100 80 86 93 99 
89 89 89 88 90 90 90 89 92 93 92 90 94 95 94 93 
96 100 100 100 90 94 96 96 85 87 89 92 81 82 84 86 
111 111 105 96 106 104 95 86 97 90 81 76 86 78 72 69 
45 63 108 116 52 90 127 97 67 121 124 71 93 133 100 56 
121 83 72 73 128 86 71 70 135 92 75 72 135 96 79 76 
104 99 92 86 104 98 91 85 103 97 90 86 104 99 93 88 
94 94 94 96 94 94 95 96 94 95 96 97 94 95 96 97 
125 120 100 73 126 117 91 66 120 109 84 62 110 98 79 60 
85 85 92 101 90 88 95 103 101 96 99 104 109 105 102 104 
65 85 125 155 60 77 118 152 57 70 105 140 57 69 96 128 
100 100 99 99 99 98 97 97 98 97 96 97 97 97 96 97 
93 104 109 106 87 100 111 109 81 94 106 108 77 87 100 104 
91 88 80 77 96 100 95 89 97 108 115 110 94 109 126 128 
133 121 111 107 112 103 97 96 97 92 90 90 91 89 89 89 
104 100 93 90 115 109 100 94 117 109 101 97 104 96 91 91 
101 101 101 102 101 101 101 101 101 101 101 101 101 101 100 100 
98 98 100 103 97 98 102 106 98 101 106 109 100 105 109 112 
103 110 121 132 94 96 105 117 94 92 94 103 96 93 94 98 
88 89 97 117 87 88 102 126 87 89 107 134 90 93 112 137 
113 103 81 66 126 116 92 69 131 126 102 76 130 129 110 85 
97 94 95 95 101 97 96 96 111 105 102 102 124 118 113 109 
160 109 62 60 172 117 61 59 177 125 66 60 175 131 74 62 
65 64 64 65 84 83 83 84 120 121 120 120 150 151 150 148 
115 117 114 107 111 110 102 95 109 109 98 86 111 111 97 84 
83 112 118 89 89 124 125 84 99 133 123 82 101 125 111 79 
105 106 105 105 105 105 105 105 104 104 105 105 104 104 105 106 
161 163 158 151 124 124 121 116 82 78 77 77 67 63 63 63 
88 151 133 83 76 116 128 104 84 92 117 121 92 77 102 126 
63 86 107 119 67 97 122 128 73 109 133 133 83 116 133 130 
106 103 99 97 101 98 99 104 99 100 109 121 105 109 124 135 
126 125 122 119 110 110 109 111 96 98 101 106 92 93 98 102 
112 110 111 110 110 109 108 108 110 108 106 105 109 106 104 103 
161 122 93 94 152 108 85 93 138 99 85 93 128 98 87 95 
103 103 105 107 105 107 108 110 108 110 111 112 111 112 112 112 
127 118 105 97 131 118 103 93 126 115 100 90 120 110 97 89 
98 90 101 116 117 96 94 107 144 113 92 96 160 129 97 92 
118 110 101 93 121 113 101 93 125 118 105 95 129 122 108 97 
163 114 82 96 144 106 89 104 118 101 105 114 98 100 115 118 
102 95 89 85 113 108 102 95 124 122 118 108 130 131 128 118 
105 107 113 117 105 108 112 119 105 107 113 118 105 108 112 116 
101 124 127 105 104 126 126 105 103 119 119 103 99 110 111 94 
111 112 112 112 111 112 112 111 111 111 111 110 112 111 111 111 
116 115 116 115 119 118 116 110 121 119 113 102 119 114 105 94 
109 111 120 126 117 124 133 137 107 113 120 122 93 91 94 100 
116 116 115 115 115 115 114 115 113 113 113 114 112 112 112 114 
103 109 116 118 105 110 109 106 116 117 114 108 125 129 126 116 
132 117 110 108 126 114 109 108 121 111 110 111 117 110 111 114 
115 112 108 108 108 111 114 117 104 113 120 125 106 115 126 131 
126 131 135 135 116 120 122 121 107 108 106 108 101 100 100 102 
85 86 93 101 104 108 116 122 122 126 132 134 125 129 130 129 
112 114 117 119 112 114 117 120 112 113 117 118 114 114 116 117 
115 115 115 114 117 116 115 114 117 116 116 115 117 118 117 116 
117 118 118 118 118 118 117 118 118 117 117 117 116 115 117 119 
113 112 112 115 116 116 117 118 120 119 119 120 122 121 120 121 
104 113 123 129 102 113 124 131 102 113 125 132 103 113 124 132 
126 124 115 108 122 122 117 110 119 121 120 114 117 120 121 117 
123 111 91 76 132 139 129 96 112 131 143 127 93 110 139 142 
118 135 153 162 98 117 142 156 82 96 120 140 76 84 98 118 
118 120 120 120 117 119 120 120 116 119 120 120 117 118 119 118 
150 145 138 128 133 124 116 111 118 109 105 105 109 103 104 105 
128 133 131 132 91 92 91 95 109 106 104 102 151 153 148 144 
97 96 117 146 109 100 116 147 126 111 113 139 136 117 113 128 
167 152 122 99 164 139 106 89 153 123 96 84 136 109 90 85 
129 122 115 111 130 121 113 110 134 124 114 110 137 126 115 109 
121 122 121 122 122 123 122 122 121 120 120 119 118 117 117 116 
83 159 151 86 83 162 158 87 81 159 155 87 81 153 149 89 
108 104 103 104 115 110 107 110 133 126 122 123 147 141 137 137 
131 124 100 74 137 137 114 82 135 142 131 98 132 140 137 115 
119 119 121 121 119 118 120 121 120 120 121 122 122 122 123 123 
75 90 130 163 74 91 137 174 75 95 143 178 81 104 147 176 
122 122 120 119 122 123 120 119 122 122 120 120 121 122 121 121 
127 127 126 124 123 123 123 121 119 119 120 118 118 119 118 117 
117 105 98 106 112 104 104 119 112 115 127 145 122 136 154 168 
110 113 118 119 120 125 129 127 124 127 129 127 120 122 122 122 
120 123 126 129 118 120 125 129 116 119 123 128 117 119 122 126 
123 123 123 123 123 123 123 122 124 123 123 123 124 124 124 123 
123 129 135 132 126 134 134 122 130 134 127 106 124 121 108 92 
136 133 125 119 134 131 124 116 129 126 120 116 122 121 117 114 
113 114 117 121 113 115 120 124 119 123 130 134 127 133 140 145 
122 123 123 125 124 125 125 126 125 125 126 127 123 124 125 126 
126 128 129 128 124 126 125 124 125 124 123 123 125 124 123 122 
128 128 128 127 122 124 127 129 121 123 127 129 121 123 124 125 
130 125 117 107 135 132 124 116 134 135 128 122 128 129 124 120 
134 131 131 138 132 127 127 133 124 119 120 127 115 112 114 122 
141 131 106 135 113 149 135 117 116 109 149 124 138 98 109 137 
136 116 110 114 138 116 116 128 130 121 129 138 124 122 135 143 
125 126 128 127 126 126 127 126 125 127 127 126 125 127 128 127 
119 137 146 145 111 131 138 132 108 126 131 124 109 124 126 119 
129 128 126 127 128 127 126 126 128 127 125 126 129 127 126 126 
123 122 125 128 124 124 127 130 126 127 129 131 127 128 129 131 
184 173 141 110 164 147 123 107 121 110 110 113 100 98 112 123 
153 110 108 148 156 107 99 148 157 110 93 144 156 114 95 140 
121 119 118 118 125 123 122 122 132 131 130 129 140 139 137 136 
110 100 97 95 133 124 116 109 157 150 135 121 166 160 144 128 
86 98 120 143 92 109 133 150 111 129 146 153 127 143 151 154 
133 156 150 129 157 166 131 104 163 149 101 87 147 114 82 91 
150 140 128 111 152 140 123 106 152 139 121 101 149 136 116 99 
81 79 81 85 107 107 108 110 150 153 154 152 171 176 176 173 
176 175 170 162 160 155 145 137 124 116 106 102 95 86 80 79 
164 138 94 79 172 148 100 82 176 158 113 88 177 162 123 96 
128 130 130 131 129 130 130 131 129 129 129 130 127 128 129 130 
118 126 133 140 118 126 134 141 118 125 134 141 117 126 134 140 
134 131 129 127 136 131 128 125 136 131 128 123 136 132 127 123 
127 138 131 109 127 141 145 128 112 131 147 144 101 119 143 145 
116 119 123 124 128 131 134 133 132 135 137 138 131 135 137 138 
158 108 104 143 148 139 93 108 124 157 123 92 153 173 165 103 
163 153 140 136 131 121 115 123 117 112 115 129 127 127 137 147 
143 144 146 146 136 134 134 135 126 124 124 125 120 119 119 120 
135 132 117 117 135 131 119 111 139 133 127 125 147 144 142 148 
90 112 144 162 113 135 156 155 133 143 143 127 138 139 118 102 
133 132 132 132 133 132 131 131 133 132 131 132 133 132 132 132 
104 140 162 170 97 133 157 169 90 122 148 164 90 115 137 155 
133 135 136 136 133 134 136 136 134 135 136 136 134 135 135 136 
124 141 159 128 116 124 160 143 130 110 140 151 150 121 123 143 
149 143 130 114 151 145 132 115 148 144 135 121 144 140 133 123 
147 152 155 150 145 152 155 152 128 134 139 141 100 104 110 119 
142 141 141 141 148 141 137 134 156 140 125 122 154 137 116 111 
141 136 131 132 142 137 132 132 142 139 135 135 142 139 137 137 
118 119 121 124 129 132 135 137 142 145 147 148 148 152 154 153 
124 129 148 167 138 125 127 142 167 143 113 115 180 159 124 105 
139 139 139 140 138 138 139 140 136 137 138 139 135 136 138 139 
136 137 154 171 126 124 142 165 121 120 135 155 126 125 136 151 
116 151 147 99 129 163 152 101 141 170 155 104 153 179 162 114 
141 141 140 140 141 141 140 140 141 141 141 140 141 141 141 140 
141 149 150 147 141 149 149 144 139 144 140 136 134 137 133 130 
115 138 144 153 116 141 150 159 116 143 155 163 116 143 154 163 
148 144 136 130 151 145 137 131 153 148 139 133 155 150 141 135 
167 166 160 156 160 153 146 143 147 136 128 128 135 124 117 119 
141 142 143 143 141 142 143 143 143 143 144 145 144 144 145 145 
147 147 146 146 146 145 145 145 143 143 143 143 141 141 141 142 
133 138 145 150 135 141 148 153 138 143 149 154 139 144 149 152 
146 140 146 155 149 140 143 155 155 141 136 150 157 143 130 143 
140 139 139 140 138 136 136 139 146 147 148 150 157 158 159 160 
146 146 146 145 147 147 146 145 147 147 146 146 147 147 146 146 
188 181 147 110 188 178 137 100 185 172 128 95 178 160 121 93 
125 125 128 132 154 156 157 154 162 164 162 157 149 150 146 142 
149 149 149 150 147 147 148 148 146 147 147 148 146 147 148 149 
160 154 145 141 157 151 142 140 155 148 142 141 154 149 145 143 
147 147 146 147 148 148 148 148 150 150 150 149 150 151 150 150 
103 122 146 156 120 140 156 161 143 159 164 162 160 167 165 160 
150 139 121 107 162 156 138 123 168 165 155 145 167 168 164 157 
145 151 155 158 142 147 154 157 140 143 152 156 140 144 150 154 
153 153 151 148 153 153 152 149 150 151 150 149 143 144 146 146 
165 166 162 145 163 164 158 137 155 157 148 126 149 150 140 119 
151 151 151 151 151 150 150 150 151 150 150 150 151 151 150 150 
173 181 183 181 160 168 173 172 132 141 150 151 104 109 118 125 
146 150 152 155 147 150 153 156 149 152 155 157 151 154 156 158 
158 158 160 161 157 158 159 160 150 150 151 152 141 141 142 143 
153 153 152 152 153 153 152 152 153 153 153 153 152 153 153 153 
156 131 153 183 153 125 156 187 146 122 157 187 137 123 159 185 
152 165 169 150 142 156 175 166 128 136 169 175 124 120 156 177 
112 113 116 120 142 146 149 152 166 172 174 174 177 182 184 182 
161 159 153 141 160 159 154 144 159 158 154 147 159 157 155 149 
168 171 173 170 152 153 156 156 140 143 146 147 146 149 151 151 
32 32 32 32 192 192 192 191 197 198 198 197 197 198 197 197 
156 156 156 156 156 155 155 156 156 155 155 156 156 156 156 156 
142 149 168 176 153 139 152 170 173 148 142 159 180 159 140 146 
176 161 145 151 178 162 140 147 178 162 138 144 176 159 140 147 
151 152 153 153 158 161 162 162 157 161 163 163 148 153 156 156 
150 148 148 148 154 153 152 152 162 161 160 159 167 166 166 165 
97 162 185 186 95 163 187 187 95 163 187 187 98 163 184 185 
129 169 168 169 127 170 169 168 126 171 169 169 126 169 168 168 
160 160 160 160 160 160 160 160 159 159 159 159 159 159 159 159 
182 163 121 85 191 181 144 97 196 192 169 123 197 196 184 149 
144 153 168 174 144 154 169 175 148 157 170 175 153 160 169 174 
199 200 200 198 195 195 193 189 172 171 167 163 93 91 89 88 
180 180 175 164 181 179 170 151 180 175 157 133 175 166 143 121 
165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 
185 186 183 181 177 176 173 173 160 157 157 161 144 143 149 154 
156 156 156 155 165 165 165 166 172 172 173 172 175 177 177 176 
171 171 171 171 171 171 171 171 171 171 171 171 170 171 171 171 
153 180 189 168 155 182 188 164 162 184 186 158 168 183 179 151 
196 196 180 144 197 196 177 134 197 195 172 127 195 191 164 125 
163 183 192 193 152 178 192 196 139 170 190 194 131 159 182 190 
177 176 176 176 177 176 175 175 177 176 175 173 176 175 174 172 
173 174 174 175 174 175 176 177 175 176 177 178 176 177 178 179 
136 143 153 161 160 168 176 181 182 188 192 194 191 195 197 197 
196 198 199 198 195 197 196 194 182 182 178 174 141 136 129 126 
193 191 177 176 195 187 169 170 193 180 159 164 189 172 160 168 
180 180 180 180 181 180 180 180 181 181 180 181 180 180 180 180 
182 184 188 190 180 183 187 190 178 181 185 189 177 180 185 189 
185 185 185 185 186 185 185 185 186 185 185 185 186 185 185 185 
197 187 166 137 201 195 181 156 203 200 194 176 202 202 198 186 
176 174 174 176 186 186 186 186 195 195 195 194 197 199 198 196 
189 189 189 189 189 189 189 189 188 189 189 189 188 188 189 189 
201 198 195 180 202 199 194 176 202 199 191 167 201 197 187 160 
191 191 190 190 192 191 191 190 192 192 191 191 192 192 192 192 
146 169 186 193 172 190 200 203 193 202 206 207 198 205 206 205 
194 194 194 195 194 194 194 195 194 194 194 194 194 194 194 194 
203 204 204 203 203 204 203 201 197 197 195 192 180 178 175 172 
178 201 202 206 177 201 203 207 174 200 202 207 171 197 201 206 
197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 
201 202 202 202 202 202 202 202 202 202 202 202 201 202 202 202 
206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 
216 218 217 216 219 220 220 218 216 215 216 212 191 188 187 180 
209 210 210 210 209 210 210 210 209 210 210 210 209 210 210 210 
208 207 204 198 214 212 211 205 216 216 214 211 217 216 216 214 
214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 
218 218 218 218 219 219 219 219 219 220 220 220 219 220 220 219 

genNeedImg.py

from cv2 import cv2 as cv
import sys

def genNeedImg(imgPath,size=None,flag='binary'):
    '''
    用于生成指定大小的灰度图或二值图, imgPath为图像路径
    size为tuple类型,用于指定生成图像的尺寸, 如:(512,512),默认为None表示输出原图像尺寸
    flag为标志转换类型,默认为binary,可选的值为binary或gray
    '''
    imgRow = cv.imread(imgPath)
    if size != None: # 调整图像尺寸
        imgRow= cv.resize(imgRow,size)
    imgGray = cv.cvtColor(imgRow,cv.COLOR_RGB2GRAY) # 转换颜色空间为灰度
    imgName = imgPath[9:].split('.')[0] # 获取图像原始名称
    if flag == 'gray': # 生成灰度图
        cv.imwrite('./images/{}_gray.bmp'.format(imgName),imgGray)
        print('Gray image generated!')
    else: # 生成二值图
        ret, imgBinary = cv.threshold(imgGray,127,255,cv.THRESH_BINARY)
        prop = int(size[0]*size[1]/(512*512)*100) # 以载体图像为512x512,算生成的水印大小占载体图的百分比
        cv.imwrite('./images/{}_binary{}.bmp'.format(imgName,prop),imgBinary) 
        print('Binary image generated!')
        print('threshold:{}'.format(ret)) # 输出转换阈值

if __name__ == "__main__":
    imgName = sys.argv[1]
    size =[int(sys.argv[2]),int(sys.argv[3])]
    flag = sys.argv[4]
    genNeedImg(imgName,tuple(size),flag=flag)

vq.py

from numpy import *
import numpy as np
from PIL import Image
import cv2

codebook = "codebook.txt"


def im2col(mtx, block_size):
    mtx_shape = mtx.shape
    sx = mtx_shape[0]
    sy = mtx_shape[1]
    # 如果设A为m×n的,对于[p q]的块划分,最后矩阵的行数为p×q,列数为m * n / p / q。
    result = np.empty((sx * sy // block_size[0] // block_size[1], block_size[0] * block_size[1]))
    # 沿着行移动,所以先保持列(i)不动,沿着行(j)走
    i = 0
    count = 0
    while i < sy:
        j = 0
        while j < sx:
            result[count] = (mtx[j:j + block_size[0], i:i + block_size[1]].ravel(order='F'))
            j+=block_size[0]
            count+=1
        i += block_size[1]
    #print(result)
    return result

def rdnumpy(txtname):
    f = open(txtname)
    line = f.readlines()
    lines = len(line)  # 行数
    for l in line:
        le = l.strip('\n').split(' ')
        columns = len(le) - 1  # 列

    A = zeros((lines, columns), dtype='int64')
    A_row = 0
    for lin in line:
        list = lin.strip('\n').split(' ')
        A[A_row:] = list[0:columns]
        A_row += 1
    return A

def col2im(mtx, image_size, block_size):
    p, q = block_size
    sx = image_size[0]
    sy = image_size[1]
    result = np.zeros(image_size, dtype='int64')
    col = 0
    # 沿着行移动,所以先保持列(i)不动,沿着行(j)走
    i = 0
    count = 0
    while i < sy:
        j = 0
        while j < sx:
            result[j:j + p, i:i + q] = mtx[col].reshape(block_size, order='F')
            col += 1
            j += block_size[0]
        i += block_size[1]
    return result

def showSecret(image_name, str_len):
    img = Image.open(r'./' + image_name)
    img_array = np.asarray(img, dtype='int64')
    a1 = im2col(img_array, (4, 4))
    a1 = a1.astype("int16")
    #print(a1)
    A = rdnumpy(codebook)
    #print(A)
    result = ""
    for i in range(str_len):
        # 从256长度中找对应的index
        index = -1
        j = 0
        for j in range(256):
            if (A[j] == a1[i]).all() :
                index = j
                break
        result += str(index % 2)
    return result
    # print(len(result))
    # print(result)

def yincan(image_name):
    A = rdnumpy(codebook)
    rowA, colA = A.shape
    global img
    img = Image.open(r'./' + image_name)
    img_array = np.asarray(img, dtype='int64')
    a1 = im2col(img_array, (4, 4))
    rowImage, colImage = a1.shape
    result = []
    for i in range(rowImage):
        index = -1
        min = 999999
        for j in range(rowA):
            temp = 0
            for t in range(colA):
                temp = temp + (a1[i][t] - A[j][t])**2
            if temp <= min :
                min = temp
                index = j
        result.append(index)
    return result

#d 0 / 1 表示从哪个开始
def yincan2(image_name, txt_str):
    A = rdnumpy(codebook)
    rowA, colA = A.shape
    global img
    img = Image.open(r'./' + image_name)
    img_array = np.asarray(img, dtype='int64')
    a1 = im2col(img_array, (4, 4))
    rowImage, colImage = a1.shape
    result = []
    for i in range(rowImage):
        index = -1
        min = 999999
        #for j in range(rowA):
        #j = int(bit[i])
        d = 1
        if(i < len(txt_str)):
            j = int(txt_str[i])
            d = 2
        else:
            j = 0
            d = 1
        while j < rowA:
            temp = 0
            for t in range(colA):
                temp = temp + (a1[i][t] - A[j][t])**2
            if temp <= min :
                min = temp
                index = j
            j+=d
        result.append(index)
    return result


def show(array, img_path):
    A = rdnumpy(codebook)
    img_arr = zeros((len(array), 16), dtype='int64')
    for i in range(len(array)):
        img_arr[i] = A[array[i]]
    result = col2im(img_arr, (512, 512), (4, 4))
    cv2.imwrite(img_path, result)
    print("输出完成。")


def text(s):  # 读取文本
    secret = open(s, "r")
    secret1 = ''
    secret2 = ''
    for ch in secret:
        secret1 = secret1 + ch
    global sec_len
    sec_len = len(secret1)
    for i in secret1:
        s = bin(ord(i))[2:]
        while (len(s) < 8):
            s = '0' + s
        secret2 = secret2 + s
    return secret2

# out是二进制流长度
def read(out, textLen):

    s = ''
    txto = "jiemi.txt"
    for i in range(0, len(out), 8):
        if (len(s) < textLen):
            s += chr(int(out[i:i + 8], 2))
    txt_out = open(txto, 'w', encoding='utf-8')
    txt_out.write(s)
    txt_out.close()
    print("提取完成。")

def text_out(imgn, txto, textLen):  # 提取密文
    textLen = textLen*8
    out = showSecret(imgn, textLen)
    s = ''
    for i in range(0, len(out), 8):
        if (len(s) < textLen):
            s += chr(int(out[i:i + 8], 2))
    txt_out = open(txto, 'w', encoding='utf-8')
    txt_out.write(s)
    txt_out.close()
    print("提取完成。")

def psnr(imag1, imag2):
    diff = imag1 - imag2
    # print(np.sum(diff))
    mse = np.mean(np.square(diff))
    np.seterr(divide='ignore', invalid='ignore')  # 消除被除数为0的警告
    psnr = 10 * np.log10(255 * 255 / mse)
    return (psnr)

if __name__ == '__main__':
    print("0. 压缩")
    print("1. 根据vq.txt恢复原图像")
    print("2. 隐藏信息")
    print("3. 恢复信息")
    print("4. 计算psnr")
    print("5. 退出")

    while True:
        choice = input("请输入选择:")
        if choice == "0":
            # 图片压缩
            result = yincan("images/lena_512_gray.bmp")
            np.savetxt('vq.txt', result, fmt='%u')
            print("压缩成功")
        if choice == "1":
            result = np.loadtxt('vq.txt', dtype='int64')
            show(result, "out.bmp")
        if choice == "2":
            txt_str = text("secret.txt")
            result = yincan2("images/lena_512_gray.bmp", txt_str)
            show(result, "has_secret.bmp")
            print("含密图片在has_secret.bmp中")
        if choice == "3":
            text_out("has_secret.bmp", "secret_out.txt", 21)
        if choice == "4":
            imgCover = cv2.imread("images/lena_512_gray.bmp", cv2.IMREAD_UNCHANGED)
            imgout = cv2.imread("out.bmp", cv2.IMREAD_UNCHANGED)
            print('载体图 和 嵌入水印图 对比算出的psnr:{}'.format(psnr(imgCover, imgout)))
        if choice == "5":
            exit()

VQMainCode.m

clear all 
close all
I=double(imread('lena_gray_512.tif'));
[originx,originy]=size(I); %初始图像大小
B=im2col(I,[4,4],'distinct');% 将图像分解为16*(128*128)的矩阵
[m,n]=size(B);              %分解后的大小
cb=importdata ('codebook.txt'); 
[row,col]=size(cb);         %codebook的大小
finaly=0
file1=fopen('vq.txt','wt'); %vq的值存储的文件
sym temp;
sym index;
sym min;
for j =1:16384
    finaly=finaly+1;%% vq
 
    min=9999999;
    index=-1;  
    for k=1:256
         temp=0;
        for p=1:16
           temp=temp+(B(p,j)-cb(k,p))^2;
        end
        if temp<=min
            min=temp;
            index=k;
        end
    end
    vq(1,finaly)=index-1;
end
for f1 =1:1
    for f2 =1:16384
            fprintf(file1,'%d\r\n',vq(f1,f2));%换行
    end
       
end
fclose(file1);

imgPSNR.m

function psnr = imgPSNR(img,imgComp)
% Input
%   	img : 原始帧
%   	imgComp : 预测帧
% Ouput
%   	psnr : 运动补偿后的图像的PSNR

img = double(img);
imgComp = double(imgComp);
[m,n]=size(img);

B=8;                                      %编码一个像素用多少二进制位
MAX=2^B-1;                                %图像有多少灰度级
MSE=sum(sum((img-imgComp).^2))/(m*n);     %均方差
psnr=10*log10(MAX^2/MSE);                 %峰值信噪
disp(psnr);
end

reverseVq.m

clear all 
close all
I=imread('lena_gray_512.tif');%初始图像
cb=importdata ('codebook.txt');
vq=importdata ('vq.txt'); 
for i=1:16384
    for j=1:16
        temp(j,i)=cb(vq(i,1)+1,j);
    end
end
disp('vq后的psnr:')
img=col2im(temp,[4,4],[512,512],'distinct');
imwrite(img,'vq.tif');
img=uint8(img);
imgPSNR(I,img);

%以下输出结果
subplot(1,2,1);imshow(I);title('原图');
subplot(1,2,2);imshow(img);title('vq图片');

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值