Opencv Python source image mapping

I cannot figure out how to display a raw image that encodes 640x480 pixels, each pixel is 8 bits. (Gray image)

I need to switch from np array to Mat format in order to be able to display an image.

#!/usr/bin/python import numpy as np import cv2 import sys # Load image as string from file/database fd = open('flight0000.raw') img_str = fd.read() fd.close() img_array = np.asarray(bytearray(img_str), dtype=np.uint8) img = ... Conversion to Mat graycolor cv2.imshow('rawgrayimage', img) cv2.waitKey(0) 

This is so confusing with cv, cv2. I tried for a while, but I can not find a solution.

+9
source share
4 answers

.RAW files are not supported in OpenCV .

But the file can be opened using Python and parsed using Numpy

 import numpy as np fd = open('flight0000.raw', 'rb') rows = 480 cols = 640 f = np.fromfile(fd, dtype=np.uint8,count=rows*cols) im = f.reshape((rows, cols)) #notice row, column format fd.close() 

This makes an array of arrays that OpenCV can directly manipulate.

 import cv2 cv2.imshow('', im) cv2.waitKey() cv2.destroyAllWindows() 
+8
source
 #!/usr/bin/python #code to display a picture in a window using cv2 import cv2 cv2.namedWindow('picture',cv2.WINDOW_AUTOSIZE) frame=cv2.imread("abc.jpg") cv2.imshow('picture',frame) if cv2.waitKey(0) == 27: cv2.destroyAllWindows() 
0
source

Please can someone share the .raw file

0
source

Just an example, if you want to save your raw image to a png file (each pixel is 32 bits, color image):

 import numpy as np import matplotlib.pyplot as plt img = np.fromfile("yourImage.raw", dtype=np.uint32) print img.size #check your image size, say 1048576 #shape it accordingly, that is, 1048576=1024*1024 img.shape = (1024, 1024) plt.imshow(img) plt.savefig("yourNewImage.png") 
-2
source

Source: https://habr.com/ru/post/953424/


All Articles