odd image using cv2 and numpy

℡╲_俬逩灬. 提交于 2019-12-25 02:13:42

问题


I am running the following code:

import cv2
import numpy
f = open("raw_image",'rb')
raw_image = f.read(720 * 1280 * 3)
image = numpy.fromstring(raw_image, dtype='uint8')
image = image.reshape((720, 1280, 3))
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

and I get this result: the image is RGB image with height of 720p and width of 1280. Any Ideas how to solve it?

EDIT: I receive images from a camera with resolution of 720x1280. The image is a colored image. The file raw_image, which I read the bytes from, contains the output from the command:

gst-launch-1.0 fdsrc ! h264parse ! avdec_h264 ! filesink location=/dev/stdout

As you can see, the received image is malformed, and I dont know how to fix it.

EDIT2: this is the raw image: https://drive.google.com/file/d/1hPRUEVNFiKmiUFbzksUlzzC04teml4hw/view?usp=sharing

EDIT3: after running the code (NOTE: I am displaying only the first 720*1280 bytes):

raw_image = f.read(720 * 1280 * 3)
raw_image=raw_image[:720 * 1280]
image=numpy.frombuffer(raw_image,dtype='uint8')
image = image.reshape((720,1280))
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

I got this result: When I move some of the left part to the right, by adding the line:

image=numpy.concatenate((image[:,141:],image[:,:141]),axis=1)

I got a fine image:

can it help solving the mystery?


回答1:


If you generate a Red-Blue gradient like this with ImageMagick in the Terminal, you will see that your code works fine:

convert -size 1280x720 gradient:red-blue -depth 8 rgb:raw_image


I deduce your gst stuff is "unhappy".


The image you have shared contains this:

I converted it to a JPEG using ImageMagick like the in the Terminal:

convert -size 1280x720 -depth 8 rgb:raw_image.dms result.jpg

I deduce again that your gst stuff is "unhappy".



来源:https://stackoverflow.com/questions/51454239/odd-image-using-cv2-and-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!