EDIT 24.03.2017: I decided to refuse form JPEG and YCBCR. I'm using bmp image and RGB, however the problem is still there.
I'm trying to make Zhao-Koch's steganography algorithm realization, however the extracted message does not correspond to the impemented and I can't seem to grasp, what causes it.
Here's the code:
Implementation:
from PIL import Image
from sklearn.feature_extraction import image
import numpy as np
from scipy.fftpack import dct
from scipy.fftpack import idct
pic = Image.open('lama.bmp') # container, 400x400 bmp picture
pic_size = pic.size #picture size
(r, g, b) = pic.split() #splitting the colour channels
u1 = 4 # coordinates for the DCT coefficients to change. [u1][v1] and [u2][v2]
v1 = 5
u2 = 5
v2 = 4
P = 25 # Threshold value to compare the difference of the coefficients with
cvz = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1] # test message
i = 0 #
acb = np.asarray(b, dtype='int64') # colour channel as array. int64 because absolute difference may go out of the [0,255] boundaries.
patches = image.extract_patches_2d(acb, (8, 8)) # dividing array to 8x8 blocks
for patch in patches: # Applying
dct(patch, overwrite_x = True)
while (i < len(cvz)): # going through message bits
patch = patches[i] # take block
K1 = patch[u1][v1] # first coefficient
K2 = patch[u2][v2] # second coefficient
K = abs(K1) - abs(K2) # difference of absolute values
cur_bit = cvz[i] # take one bit of the message
if (cur_bit == 1) & (K >= -P): # Implementation works the following way: if message bit is 0 than K must be more than P. If it's 1, K must be less than -P. If the requirements are not met, the coefficients change.
i = i +1
while (K >= -P): # changing coefficient
K1 = K1 - 1
print(K1)
K2 = K2 + 1
print(K2)
K = abs(K1) - abs(K2)
patch[u1][v1] = K1 # applying new values
patch[u2][v2] = K2 # applying new values
elif (cur_bit == 0) & (K <= P): # changing coefficient
i = i + 1
while (K <= P):
K1 = K1 + 1
print(K1)
K2 = K2 - 1
print(K2)
K = abs(K1) - abs(K2)
patch[u1][v1] = K1 # applying new values
patch[u2][v2] = K2 # applying new values
else: # requirements are met and there is no need to change coefficients
i = i + 1
for patch in patches: # applying IDCT to blocks
idct(patch, overwrite_x = True)
acb2 = image.reconstruct_from_patches_2d(patches, (400,400)) # reconstructing colour channel
acb2 = acb2.astype(np.uint8) # converting
b_n = Image.fromarray(acb2, 'L') # converting colour channel array to image
changed_image = Image.merge('RGB', (r,g,b_n)) # merging channels to create new image
changed_image.save("stego.bmp") # saving image
Extraction:
from PIL import Image
from sklearn.feature_extraction import image
import numpy as np
from scipy.fftpack import dct
from scipy.fftpack import idct
pic = Image.open('stego.bmp')
(r, g, b) = pic.split()
u1 = 4
v1 = 5
u2 = 5
v2 = 4
length = 13
i = 0
cvz = []
acb = np.asarray(b, dtype='int64')
patches = image.extract_patches_2d(acb, (8, 8))
for patch in patches:
dct(patch,overwrite_x = True)
while (i < length): # extracting message. If absolute of coefficient 1 is more than absolute of coefficient 2 than message bit is 0. Otherwise it's 1
patch = patches[i]
print (patch[u1][v1])
print (patch[u2][v2])
K1 = abs(patch[u1][v1])
K2 = abs(patch[u2][v2])
if (K1 > K2):
cvz.append(0)
i = i + 1
else:
cvz.append(1)
i = i + 1
print(cvz)
However the extracted message is wrong:
Original message:
[1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1]
Extracted message:
[1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1]
I'm guessing that I'm doing something wrong with coefficient changes.
Can someone help me with it, please?
UPD: It seems that changed DCT coefficients are not saved since I can't find them in changed picture if I try to look for them specifically.