所以我正在尝试使用webp API来编码图像.现在我将使用openCV来打开和操作图像,然后我想将它们保存为webp.这是我正在使用的来源:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <webp/encode.h>
int main(int argc, char *argv[])
{
IplImage* img = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;
if (argc<2) {
printf("Usage:main <image-file-name>\n\7");
exit(0);
}
// load an image
img=cvLoadImage(argv[1]);
if(!img){
printf("could not load image file: %s\n",argv[1]);
exit(0);
}
// get the image data
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
printf("processing a %dx%d image with %d channels \n", width, height, channels);
// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin",100,100);
// invert the image
for (i=0;i<height;i++) {
for (j=0;j<width;j++) {
for (k=0;k<channels;k++) {
data[i*step+j*channels+k] = 255-data[i*step+j*channels+k];
}
}
}
// show the image
cvShowImage("mainWin", img);
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img);
float qualityFactor = .9;
uint8_t** output;
FILE *opFile;
size_t datasize;
printf("encoding image\n");
datasize = WebPEncodeRGB((uint8_t*)data,width,height,step,qualityFactor,output);
printf("writing file out\n");
opFile=fopen("output.webp","w");
fwrite(output,1,(int)datasize,opFile);
}
Run Code Online (Sandbox Code Playgroud)
当我执行此操作时,我得到了这个:
nato@ubuntu:~/webp/webp_test$ ./helloWorld ~/Pictures/mars_sunrise.jpg
processing a 2486x1914 image with 3 channels
encoding image
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
它显示图像很好,但在编码上有段错误.我最初的猜测是因为我在尝试写出数据之前发布了img,但是在尝试编码之前或之后我是否发布它并不重要.还有其他我想念的东西可能导致这个问题吗?我是否必须复制图像数据或其他内容?
WebP api文档是......稀疏的.这是README关于WebPEncodeRGB的内容:
The main encoding functions are available in the header src/webp/encode.h
The ready-to-use ones are:
size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height,
int stride, float quality_factor, uint8_t** output);
Run Code Online (Sandbox Code Playgroud)
文档特别没有说'stride'是什么,但我假设它与opencv中的'step'相同.这合理吗?
提前致谢!
首先,如果以后使用它,请不要释放图像.其次,输出参数指向非初始化地址.这是如何使用初始化内存作为输出地址:
uint8_t* output;
datasize = WebPEncodeRGB((uint8_t*)data, width, height, step, qualityFactor, &output);
Run Code Online (Sandbox Code Playgroud)