该代码的作用是体现图片的可视化结果,处理是批量化处理
Input:
Output:
1.将图片读取出来生成npy文件
import os
import xml.dom.minidom
import cv2
import numpy as np
from tqdm import tqdm
import random
from ipdb import set_trace
from IPython import embed
import shutil
def mkDir(path):
isExists = os.path.exists(path)
if not isExists: #判断如果文件不存在,则创建
os.makedirs(path)
def light_count(frames_all_dir):
frames_all_10 = os.listdir(frames_all_dir)
for frame in tqdm(frames_all_10):
print(frames_all_dir.split('/')[-3]+'/'+ frame)
frame_dir = os.path.join(frames_all_dir,frame)
img = cv2.imread(frame_dir)
b = img[:, :, 0]
g = img[:, :, 1]
r = img[:, :, 2]
light_img = 0.299*r + 0.587*g + 0.114*b
x,y=light_img.shape
light_heat_map = np.zeros((x, y))
light_heat_map = (light_heat_map + light_img)/2
light_mean = np.mean(light_img)
npy_path='/home/jy/xl/workstation/Car/Dataset/Test/Veri776/Npy/'
mkDir(npy_path)
np.save(os.path.join(npy_path, frame + '_heatmap.npy'),
light_heat_map)
if __name__ == '__main__':
root ='/home/jy/xl/workstation/Car/Dataset/Test/Veri776'
files = os.listdir(root)
train_dir = os.path.join(root,'light_vehicle')
mkDir(train_dir)
light_count(train_dir)
2.读取npy文件,生成heatmap图片
import os
import cv2
import numpy as np
from tqdm import tqdm
from ipdb import set_trace
from IPython import embed
def mkDir(path):
isExists = os.path.exists(path)
if not isExists: #判断如果文件不存在,则创建
os.makedirs(path)
light_npy_dir = "/home/jy/xl/workstation/Car/Dataset/Test/Veri776/Npy"
mkDir(light_npy_dir)
file_path="/home/jy/xl/workstation/Car/Dataset/Test/Veri776/light_vehicle"
filenames=os.listdir(file_path)
for filename in filenames:
heatmap_dir = "/home/jy/xl/workstation/Car/Dataset/Test/Veri776/Heatmap"
mkDir(heatmap_dir)
heatmap_file = os.path.join(heatmap_dir,filename)
light_npy_file = os.path.join(light_npy_dir,filename+'_heatmap.npy')
light_npy = np.load( light_npy_file)
light_npy = light_npy - np.min(light_npy)
cam_img = light_npy / np.max(light_npy)
cam_img = np.uint8(255 * cam_img)
heatmap = cv2.applyColorMap(cam_img, cv2.COLORMAP_JET)
cv2.imwrite(heatmap_file, heatmap)
我的思考
做光照对比的时候可以用到,感觉这种热力图真的挺好看的,找的是单张生成的,自己转成了批量生成,yyds😄