Ask Your Question

how to decode by imdecode

asked 2018-10-30 14:59:36 -0500

biquaternion gravatar image

Hi, there! I'm using python cv2. Explain me, please, why does this code (below) returns None (I.e. im == None eventually) ?

   # case 1
    frame = np.random.uniform(0, 255, (768, 576, 3))
    im = cv2.imdecode(frame, cv2.IMREAD_COLOR)
    print(im)  #  None we see in output
   # case 2
    frame = np.random.uniform(0, 255, (768, 576))
    im = cv2.imdecode(frame, cv2.IMREAD_UNCHANGED)
    print(im)  #  output is None as well
  # etc...

And show me the right way, please!

edit retag flag offensive close merge delete


you seem to have no idea, what imdecode is about (or when it should be used)

berak gravatar imageberak ( 2018-10-30 16:24:30 -0500 )edit

Yes I do. I wanted to read binary data from file. When I use C++, it's rather easy - just create cv::Mat instance from data pointer. But I hadn't found the same in python wrapper. As I got from answer below the np.array is already the same as Mat object.

biquaternion gravatar imagebiquaternion ( 2018-10-31 02:13:48 -0500 )edit

no, the input to imdecode is a (1d) byte array like a file on disc (including headers, compressed pixels, etc)

that's not the same as a numpy image in memory, which is, what you're trying above

berak gravatar imageberak ( 2018-10-31 02:35:52 -0500 )edit

I've just tried to read an image from a buffer in memory as is. Didn't need any coding like jpg etc. I guess, the closest description in docs is the: link textReads an image from a buffer in memory. (c) Thank you for explanation

biquaternion gravatar imagebiquaternion ( 2018-10-31 06:14:47 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-30 15:21:33 -0500

LBerger gravatar image

updated 2018-10-30 15:30:50 -0500

an example

import numpy as np
import cv2 as cv

frame=np.zeros((32,32,3), np.uint8)
bufjpg = bytearray(buf)
fs = open("toto.jpg", "wb")
print (buf[0:15].tostring())

encode is to convert a Mat in an jpg buffer (example). you can write buffer to disk an open this file in jpg. You can decode this buffer (imdecode) and display on screen

In your example frame is a set of pixel and it is already decode

edit flag offensive delete link more



Thanks! Now it's clear

biquaternion gravatar imagebiquaternion ( 2018-10-31 02:14:34 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-10-30 14:59:36 -0500

Seen: 86 times

Last updated: Oct 30 '18