Opencv Python 显示原始图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18682830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:26:47  来源:igfitidea点击:

Opencv Python display raw image

pythonimageopencvnumpytype-conversion

提问by mgoubert

I can't figure out how to display a raw image wich conatains 640x480 pixel information, each pixel 8 bit. (Gray image)

我不知道如何显示包含 640x480 像素信息的原始图像,每个像素 8 位。(灰色图像)

I need to go from an np array to Mat format to be able to display the image.

我需要从 np 数组转换为 Mat 格式才能显示图像。

#!/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)

It so confusing with the cv ,cv2. I have been trying for a while now, but i can't find the solution.

它与 cv ,cv2 混淆。我已经尝试了一段时间,但我找不到解决方案。

采纳答案by DanGoodrick

.RAW files are not supported in OpenCV see imread,

OpenCV 不支持 .RAW 文件,请参阅 imread

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

但是文件可以用Python打开,用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 a numpy array that can be directly manipulated by OpenCV

这使得一个可以被 OpenCV 直接操作的 numpy 数组

import cv2
cv2.imshow('', im)
cv2.waitKey()
cv2.destroyAllWindows()

回答by Irene

Just an example if you want to save your 'raw' image to 'png' file (each pixel 32 bit, colored image):

如果您想将“原始”图像保存到“png”文件(每个像素为 32 位,彩色图像),则只是一个示例:

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")

回答by Kiran Thomas

#!/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()