我使用opencv读取图像并再次保存,但是当我稍后读取时数据不一样,我的意思是在我读取图像后保存它,然后复制保存的图像并读取该图像中的数据,但数据将与以前不同,我编写小代码来执行以下操作 1- 读取图像 2- 保存图像 3- 将图像数据保存到文本文件中 4- 从步骤 2 中读取保存的图像 5- 比较图像的值文本文件的值并将它们一起打印
我的代码是
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <time.h>
#include <stdint.h>
#include "highgui.h"
IplImage *PlainImage=0,*CipherImage=0,*DecPlainImage=0;
void func_printimage()
{
// create a window
cvNamedWindow("Plain Image",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Plain Image", 800, 600);
// show the image
cvShowImage("Plain Image", PlainImage );
// wait for a key
cvNamedWindow("Cipher Image",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Cipher Image", 800, 600);
// show the image
cvShowImage("Cipher Image", CipherImage );
cvSaveImage("CipherImage.jpg",CipherImage,0);
cvWaitKey(0);
}
int main()
{
//i j and k used as counters
int i,j,step,dep,k,ch,L,C,P,sum=0;
uchar *data_byte;
//Define CPU time parameters for each Layer
PlainImage=cvLoadImage("PlainImage.jpg",3);
CipherImage=cvLoadImage("PlainImage.jpg",3);
L = PlainImage->height;
C = PlainImage->width;
P = PlainImage->nChannels;
step = PlainImage->widthStep;
data_byte=CipherImage->imageData;
printf("Image Information are:\nL=%d\n",L);
printf("C=%d\n",C);
printf("P=%d\n",P);
system("pause");
FILE *f1;
f1 = fopen ("cipher1.txt", "wt");
fprintf(f1,"%d\t%d\t%d\t%d\t",L,C,P,CipherImage->depth);
for(k=0;k<L*C*P;k++)
{
fprintf(f1,"%d\t",data_byte[k]);
}
fclose (f1);
func_printimage();
for(k=0;k<L*C*P;k++)
{
data_byte[k]=0;
}
f1 = fopen ("cipher1.txt", "rt");
fscanf (f1,"%d", &L);
fscanf (f1,"%d", &C);
fscanf (f1,"%d", &P);
fscanf (f1,"%d", &dep);
CipherImage=cvLoadImage("CipherImage.jpg",3);
data_byte=CipherImage->imageData;
printf("Image Information are:\nL=%d\n",L);
printf("C=%d\n",C);
printf("P=%d\n",P);
system("pause");
for(k=0;k<L*C*P;k++)
{
fscanf (f1,"%d", &i);
sum+=abs(i-data_byte[k]);
printf("i=%d data=%d\n",i,data_byte[k]);
}
printf("difference=%d\n",sum);
fclose (f1);
system("pause");
return 0;
}
//主程序结束