因此,我尝试使用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);
}
当执行此命令时,我得到以下信息:
nato@ubuntu:~/webp/webp_test$ ./helloWorld ~/Pictures/mars_sunrise.jpg
processing a 2486x1914 image with 3 channels
encoding image
Segmentation fault
它显示图像很好,但是在编码上存在段错误。我最初的猜测是,这是因为我在尝试写出数据之前就发布了img,但是在尝试编码之前还是之后发布都似乎无关紧要。我还有其他可能会导致此问题的东西吗?是否需要复制图像数据或其他内容?
WebP api文档稀疏。这是自述文件中关于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);
文档没有具体说明“步幅”是什么,但是我假设它与opencv的“步骤”相同。那合理吗?
提前致谢!
最佳答案
首先,如果以后使用它,请不要释放它。其次,您的输出参数指向未初始化的地址。这是将初始化的内存用于输出地址的方法:
uint8_t* output;
datasize = WebPEncodeRGB((uint8_t*)data, width, height, step, qualityFactor, &output);
关于c++ - WebP编码-段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10196671/