import numpy as np
import cv2
import os
# Your file path
file_dep = open(r"E:\\data\\3DHuman-Detection\\withoutlabel\\20180715_50_50_uint16_amp0.bin", 'rb')
i=0
while 1:
frame = np.frombuffer(file_dep.read(320 * 240 * 2), dtype=np.uint16)
img = frame.copy().reshape(240,320)
img_u8 = (img.copy() / (6000/255)).astype(np.uint8)
cv2.imshow("img", img_u8)
__path__="E:\\data\\3DHuman-Detection\\1\\"
#实现增加后缀名存储
fileName = str(i)+".png"
i=i+1
fileName = os.path.join(__path__, fileName)
#cv2.imwrite(__path__, img_u8)
cv2.imwrite(fileName, img_u8 , [cv2.IMWRITE_PNG_COMPRESSION, 0])
#img_rgb = cv2.applyColorMap(img_u8, cv2.COLORMAP_JET)
# cv2.imshow("img_rgb", img_rgb)
cv2.waitKey(10)
划分bin文件成128k
# -*- coding: utf-8 -*-
#按照大小分割文件
import os
filename = "E:\\data\\3DHuman-Detection\\withoutlabel\\20180715_50_50_uint16_amp0.bin"#需要进行分割的文件,请修改文件名
size = 131072 #分割大小约128K
def mk_SubFile(srcName,sub,buf):
[des_filename, extname] = os.path.splitext(srcName)
filename = des_filename + '_' + str(sub) + extname
print( '生成子文件ing: %s' %filename)
with open(filename,'wb') as fout:
fout.write(buf)
return sub+1
def split_By_size(filename,size):
with open(filename,'rb') as fin:
buf = fin.read(size)
sub = 1
while len(buf)>0:
sub = mk_SubFile(filename,sub,buf)
buf = fin.read(size)
print("ok")
if __name__=="__main__":
split_By_size(filename, size)
将bin转换成8位的灰度图(具有resize功能)
# coding=utf-8
import cv2
import numpy as np
import os
def convert_bin2png():
"""把单通道的bin文件转换为png"""
for i in range(pic_num):
bin_name = "20180715_50_50_uint16_amp0_"+"{}.bin".format(i+1)
# bin_name = "manba_1650_1800_rgb_888-"+"{}.bin".format(i)
input_name = path + bin_name # 输入图像文件名
# input_name = path+"xy_1800_2600_rgb_888-"+bin_name
# input_name = path+"xy_1800_2600_depth_u16-"+bin_name
image = np.fromfile(input_name, dtype=np.uint16) # 和bin文件的位数一致才可以转,不然报错
# image = np.fromfile(input_name, dtype=np.uint8)# rgb图像为8位3通道
image_gray = np.reshape(image, (height, width, channel))
output_name = "20180715_50_50_uint16_amp0_"+"{}.png".format(i+1) # 输出图片文件名
# output_name ="manba_1650_1800_rgb_888-" "{}.png".format(i)
out_path_name = path1 + output_name # 输出图片路径
#dst = cv2.resize(image_gray, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
cv2.imwrite(out_path_name, image_gray , [cv2.IMWRITE_PNG_COMPRESSION, 0])
print("already wrote", i + 1, "/", pic_num, "pictures") # 浏览写入进度
def convert_bin2video():
""""""
size = (width, height) # 保存图片的尺寸
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
video_writer = cv2.VideoWriter(path + '_out_video.avi', fourcc, fps, size)
for i in range(0, pic_num):
bin_name = "{}.bin".format(i)
input_name = path + bin_name
image = np.fromfile(input_name, dtype=np.uint8)
frame = np.reshape(image, (height, width, channel))
video_writer.write(frame)
print("converting", i, "th picture to video")
video_writer.release()
if __name__ == '__main__':
path = r"E:\\data\\3DHuman-Detection\\withoutlabel\\" # 输入bin文件的路径
path1 = r"E:\\data\\3DHuman-Detection\\1\\" # 输入png文件的路径
# path1= "E:\\data\\img\\"
pic_num = len(os.listdir(path)) # 自动获取图片数量
width = 320 # 图片宽度
height = 240 # 图片高度
channel = 1 # 通道数
# channel = 1 # 灰度图和深度图通道数
fps = 30 # 保存视频的FPS,30:正常速度, 60:2倍速
# 转换成图片帧
convert_bin2png()
# 转换成视频
#convert_bin2video()