Ask Your Question
0

how to decode by imdecode

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

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

Comments

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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

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

LBerger gravatar image

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

an example

import numpy as np
import cv2 as cv


frame=np.zeros((32,32,3), np.uint8)
frame[16,0:31,:]=255
ret,buf=cv.imencode("toto.jpg",frame)
bufjpg = bytearray(buf)
fs = open("toto.jpg", "wb")
fs.write(bufjpg)
print (buf[0:15].tostring())
img=cv.imdecode(buf,cv.IMREAD_COLOR)
cv.imshow("img",img)
cv.waitKey(0)

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

Comments

1

Thanks! Now it's clear

biquaternion gravatar imagebiquaternion ( 2018-10-31 02:14:34 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 19,226 times

Last updated: Oct 30 '18