Opencv Python显示原始图像 [英] Opencv Python display raw image

查看:219
本文介绍了Opencv Python显示原始图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何显示包含640x480像素信息的原始图像,每个像素为8位。 (灰色图像)

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

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

I need to go from an np array to Mat format to be able to display the 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)

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

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

推荐答案

.RAW文件不受支持OpenCV 查看imread

.RAW files are not supported in OpenCV see imread,

但该文件可以用Python打开并用Numpy解析

But the file can be opened with Python and parsed with 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()

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

This makes a numpy array that can be directly manipulated by OpenCV

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

这篇关于Opencv Python显示原始图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆