2009-04-29 9 views
15

ओपनसीवी cvLoadImage और cvSaveImage में दो फ़ंक्शन फ़ाइल पथ को तर्क के रूप में स्वीकार करते हैं।मेमोरी बफर या फ़ाइल पॉइंटर्स में उपयोग करने के लिए OpenCV

उदाहरण के लिए, जब छवि को सहेजते हैं तो यह cvSaveImage ("/ tmp/output.jpg", dstIpl) और यह डिस्क पर लिखता है।

क्या यह बफर पहले से ही स्मृति में खिलाने का कोई तरीका है? तो डिस्क लिखने के बजाय, आउटपुट छवि स्मृति में होगी।

मैं इसे cvSaveImage और cvLoadImage (स्मृति बफर को पढ़ने और लिखने) दोनों के लिए भी जानना चाहूंगा। धन्यवाद!


मेरा लक्ष्य मेमोरी में फ़ाइल के एन्कोडेड (जेपीईजी) संस्करण को स्टोर करना है। CvLoadImage पर भी जाता है, मैं एक jpeg लोड करना चाहता हूं जो IplImage प्रारूप में स्मृति में है।

+1

आप स्मृति के लिए छवि लिखना चाहते हैं साथ प्रयोग के लिए एक 24bit बिटमैप धर्मान्तरित? लेकिन dstIpl पहले से ही स्मृति में है, आप वास्तव में क्या करने की उम्मीद कर रहे हैं? आप dstIpl-> imageData या -> डेटा या कुछ के साथ छवि डेटा तक पहुंच सकते हैं। – mpen

+0

इसी तरह, आप iplImage के डेटा बफर को उस छवि को लोड करने के लिए उपयोग कर सकते हैं जो पहले से ही स्मृति में है ... बस बीजीआर प्रारूप में होना चाहिए। – mpen

+0

मुझे लगता है कि मूल पोस्टर छवि को एन्कोड करना चाहता है लेकिन डिस्क को पढ़ने/लिखने के लिए सहेजना चाहता है। – M456

उत्तर

14

पुस्तकालय की SVN संस्करण में गैर-दस्तावेजी कार्यों के एक जोड़े हैं:

CV_IMPL CvMat* cvEncodeImage(const char* ext, 
           const CvArr* arr, const int* _params) 

CV_IMPL IplImage* cvDecodeImage(const CvMat* _buf, int iscolor) 

संदेश में नवीनतम जाँच कहा गया है कि वे मूल एन्कोडिंग के लिए कर रहे हैं/BMP, png, पीपीएम और टिफ (एन्कोडिंग के लिए ही डिकोडिंग)।

वैकल्पिक रूप से आप एक मानक छवि एन्कोडिंग लाइब्रेरी (उदा। Libjpeg) का उपयोग कर सकते हैं और एन्कोडिंग लाइब्रेरी की इनपुट संरचना से मेल खाने के लिए IplImage में डेटा का उपयोग कर सकते हैं।

+7

कृपया अनियंत्रित कार्यों पर भरोसा न करें, आप अपने लिए एक रखरखाव दुःस्वप्न तैयार करेंगे। कारण यह दस्तावेज नहीं है क्योंकि डिजाइनर ** समय के साथ स्थिर रहने की उम्मीद नहीं करते **। वे अगले संस्करण के साथ गायब हो सकता है! –

+3

मुझे लगता है कि चेतावनी किसी भी विकास संस्करण के साथ निहित है। साथ ही, ओपनसीवी के साथ प्रलेखन हमेशा एक मुद्दा रहा है, स्थिर कार्यों में से कई अभी भी अनियंत्रित हैं। – M456

0

यह एक अप्रत्यक्ष जवाब है ...

अतीत में, मैं सीधे libpng और libjpeg का उपयोग किया है सीधे यह करने के लिए। उनके पास निम्न स्तर का पर्याप्त एपीआई है जिसे आप पढ़ने और लिखने के लिए फ़ाइल बफर के बजाय मेमोरी बफर का उपयोग कर सकते हैं।

+0

यदि आप डाउनवोट करते हैं, तो कृपया एक टिप्पणी छोड़ दें कि यह उत्तर गलत क्यों है। इस तरह इसे बेहतर किया जा सकता है। –

1

मुझे लगता है कि आप लिनक्स में काम कर रहे हैं। libjpeg.doc से:

एक JPEG संपीड़न आपरेशन के किसी न किसी रूपरेखा है:
का आवंटन और एक JPEG संपीड़न वस्तु को प्रारंभ
संकुचित डेटा (जैसे, एक फ़ाइल)
के लिए गंतव्य निर्दिष्ट करें संपीड़न के लिए पैरामीटर सेट, छवि का आकार & colorspace

jpeg_start_compress (...) सहित;
जबकि (स्कैन लाइनों को लिखा जाना बाकी है)
jpeg_write_scanlines (...);

jpeg_finish_compress (...);
रिलीज JPEG संपीड़न वस्तु

कि आप क्या करना एक कस्टम "डेटा गंतव्य (या स्रोत) प्रबंधक" जो jpeglib.h में परिभाषित किया गया है प्रदान कर रहा है चाहता हूँ करने के लिए असली चाल:

struct jpeg_destination_mgr { 
    JOCTET * next_output_byte; /* => next byte to write in buffer */ 
    size_t free_in_buffer;  /* # of byte spaces remaining in buffer */ 

    JMETHOD(void, init_destination, (j_compress_ptr cinfo)); 
    JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo)); 
    JMETHOD(void, term_destination, (j_compress_ptr cinfo)); 
}; 

असल में इसे सेट अप करें ताकि आपका स्रोत और/या गंतव्य मेमोरी बफर हैं जो आप चाहते हैं, और आपको जाने के लिए अच्छा होना चाहिए।

एक तरफ के रूप में, यह पोस्ट बहुत बेहतर हो सकता है लेकिन libjpeg62 दस्तावेज काफी स्पष्ट रूप से शानदार है। बस apt-libjpeg62-dev प्राप्त करें और libjpeg.doc पढ़ें और example.c देखें। यदि आप समस्याओं में भाग लेते हैं और काम करने के लिए कुछ नहीं प्राप्त कर सकते हैं, तो बस दोबारा पोस्ट करें और मुझे यकीन है कि कोई मदद करने में सक्षम होगा।

0

आपको मेमोरी बफर से फ़ाइलों को लोड करने की आवश्यकता है एक अलग src manager (libjpeg) है। मैंने उबंटू 8.10 में निम्नलिखित कोड का परीक्षण किया है।

/******************************** First define mem buffer function bodies **************/ 
<pre> 
/* 
* memsrc.c 
* 
* Copyright (C) 1994-1996, Thomas G. Lane. 
* This file is part of the Independent JPEG Group's software. 
* For conditions of distribution and use, see the accompanying README file. 
* 
* This file contains decompression data source routines for the case of 
* reading JPEG data from a memory buffer that is preloaded with the entire 
* JPEG file. This would not seem especially useful at first sight, but 
* a number of people have asked for it. 
* This is really just a stripped-down version of jdatasrc.c. Comparison 
* of this code with jdatasrc.c may be helpful in seeing how to make 
* custom source managers for other purposes. 
*/ 

/* this is not a core library module, so it doesn't define JPEG_INTERNALS */ 
//include "jinclude.h" 
include "jpeglib.h" 
include "jerror.h" 


/* Expanded data source object for memory input */ 

typedef struct { 
    struct jpeg_source_mgr pub; /* public fields */ 

    JOCTET eoi_buffer[2];  /* a place to put a dummy EOI */ 
} my_source_mgr; 

typedef my_source_mgr * my_src_ptr; 


/* 
* Initialize source --- called by jpeg_read_header 
* before any data is actually read. 
*/ 

METHODDEF(void) 
init_source (j_decompress_ptr cinfo) 
{ 
    /* No work, since jpeg_memory_src set up the buffer pointer and count. 
    * Indeed, if we want to read multiple JPEG images from one buffer, 
    * this *must* not do anything to the pointer. 
    */ 
} 


/* 
* Fill the input buffer --- called whenever buffer is emptied. 
* 
* In this application, this routine should never be called; if it is called, 
* the decompressor has overrun the end of the input buffer, implying we 
* supplied an incomplete or corrupt JPEG datastream. A simple error exit 
* might be the most appropriate response. 
* 
* But what we choose to do in this code is to supply dummy EOI markers 
* in order to force the decompressor to finish processing and supply 
* some sort of output image, no matter how corrupted. 
*/ 

METHODDEF(boolean) 
fill_input_buffer (j_decompress_ptr cinfo) 
{ 
    my_src_ptr src = (my_src_ptr) cinfo->src; 

    WARNMS(cinfo, JWRN_JPEG_EOF); 

    /* Create a fake EOI marker */ 
    src->eoi_buffer[0] = (JOCTET) 0xFF; 
    src->eoi_buffer[1] = (JOCTET) JPEG_EOI; 
    src->pub.next_input_byte = src->eoi_buffer; 
    src->pub.bytes_in_buffer = 2; 

    return TRUE; 
} 


/* 
* Skip data --- used to skip over a potentially large amount of 
* uninteresting data (such as an APPn marker). 
* 
* If we overrun the end of the buffer, we let fill_input_buffer deal with 
* it. An extremely large skip could cause some time-wasting here, but 
* it really isn't supposed to happen ... and the decompressor will never 
* skip more than 64K anyway. 
*/ 

METHODDEF(void) 
skip_input_data (j_decompress_ptr cinfo, long num_bytes) 
{ 
    my_src_ptr src = (my_src_ptr) cinfo->src; 

    if (num_bytes > 0) { 
    while (num_bytes > (long) src->pub.bytes_in_buffer) { 
     num_bytes -= (long) src->pub.bytes_in_buffer; 
     (void) fill_input_buffer(cinfo); 
     /* note we assume that fill_input_buffer will never return FALSE, 
     * so suspension need not be handled. 
     */ 
    } 
    src->pub.next_input_byte += (size_t) num_bytes; 
    src->pub.bytes_in_buffer -= (size_t) num_bytes; 
    } 
} 


/* 
* An additional method that can be provided by data source modules is the 
* resync_to_restart method for error recovery in the presence of RST markers. 
* For the moment, this source module just uses the default resync method 
* provided by the JPEG library. That method assumes that no backtracking 
* is possible. 
*/ 


/* 
* Terminate source --- called by jpeg_finish_decompress 
* after all data has been read. Often a no-op. 
* 
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 
* application must deal with any cleanup that should happen even 
* for error exit. 
*/ 

METHODDEF(void) 
term_source (j_decompress_ptr cinfo) 
{ 
    /* no work necessary here */ 
} 


/* 
* Prepare for input from a memory buffer. 
*/ 

GLOBAL(void) 
jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize) 
{ 
    my_src_ptr src; 

    /* The source object is made permanent so that a series of JPEG images 
    * can be read from a single buffer by calling jpeg_memory_src 
    * only before the first one. 
    * This makes it unsafe to use this manager and a different source 
    * manager serially with the same JPEG object. Caveat programmer. 
    */ 
    if (cinfo->src == NULL) { /* first time for this JPEG object? */ 
    cinfo->src = (struct jpeg_source_mgr *) 
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 
        SIZEOF(my_source_mgr)); 
    } 

    src = (my_src_ptr) cinfo->src; 
    src->pub.init_source = init_source; 
    src->pub.fill_input_buffer = fill_input_buffer; 
    src->pub.skip_input_data = skip_input_data; 
    src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ 
    src->pub.term_source = term_source; 

    src->pub.next_input_byte = buffer; 
    src->pub.bytes_in_buffer = bufsize; 
} 

फिर उपयोग बहुत आसान है। आपको sizeof() को आकार() के साथ प्रतिस्थापित करने की आवश्यकता हो सकती है। एक मानक डिकंप्रेशन उदाहरण पाएं। "Jpeg_mdory_src" के साथ बस "jpeg_stdio_src" को प्रतिस्थापित करें। उम्मीद है की वो मदद करदे!

15

यह मेरे लिए काम किया

// decode jpg (or other image from a pointer) 
// imageBuf contains the jpg image 
    cv::Mat imgbuf = cv::Mat(480, 640, CV_8U, imageBuf); 
    cv::Mat imgMat = cv::imdecode(imgbuf, CV_LOAD_IMAGE_COLOR); 
// imgMat is the decoded image 

// encode image into jpg 
    cv::vector<uchar> buf; 
    cv::imencode(".jpg", imgMat, buf, std::vector<int>()); 
// encoded image is now in buf (a vector) 
    imageBuf = (unsigned char *) realloc(imageBuf, buf.size()); 
    memcpy(imageBuf, &buf[0], buf.size()); 
// size of imageBuf is buf.size(); 

मैं सी के बजाय ++ एक सी संस्करण के बारे में पूछा गया था:

#include <opencv/cv.h> 
#include <opencv/highgui.h> 

int 
main(int argc, char **argv) 
{ 
    char *cvwin = "camimg"; 

    cvNamedWindow(cvwin, CV_WINDOW_AUTOSIZE); 

    // setup code, initialization, etc ... 
    [ ... ] 

    while (1) {  
     // getImage was my routine for getting a jpeg from a camera 
     char *img = getImage(fp); 
     CvMat mat; 

    // substitute 640/480 with your image width, height 
     cvInitMatHeader(&mat, 640, 480, CV_8UC3, img, 0); 
     IplImage *cvImg = cvDecodeImage(&mat, CV_LOAD_IMAGE_COLOR); 
     cvShowImage(cvwin, cvImg); 
     cvReleaseImage(&cvImg); 
     if (27 == cvWaitKey(1))   // exit when user hits 'ESC' key 
     break; 
    } 

    cvDestroyWindow(cvwin); 
} 
+0

क्या यह इरादा नहीं है कि imdecode छवि को डीकोड करना चाहिए, तो imageBuf को cv :: mat में क्यों रखा जाना चाहिए? मुझे समस्याएं आ रही हैं जहां मेरे पास एक हस्ताक्षरित चार * है जिसमें कच्चे डेटा को तार पर लोड किया गया है (इसकी एक टिफ इमेज), लेकिन कच्चे डेटा पर इमेकोडोड करने के बाद मैट में mat.data == 0 है और कोई त्रुटि नहीं दी गई है। –

+0

@pksorensen imageBuf को मैट में डालने की आवश्यकता है ताकि इसे imdecode() के पैरामीटर के रूप में स्वीकार किया जा सके, जब तक कि C++ जादू न हो जो इसे InputArray में बदल सकता है जो imdecode() चाहता है। आपकी टीआईएफएफ छवि को डीकोड करने के कारण नहीं होंगे (1) मैट आईएमजीडीकोड पंक्तियों में पारित किया गया * कोल्स 1 से कम था, (2) टीआईएफएफ समर्थन आपकी ओपनसीवी लाइब्रेरी में संकलित नहीं किया गया था या (3) कि टीआईएफएफ डिकोडर आपकी पहचान नहीं करता था एक वैध टीआईएफएफ छवि के रूप में छवि बफर। – codeDr

0

यहाँ डेल्फी में एक उदाहरण है। यह OpenCV

function BmpToPIplImageEx(Bmp: TBitmap): pIplImage; 
Var 
    i: Integer; 
    offset: LongInt; 
    dataByte: PByteArray; 
Begin 
    Assert(Bmp.PixelFormat = pf24bit, 'PixelFormat must be 24bit'); 
    Result := cvCreateImageHeader(cvSize(Bmp.Width, Bmp.Height), IPL_DEPTH_8U, 3); 
    cvCreateData(Result); 
    for i := 0 to Bmp.height - 1 do 
    Begin   
    offset := longint(Result.imageData) + Result.WidthStep * i; 
    dataByte := PByteArray(offset);  
    CopyMemory(dataByte, Bmp.Scanline[i], Result.WidthStep); 
    End; 
End; 
संबंधित मुद्दे