#coding:utf -8 import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" from PIL import Image import numpy as np import math from numba import jit import cv2 import time import glob from sklearn.preprocessing import MinMaxScaler np.seterr(divide='ignore',invalid='ignore') def flow_rgb(x_img,y_img): n,m=np.array(x_img).shape fuzhi=np.zeros((n,m)) rgb = np.zeros((n,m,3)) i=0 j=0 while i <n: while j<m: fuzhi[i,j]=math.sqrt(math.pow(x_img[i,j],2)+math.pow(y_img[i,j],2)) j+=1 i+=1 j=0 #print fuzhi #x_img.show() #转成RGB图片,并且归一化(每个通道的像素减去改通道像素的最大值) rgb[:, :, 0] = (x_img- np.ones((n,m))*np.min(x_img)) *1.0 / ( np.ones((n,m))*np.max(x_img) - np.ones((n,m))*np.min(x_img) ) rgb[:, :, 1] = (y_img- np.ones((n,m))*np.min(y_img)) *1.0 / ( np.ones((n,m))*np.max(y_img) - np.ones((n,m))*np.min(y_img) ) rgb[:, :, 2] = (fuzhi- np.ones((n,m))*np.min(fuzhi)) *1.0 / ( np.ones((n,m))*np.max(fuzhi) - np.ones((n,m))*np.min(fuzhi) ) rgb_255=(rgb*225) rgb_255 =rgb_255.astype(np.uint8) rgb_255 = cv2.cvtColor(rgb_255, cv2.COLOR_BGR2RGB) #cv2.imshow('rgb_quanju',rgb_255) #cv2.waitKey() return rgb_255 path='/media/xuguohan/Elements/shajie/dataset/IsoGD_flows/' save_path='/media/xuguohan/E/shajie/image/GD/IsoGD/isogd_flow_rgb' dataset='train' path=os.path.join(path,dataset) save_path=os.path.join(save_path,dataset) if os.path.exists(save_path) is False: os.makedirs(save_path) wenjians=os.listdir(path)#'001'...'010'...'100' for wenjian in wenjians:#'001' wenjian_read=os.path.join(path,wenjian)#'/media/xuguohan/Elements/shajie/dataset/IsoGD_frames/train/001' wenjain_save=os.path.join(save_path,wenjian)#'/media/xuguohan/E/shajie/image/GD/IsoGD/isogd_flow_rgb/train/001' if os.path.exists(wenjain_save) is False: os.makedirs(wenjain_save) www=os.listdir(wenjian_read)#'K_00001'...'M_00121'... for KM in www: if KM.split('_')[0]=='M':#'M_00065' M_path=os.path.join(wenjian_read,KM)#'./IsoGD_frames/train/001/M_00065' M_path_save=os.path.join(wenjain_save,KM)#'./isogd_flow_rgb/train/001/M_00065' if os.path.exists(M_path_save) is False: os.makedirs(M_path_save) frame_name=os.listdir(M_path)#'flow_y_0005.jpg'...'img_0011.jpg'...'flow_x_0002.jpg' for frame_one in frame_name: if frame_one.split('_')[1]=='x':#frame_one.split('_')=['flow', 'x', '0001.jpg'] x_split=frame_one.split('x')#['flow_', '_0001.jpg'] flow_y=x_split[0]+'y'+x_split[-1] flow_x_path = os.path.join(M_path,frame_one)#'./train/001/M_00065/flow_x_0001.jpg' flow_y_path = os.path.join(M_path, flow_y)#'./train/001/M_00065/flow_y_0001.jpg' flow_x = cv2.imread(flow_x_path, 0) flow_y = cv2.imread(flow_y_path, 0) rgb=flow_rgb(flow_x, flow_y) rgb_name='img_{}'.format(x_split[-1]) rgb_path = os.path.join(M_path_save, rgb_name) print rgb_path if os.path.exists(rgb_path): print 'rgb_path exists' pass else: print 'rgb_path save' cv2.imwrite(rgb_path, rgb)