显示yuyv图像
import numpy as np
import cv2
def yuyv_to_rgb(input_file, width, height):
with open(input_file, 'rb') as f:
yuv = np.frombuffer(f.read(), dtype=np.uint8).reshape(int(height), width, 2)
rgb = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR_YUYV)
return rgb
input_file = '/home/nvidia/zyy/nvwork/ppp20230912/nv12/yuyv/9_right_front_1920_1080.bin'
width = 1920
height = 1080
rgb_image = yuyv_to_rgb(input_file, width, height)
cv2.imwrite('output2.jpg', rgb_image)
显示yuv图像
import numpy as np
import cv2
def nv12_to_rgb(input_file, width, height):
with open(input_file, 'rb') as f:
yuv = np.frombuffer(f.read(), dtype=np.uint8).reshape(int(height * 1.5), width)
rgb = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR_NV12)
return rgb
input_file = '/home/nvidia/zyy/nvwork/ppp20230912/nv12/yuv/2.yuv'
width = 1920
height = 1536
rgb_image = nv12_to_rgb(input_file, width, height)
cv2.imwrite('output2.jpg', rgb_image)
显示bin->rgb
import cv2
import numpy as np
# 读取二进制文件
with open('tensor.binary', 'rb') as file:
binary_data = file.read()
trimmed_data = binary_data[28:]
height = 600 # 图像高度
width = 1200 # 图像宽度
image_array = np.frombuffer(trimmed_data, dtype=np.uint8).reshape((height, width, 3))
cv2.imwrite('output2.jpg', image_array)
反归一化
import tensor
import numpy as np
import torch
import numpy as np
import cv2
torch.set_printoptions(sci_mode=False)
np.set_printoptions(3, suppress=True, linewidth=130)
def denorm(x):
return ((x * np.array([58.395, 57.12, 57.375]).reshape(1, 3, 1, 1)) + np.array([123.675, 116.28, 103.53]).reshape(1, 3, 1, 1)).clip(0, 255).astype(np.uint8)
for i in range(7):
iimg = i
original = np.frombuffer(open("1691056674.766666.bin", "rb").read(), dtype=np.float32).reshape(7, 3, 480, 800)
dnorm_original = denorm(original)
cv2.imwrite(f"original_{iimg}.jpg", dnorm_original[iimg].transpose(1, 2, 0))
output0 = tensor.load("output_yuyv.binary")
# B x C x H x W
# print(output0.reshape(-1)[:20])
print(output0.shape)
dnorm_output0 = denorm(output0)
cv2.imwrite(f"./result/1691056674.766666_{iimg}.jpg", dnorm_output0[iimg].transpose(1, 2, 0))
print(np.abs(original - output0).max())
print(output0.reshape(output0.shape[0], -1)[:, :20])
import numpy as np
import torch
import numpy as np
import cv2
torch.set_printoptions(sci_mode=False)
np.set_printoptions(3, suppress=True, linewidth=130)
def denorm(x):
return ((x * np.array([58.395, 57.12, 57.375]).reshape(1, 3, 1, 1)) + np.array([123.675, 116.28, 103.53]).reshape(1, 3, 1, 1)).clip(0, 255).astype(np.uint8)
for i in range(7):
iimg = i
original = np.frombuffer(open("1056674.766666.bin", "rb").read(), dtype=np.float32).reshape(7, 3, 480, 800)
dnorm_original = denorm(original)
cv2.imwrite(f"./result_zyy/1056674.766666_{iimg}.jpg", dnorm_original[iimg].transpose(1, 2, 0))
```