下面是一个简单的将raw图显示出来的python代码
代码中所使用的raw图上传不了,如有需要,请留邮箱
代码如有错,烦请指出,谢谢
import cv2
from PIL import Image as im
import numpy as np
class pyOpenCvTest():
'''
author:Mars
function:将1600x1200的raw图显示出来
res:cwf__1600x1200_10_3.raw
step1:
将raw数据通过numpy的fromfile()函数以uint16读入,读入后是一个size为1600x1200的1维数组
Tip:因为raw数据是AD10bit,所以要用uint16
step2:
将数组reshape成1200x1600,因为图像是按1200x1600结构存储的
step3:
将图像从10bit转成8bit。
Question:为什么要转成8bit输出
10bit输出时画面PIL.image.show()会过曝,猜测show()是按YUV 8bit显示的。
step4:
通过PIL的image类的fromarray()函数将数组转成图像输出
'''
rawPath = r"G:\OpenCvTest\pyOpenCvTest\res\cwf__1600x1200_10_3.raw"
imgSize = (1200,1600)
def cvTest():
#step1:
rawData = np.fromfile(pyOpenCvTest.rawPath,dtype = 'uint16')
print(np.shape(rawData)) #1920000
#step2:
reshapeRawData = np.reshape(rawData,pyOpenCvTest.imgSize)
#step3:
for i in range(reshapeRawData.shape[0]):
for j in range(reshapeRawData.shape[1]):
#将10bit数转成8bit
val = np.round(reshapeRawData[i][j] >> 2)
if val >= 255:
reshapeRawData[i][j] = 255
elif val <= 0:
reshapeRawData[i][j] = 0
else:
reshapeRawData[i][j] = val
print(reshapeRawData.max())#200
print(reshapeRawData.min())#3
img = im.fromarray(reshapeRawData)
img.show()
显示结果