Load a jpg image from memory [closed]

asked 2016-06-20 04:50:19 -0600

peak@opencv gravatar image

From an IP camera I've read in a jpg image to a buffer in memory. If I write this buffer to a file "picture.jpg", I can open it directly in Windows. I can also open the file with OpenCV: Mat image; image = imread("picture.jpg"); imshow("video", image);

Is there a way to directly pass the memory buffer to OpenCV? Casting it to InputArray ends in error.

Best regards, Peak

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2016-06-20 06:59:37.300353

Comments

berak gravatar imageberak ( 2016-06-20 05:00:22 -0600 )edit

Well, as I said, casting the (byte) buffer to InputArray throws an error: OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in cv::_InputArray

peak@opencv gravatar imagepeak@opencv ( 2016-06-20 05:16:25 -0600 )edit

ok, please show your code.

berak gravatar imageberak ( 2016-06-20 05:36:00 -0600 )edit

Cannot paste code. Formats ugly regardless of code window or not

peak@opencv gravatar imagepeak@opencv ( 2016-06-20 05:41:20 -0600 )edit

try to read it into vector<uchar> not a BYTE *

berak gravatar imageberak ( 2016-06-20 05:44:17 -0600 )edit
  • add the code to your question
  • use the 10101 button to format it
berak gravatar imageberak ( 2016-06-20 05:51:32 -0600 )edit

I did that

peak@opencv gravatar imagepeak@opencv ( 2016-06-20 05:53:44 -0600 )edit


    BYTE *g_image; // byte array for the jpg picture from the camera
    // <allocate g_image, read from camera to g_image>
    // g_image now contains a jpg image of approx 40 000 bytes and displays ok if saved to file
    //
    Mat image;
    image = imdecode((InputArray)g_image, CV_LOAD_IMAGE_UNCHANGED); // Error here, unhandled exeption from OpenCV
    imshow("video", image);


peak@opencv gravatar imagepeak@opencv ( 2016-06-20 05:54:22 -0600 )edit

again, use a vector, not a pointer

berak gravatar imageberak ( 2016-06-20 05:59:32 -0600 )edit

I did that (you see the old code) and it's working now. Thank You!

peak@opencv gravatar imagepeak@opencv ( 2016-06-20 06:53:44 -0600 )edit