C++ WebP编码-分段错误
所以我尝试使用WebPAPI对图像进行编码。现在我将使用openCV打开和操作图像,然后我想将它们保存为webp。以下是我使用的来源:C++ WebP编码-分段错误,c++,opencv,webp,C++,Opencv,Webp,所以我尝试使用WebPAPI对图像进行编码。现在我将使用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 *a
#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);
}
它可以很好地显示图像,但在编码上有缺陷。我最初的猜测是,这是因为我在试图写出数据之前释放了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中的“步幅”相同。这合理吗
提前谢谢 在尝试使用指向图像数据的指针进行编码之前,请使用
cvReleaseImage
释放图像。这个释放函数可能会释放图像缓冲区,并且您的数据指针现在不再指向有效内存
这可能是segfault的原因。在尝试使用指向图像数据的指针进行编码之前,请使用cvReleaseImage
释放图像。这个释放函数可能会释放图像缓冲区,并且您的数据指针现在不再指向有效内存
这可能是您的SEGFULT的原因。因此问题似乎在这里:
// load an image
img=cvLoadImage(argv[1]);
函数cvLoadImage接受一个额外的参数
cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)
当我换成
img=cvLoadImage(argv[1],1);
断层消失了 看来问题出在这里:
// load an image
img=cvLoadImage(argv[1]);
函数cvLoadImage接受一个额外的参数
cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)
当我换成
img=cvLoadImage(argv[1],1);
断层消失了 首先,如果以后使用,请不要释放图像。其次,输出参数指向未初始化的地址。以下是如何将初始化内存用于输出地址:
uint8_t* output;
datasize = WebPEncodeRGB((uint8_t*)data, width, height, step, qualityFactor, &output);
首先,如果以后使用,请不要释放图像。其次,输出参数指向未初始化的地址。以下是如何将初始化内存用于输出地址:
uint8_t* output;
datasize = WebPEncodeRGB((uint8_t*)data, width, height, step, qualityFactor, &output);
正如我在文章中所指出的,“我最初的猜测是,这是因为我在试图写出数据之前发布了img,但我在尝试编码之前还是之后发布它似乎并不重要。”我尝试将“发布”移动到编码之后,但我得到了完全相同的结果。正如我在文章中所指出的,“我最初的猜测是,这是因为我在试图写出数据之前发布了img,但我在尝试编码之前还是之后发布它似乎并不重要。”我尝试将“发布”移动到编码之后,但我得到了完全相同的结果。