我试图完成DCT隐藏项目和一些问题的发生。有人能帮我吗?我所做的:
1)将图像分割成8x8像素
public BufferedImage[] getBlocksOfImage(BufferedImage image){
int width = image.getWidth();
int height = image.getHeight();
int arrayIndex = 0;
//TODO specify dynamic length
//TODO for 512x512 --> 4096 (8x8 block)
BufferedImage[] blocksOfImage = new BufferedImage[4096];
for (int i = 0; i<width; i=i+8) {
for (int j = 0; j<height; j=j+8) {
blocksOfImage[arrayIndex] = image.getSubimage(i,j,8,8);
arrayIndex++;
}
}
return blocksOfImage;
}2)使用以下方法计算每个块的矩阵:
//get matrix of pixels (used in DCT]
public int[][] getMatrixPixels(BufferedImage image){
int[][] matrixPixels = new int[image.getWidth()][image.getHeight()];
for (int i = 0; i < matrixPixels[0].length; i++) {
for (int j = 0; j < matrixPixels.length; j++) {
matrixPixels[i][j] = image.getRGB(i, j);
}
}
return matrixPixels;
}3)从块中,我得到了使用以下方法计算的DCT矩阵:
public double[][] getDTCTransformMatrix(int[][] imageMatrix){
double[][] dctTransformMatrix = new double[m][n];
double ci, cj, tmpDCTValue, tmpSum;
tmpSum = 0;
//TODO simplify 4x for --> BAD
//TODO consulting
for(int i = 0; i<m; i++) {
for(int j = 0; j<n; j++) {
if (i == 0){
ci = 1 / Math.sqrt(m);
} else {
ci = Math.sqrt(2) / Math.sqrt(m);
}
if (j == 0){
cj = 1 / Math.sqrt(n);
} else {
cj = Math.sqrt(2) / Math.sqrt(m);
}
for (int i_image = 0; i_image < m; i_image++) {
for (int j_image = 0; j_image < n; j_image++) {
tmpDCTValue = imageMatrix[i_image][j_image] *
Math.cos((2 * i_image + 1) * i * Math.PI / (2 * m)) *
Math.cos((2 * j_image + 1) * j * Math.PI / (2 * n));
tmpSum += tmpDCTValue;
}
}
dctTransformMatrix[i][j] = ci * cj * tmpSum;
}
}
return dctTransformMatrix;
}以下是我的问题:
1)应该将什么传递到getDTCTransformMatrix()函数中?现在我正在传递像素的整数值,我认为这是错误的。我看到了一些例子,其中的人传递值从0-255,所以我应该把图像转换为灰度吗?或者我应该为每种颜色(R,G,B)做这件事?
2)执行getDTCTransformMatrix()后,得到双矩阵。如何编辑双值LSB?这是正确的做法吗?
3)当我将一个双值的LSB改变之后,我下一步应该做什么?如何确保信息存储在图像中。
谢谢大家的回答:)
--编辑--
我已经编辑了代码,现在我分别传递每个通道(R、G、B),并添加了以下函数:
public int[][] getQuantiseCoefficients(double[][] DTCTransformMatrix) {
int[][] quantiseResult = new int[DTCTransformMatrix.length][DTCTransformMatrix[1].length];
int[][] quantiseMatrix = {{16, 11, 10, 16, 24, 40, 51, 61},
{12, 12, 14, 19, 26, 58, 60, 55},
{14, 13, 16, 24, 40, 57, 69, 56},
{14, 17, 22, 29, 51, 87, 80, 62},
{18, 22, 37, 56, 68, 109, 103, 77},
{24, 35, 55, 64, 81, 104, 113, 92},
{49, 64, 78, 87, 103, 121, 120, 101},
{72, 92, 95, 98, 112, 100, 103, 99}};
//TODO delete static 8
for (int i = 0; i<8; i++) {
for (int j = 0; j<8; j++) {
//Bij = round(Gij/Qij)
quantiseResult[i][j] = (int)Math.round(DTCTransformMatrix[i][j]/quantiseMatrix[i][j]);
}
}
return quantiseResult;
}以及添加以下信息:
public ArrayList<int[][]> addMessageFirst(ArrayList<int[][]> quantiseMatrixRGB, String message) {
Utils utils = new Utils();
int bitTextShift = 7;
int byteTextShift = 0;
byte[] textByteArray = text.getBytesFromText(message);
for (int i = 0; i < textByteArray.length*8; i++) {
byte[] firstValueByte = utils.integerToByte(quantiseMatrixRGB.get(i)[0][0]);
firstValueByte[3] = (byte) ((firstValueByte[3] & 0xFE) | ((int) textByteArray[byteTextShift] >>> bitTextShift) & 1);
if (bitTextShift <= 0) {
bitTextShift = 7;
byteTextShift++;
} else {
bitTextShift--;
}
}
return quantiseMatrixRGB;
}一切看起来都很好,但是现在我正在努力把一个图像重新组合起来。我所做的代码如下,但我认为这是相当困难的。难道没有更简单的方法吗?
例如,通过使用一些已经创建的JPEG编码器?
public int[][] revertGetQuantiseCoefficients (int[][] quantiseMatrixRGB) {
int[][] quantiseMatrix = {{16, 11, 10, 16, 24, 40, 51, 61},
{12, 12, 14, 19, 26, 58, 60, 55},
{14, 13, 16, 24, 40, 57, 69, 56},
{14, 17, 22, 29, 51, 87, 80, 62},
{18, 22, 37, 56, 68, 109, 103, 77},
{24, 35, 55, 64, 81, 104, 113, 92},
{49, 64, 78, 87, 103, 121, 120, 101},
{72, 92, 95, 98, 112, 100, 103, 99}};
for (int j = 0; j<8; j++) {
for (int k = 0; k<8; k++) {
quantiseMatrixRGB[j][k]=quantiseMatrixRGB[j][k]*quantiseMatrix[j][k];
}
}
return quantiseMatrixRGB;
}
public int[][] revertGetDTCTransformMatrix(int[][] quantiseMatrixRGB) {
double[][] dctTransformMatrix = new double[m][n];
double ck, cl, tmpDCTValue, tmpSum;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
tmpSum = 0;
for (int i_image = 0; i_image < m; i_image++) {
for (int j_image = 0; j_image < n; j_image++) {
if (i_image == 0) {
ck = 1 / Math.sqrt(m);
} else {
ck = Math.sqrt(2) / Math.sqrt(m);
}
if (j_image == 0) {
cl = 1 / Math.sqrt(n);
} else {
cl = Math.sqrt(2) / Math.sqrt(n);
}
tmpDCTValue = quantiseMatrixRGB[i_image][j_image] *
Math.cos((2 * i_image + 1) * i * pi / (2 * m)) *
Math.cos((2 * j_image + 1) * j * pi / (2 * n));
tmpSum = tmpSum + ck*cl*tmpDCTValue;
}
}
dctTransformMatrix[i][j] = tmpSum;
}
}
return quantiseMatrixRGB;
}
public int[][] getMergeRGB (int[][] r, int[][] g, int[][] b) {
int[][] mergeRGB = new int[r.length][r[1].length];
for (int i = 0; i<8; i++) {
for(int j = 0; j<8; j++) {
mergeRGB[i][j] = r[i][j];
mergeRGB[i][j] = (mergeRGB[i][j]<<8) + g[i][j];
mergeRGB[i][j] = (mergeRGB[i][j]<<8) + b[i][j];
}
}
return mergeRGB;
}
public BufferedImage getPixelBlock (int[][] mergeRGB) {
BufferedImage bi = new BufferedImage( 8, 8, BufferedImage.TYPE_INT_RGB );
final int[] a = ( (DataBufferInt) bi.getRaster().getDataBuffer() ).getData();
System.arraycopy(mergeRGB, 0, a, 0, mergeRGB.length);
return bi;
}
public BufferedImage joinImages (BufferedImage subImage) {
return null;
}
}我很难把BufferedImage subImages重新组合起来。有什么想法吗?
非常感谢您的时间和负担。
发布于 2019-11-17 15:33:29
看来你是在做JPEG密写。编写jpeg编码器的标准非常复杂,因此更容易借用已经编写的jpeg编码器,并对其进行任何小的修改,从而将隐藏算法注入其中。
我已经已应答了一个类似的问题,我简要地总结了算法的要点,并在java中展示了一个例子。
了解这一过程的一个良好开端是在JPEG编码上的维基百科上。这应该能回答你提出的所有问题,但我也会在这里讨论它们。
应该将什么传递到getDTCTransformMatrix()函数中?现在我正在传递像素的整数值,我认为这是错误的。我看到了一些例子,其中的人传递值从0-255,所以我应该把图像转换为灰度吗?或者我应该为每种颜色(R,G,B)做这件事?
是的,您可以单独处理每个彩色平面的传递整数值。它既可以在RGB中也可以在YCrCb中。它也可以是从0到255,或者集中在0附近,即-127,128。之所以选择YCrCb,是因为一些频道可以被压缩得更多,而不会因为我们的眼睛的工作方式而造成任何明显的质量损失。将数字的范围移到0附近,就意味着得到的DCT系数会有更小的值,存储的比特也会更少。
在执行getDTCTransformMatrix()之后,我得到了双矩阵。如何编辑双值LSB?这是正确的做法吗?
这是JPEG编码的重点。你应该用一个特定的量化矩阵来量化系数(把它们转换成整数)。虽然有一个默认的程序,但各种程序都选择使用自定义程序。其思想是低频系数不会受到很大影响,而大多数高频系数可能变为0,这有助于最终文件大小变小。这是一个有损的过程,您应该在量化系数之后嵌入您的信息,因为其余的步骤都是无损的。
在我要改变一个双值的LSB之后,我下一步应该做什么?如何确保信息存储在图像中。
你把系数排列成一个字形,在一个一维,所以低频系数是第一位。然后,使用运行长度和Huffman编码的组合将该信息存储在文件中。该文件的二进制数据与原始像素(显然)或DCT系数的值没有任何相似之处。这是系数的压缩数据。
https://stackoverflow.com/questions/58899688
复制相似问题