Python脚本019:生成光照热力图

该代码的作用是体现图片的可视化结果,处理是批量化处理
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😄

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值