【发布时间】:2016-10-18 09:24:28
【问题描述】:
我正在尝试将 Zhao Koch 隐写方法从 matlab 重写为 python,但我一开始就卡住了。
matlab 中的前两个程序:
第 1 步:
A = imread(casepath); # Reading stegonography case image and aquiring it's RGB values. In my case it's a 400x400 PNG image, so it gives a 400x400x3 array.
第 2 步:
D = dct2(A(:,:,3)); # Applying 2D DCT to blue values of the image
Python 代码模拟:
from scipy import misc
from numpy import empty,arange,exp,real,imag,pi
from numpy.fft import rfft,irfft
arr = misc.imread('casepath')# 400x480x3 array (Step 1)
arr[20, 30, 2] # Getting blue pixel value
def dct(y): #Basic DCT build from numpy
N = len(y)
y2 = empty(2*N,float)
y2[:N] = y[:]
y2[N:] = y[::-1]
c = rfft(y2)
phi = exp(-1j*pi*arange(N)/(2*N))
return real(phi*c[:N])
def dct2(y): #2D DCT bulid from numpy and using prvious DCT function
M = y.shape[0]
N = y.shape[1]
a = empty([M,N],float)
b = empty([M,N],float)
for i in range(M):
a[i,:] = dct(y[i,:])
for j in range(N):
b[:,j] = dct(a[:,j])
return b
D = dct2(arr) # step 2 anlogue
但是,当我尝试执行代码时,出现以下错误:
Traceback (most recent call last):
File "path to .py file", line 31, in <module>
D = dct2(arr)
File "path to .py file", line 25, in dct2
a[i,:] = dct(y[i,:])
File "path to .py file", line 10, in dct
y2[:N] = y[:]
ValueError: could not broadcast input array from shape (400,3) into shape (400)
也许有人可以向我解释我做错了什么?
附加信息: 操作系统:Windows 10 专业版 64 位 蟒蛇:2.7.12 scipy:0.18.1 麻木:1.11.2 枕头:3.4.1
【问题讨论】:
-
从 numpy 和 scipy 你可以直接访问
dct()。您是否有兴趣滚动自己的缓慢方法? -
谢谢,不知道刚刚试了一下。看起来效果很好。