全套源码下载地址:https://download.csdn.net/download/bvngh3247/10774909
主程序:
import os
import cv2
import numpy as np
from PIL import Image
#from scipy import misc
import utilty as util
search_path = 'E:/stitch/run_auto_video'
def yuv_to_rgb(yuv_path, frame_num):
#yuv_path = search_path + '/' + filename
print(yuv_path)
w = int(1080)
h = int(1920)
recon_ycbcr_image = np.zeros((h, w, 3), np.uint8)
recon_y = recon_ycbcr_image[:, :, 0]
w_c = int(w/2)
h_c = int(h/2)
#print (w_c)
#print (h_c)
recon_cb = np.zeros((h_c, w_c), np.uint8)
recon_cr = np.zeros((h_c, w_c), np.uint8)
#PIL image
#image = Image.open(pic_path)
#image.show()
#image = np.array(image)
#h,w,c
#print(image.shape)
#img = Image.fromarray(img.astype('uint8')).convert('RGB')
#im = Image.fromarray(image)
#im.save('./123.tif')
util.read_yuv420_file(yuv_path, recon_y, recon_cb, recon_cr, w, h, frame_num)
recon_cb = util.resize_image_by_pil(recon_cb, 2)
recon_cr = util.resize_image_by_pil(recon_cr, 2)
recon_ycbcr_image[:, :, 1] = recon_cb
recon_ycbcr_image[:, :, 2] = recon_cr
# yuv444 to rgb
rgb_image = util.convert_ycbcr_to_rgb(recon_ycbcr_image, False)
test_name = yuv_path[:-4] + '.JPG'
print('output =>' + test_name)
util.save_image(test_name, rgb_image)
yuv_path = search_path + '/' + '2.yuv'
frame_num = 9 #zhaopan(1,9)
yuv_to_rgb(yuv_path, frame_num)
读取yuv,并且转为RGB,图片读写程序:
import os
import numpy as np
from scipy import misc
from PIL import Image
def read_yuv420_file(r_file, y, cb, cr, w, h, frame_num):
my_file = open(r_file,'rb')
my_file.read((frame_num-1)*int(w*h*3/2))
for num in range(0, 1):
print ('frame = ' + str(frame_num))
for i in range(0, h):
for j in range(0, w):
data = my_file.read(1)
data = ord(data)
y[i, j] = data
for y in range(0, int(h / 2)):
for x in range(0, int(w / 2)):
data = my_file.read(1)
data = ord(data)
cb[y, x] = data
for y in range(0, int(h / 2)):
for x in range(0, int(w / 2)):
data = my_file.read(1)
data = ord(data)
cr[y, x] = data
def convert_ycbcr_to_rgb(ycbcr_image, jpeg_mode=True, max_value=255.0):
rgb_image = np.zeros([ycbcr_image.shape[0], ycbcr_image.shape[1], 3]) # type: np.ndarray
if jpeg_mode:
rgb_image[:, :, [1, 2]] = ycbcr_image[:, :, [1, 2]] - (128.0 * max_value / 256.0)
xform = np.array([[1, 0, 1.402], [1, - 0.344, - 0.714], [1, 1.772, 0]])
rgb_image = rgb_image.dot(xform.T)
else:
rgb_image[:, :, 0] = ycbcr_image[:, :, 0] - (16.0 * max_value / 256.0)
rgb_image[:, :, [1, 2]] = ycbcr_image[:, :, [1, 2]] - (128.0 * max_value / 256.0)
xform = np.array(
[[max_value / 219.0, 0, max_value * 0.701 / 112.0],
[max_value / 219, - max_value * 0.886 * 0.114 / (112 * 0.587), - max_value * 0.701 * 0.299 / (112 * 0.587)],
[max_value / 219.0, max_value * 0.886 / 112.0, 0]])
rgb_image = rgb_image.dot(xform.T)
return rgb_image
def resize_image_by_pil(image, scale, resampling_method="bicubic"):
width, height = image.shape[1], image.shape[0]
new_width = int(width * scale)
new_height = int(height * scale)
if resampling_method == "bicubic":
method = Image.BICUBIC
elif resampling_method == "bilinear":
method = Image.BILINEAR
elif resampling_method == "nearest":
method = Image.NEAREST
else:
method = Image.LANCZOS
if len(image.shape) == 3 and image.shape[2] == 3:
image = Image.fromarray(image, "RGB")
image = image.resize([new_width, new_height], resample=method)
image = np.asarray(image)
elif len(image.shape) == 3 and image.shape[2] == 4:
# the image may has an alpha channel
image = Image.fromarray(image, "RGB")
image = image.resize([new_width, new_height], resample=method)
image = np.asarray(image)
else:
image = Image.fromarray(image.reshape(height, width))
image = image.resize([new_width, new_height], resample=method)
image = np.asarray(image)
#image = image.reshape(new_height, new_width, 1)
return image