最近在写opencv压缩图片的逻辑的时候,发现一张图片用cvResize之后的像素值,和保存后的图片像素值不一样。
后来发现,原来是用cvSaveImage的时候,有第三个参数,会导致保存的图片像素值有误差,参考:http://www.cnblogs.com/wolfplan/p/4058851.html
这是我的部分代码:
IplImage *image=0,*imageresize=0;
//imageresize = cvLoadImage("save.jpg",1);
image=cvLoadImage("1.jpg",1);
if(!image) printf("can't load an image/n");
imageresize=cvCreateImage(cvSize(224,224),IPL_DEPTH_8U,3);
cvResize(image,imageresize,CV_INTER_LINEAR);
int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 100; //这里是重点
p[2] = 0;
cvSaveImage("save2.jpg",imageresize, p);
//std::ofstream fileout("imgtest.txt");
printf("imageresize:%d\n", imageresize->imageSize);
std::cout<<imageresize->width<<","<<imageresize->height<<","<<imageresize->widthStep<<std::endl;
//int fd = open("img.txt", O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IXGRP|S_IROTH|S_IXOTH);
size_t addr = 0x00;
//for(int y=0;y<imageresize->height;y++)
for (int y=0; y<1; y++)
{
unsigned char* p=(unsigned char*)(imageresize->imageData+y*imageresize->widthStep);
for (int x=0; x<imageresize->width*imageresize->nChannels; x+=3)
{
//get BGR:
//pwrite_bgr_16bits(p[x+2], p[x+1], p[x], fd, addr);
printf("(%d,%d,%d)", p[x+2], p[x+1], p[x]);
addr += 64;
}
//fileout<<p[x];
//fileout<<'\n';
}