2011-03-27 4 views
0

그래서이 문제가 있습니다. 나는 IplImage가 JPEG로 압축하고 그것으로 무언가를하고 싶습니다. libjpeg8b를 사용합니다. "Bogus input colorspace"오류가있는 jpeg_start_compress()의 함수가 될 때 코드가 종료됩니다. 여기 내 코드가 있습니다.OpenCV에서 libjpeg를 사용하여 JPEG로 IplImage 압축

#include "highgui.h" 
#include <stdio.h> 
#include "jpeglib.h" 
#include "cv.h" 
#include <iostream> 
#include <fstream> 
using namespace std; 
using namespace cv; 

#pragma comment(lib, "jpeglib.lib") 


bool ipl2jpeg(IplImage *frame, unsigned char **outbuffer, unsigned long*outlen) 
{ 
    IplImage *img = new IplImage; 
    memcpy(img,frame,frame->nSize); 
    unsigned char *outdata = (uchar *) img->imageData; 
    struct jpeg_compress_struct cinfo = {0}; 
    struct jpeg_error_mgr jerr; 
    JSAMPROW row_ptr[1]; 
    int row_stride; 

    *outbuffer = NULL; 
    *outlen = 0; 

    cinfo.err = jpeg_std_error(&jerr); 
    jpeg_create_compress(&cinfo); 
    jpeg_mem_dest(&cinfo, outbuffer, outlen); 

    cinfo.image_width = frame->width; 
    cinfo.image_height = frame->height; 
    cinfo.input_components = frame->nChannels; 
    cinfo.in_color_space = JCS_RGB; 

    jpeg_set_defaults(&cinfo); 
    jpeg_start_compress(&cinfo, TRUE); 
    system("pause"); 
    row_stride = frame->width * frame->nChannels; 


    while (cinfo.next_scanline < cinfo.image_height) 
    { 
     row_ptr[0] = &outdata[cinfo.next_scanline * row_stride]; 
     jpeg_write_scanlines(&cinfo, row_ptr, 1); 
    } 

    jpeg_finish_compress(&cinfo); 
    jpeg_destroy_compress(&cinfo); 

    return true; 

} 


int main() 
{ 
    ofstream fout("text.txt"); 
    unsigned char **buf; 
    buf = new unsigned char* [120]; 
    for(int i=0;i<500;i++) 
    { 
     buf[i] = new unsigned char[120]; 
    } 

    for(int i=0;i< 120;i++) 
    { 
     for(int j=0;j<120;j++) 
     { 
      buf[i][j] = 0; 
     } 
    } 

    unsigned long *len = new unsigned long; 
    *len = 120*120; 
    Ptr<IplImage> img = cvLoadImage("test.jpg",CV_LOAD_IMAGE_GRAYSCALE); 
    ipl2jpeg(img,buf,len); 

    for(int i=0;i< 120;i++) 
    { 
     for(int j=0;j<120;j++) 
     { 
      fout<<buf[i][j]<<endl; 
     } 
    } 

    return 0; 
} 

답변

1

opencv의 기본 JPEG 지원을 사용하지 않는 이유가 있습니까?

cvSaveImage(frame, "frame.jpeg"); 

설명서는 here입니다.

편집

당신이 libjpeg을 사용하여 주장하는 경우,이 post를 보라.

+0

디스크 이외의 메모리에서 압축하려고합니다. – user676932

+0

C++ 인터페이스에 액세스 할 수 있습니까? 'imdecode'와'imencode' 함수가 있습니다. – misha

+0

OpenCV의 자연 JPEG 지원을 사용하지 않는 이유는 OpenCV를 통한 옵션이 매우 제한되어 있기 때문입니다. – TimZaman

1

이전에 libjpeg를 사용한 적이 없지만 색상 공간이 섞여있는 것처럼 보입니다. 이미지를 그레이 스케일 (CV_LOAD_IMAGE_GRAYSCALE)로로드하지만 libjpeg에 RGB 이미지 (JCS_RGB)라고 알려줍니다. 당신은

cinfo.in_color_space = JCS_GRAYSCALE; 

에 선

cinfo.in_color_space = JCS_RGB; 

을 변경 시도?

관련 문제