CPM项目处理数据代码

记录项目中用写的小代码,并未优化,只是简单的完成功能。 crop 2448*560

import cv2 import numpy as np from skimage import io import os save_id = 0ori_image_folder = "E:/ATL350test/" crop_image_folder = "E:/ATL350_croptest/"def Image_Crop(image): global save_id gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray[1420:, :] = 0; prob = np.zeros((1, gray.shape[0]), dtype=np.float32) print(prob.shape)#flag = np.sum(gray[:,0:2448])/(560*2448) #print(flag) #print("~~~~~~~~~~~~~") #print(type(flag)) #if(flag<40): #continue #else: prob = np.sum(gray[:,0:2448], axis=1) / np.sum(gray[:,0:2448]) print(prob.shape) print(np.sum(prob)) expect = np.sum(np.multiply(range(gray.shape[0]), prob)) print(expect) image_save_gray = gray[int(expect - crop_weight // 2):int(expect + crop_weight // 2),:]image_save = cv2.cvtColor(image_save_gray, cv2.COLOR_GRAY2BGR) # print(save_id) if save_id < 10: io.imsave(crop_image_folder + '0000' + str(save_id) + '.bmp', image_save) elif save_id < 100: io.imsave(crop_image_folder + '000' + str(save_id) + '.bmp', image_save) elif save_id < 1000: io.imsave(crop_image_folder + '00' + str(save_id) + '.bmp', image_save) elif save_id < 10000: io.imsave(crop_image_folder + '0' + str(save_id) + '.bmp', image_save) elif save_id < 100000: io.imsave(crop_image_folder + str(save_id) + '.bmp', image_save) save_id = save_id+1for file in os.listdir(ori_image_folder): print(file) nw = "E:/ATL350/"+file+"/" print(nw)for filename in os.listdir(nw): print(filename) image = cv2.imread(nw + filename) Image_Crop(image)

crop 560*560
# ps:感谢hby师弟 import cv2 import numpy as np fromskimage import io import os############ H W C crop_height = 560 crop_weight = 560 crop_overlap = 25save_id = 0ori_image_folder = "E:/ATL350/" crop_image_folder = "E:/ATL350_crop/"def Image_Crop(image): global save_id gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray[1420:, :] = 0; prob = np.zeros((1, gray.shape[0]), dtype=np.float32) print(prob.shape) for height in range(gray.shape[1]//crop_height + 1): if height == 0: flag = np.sum(gray[:,height*crop_height:(height+1)*crop_height])/(560*560) print(flag) print("~~~~~~~~~~~~~") print(type(flag)) if(flag<40): continue else: prob = np.sum(gray[:,height*crop_height:(height+1)*crop_height], axis=1) / np.sum(gray[:,height*crop_height:(height+1)*crop_height]) print(prob.shape) print(np.sum(prob)) expect = np.sum(np.multiply(range(gray.shape[0]), prob)) print(expect) image_save_gray = gray[int(expect - crop_weight // 2):int(expect + crop_weight // 2), height * crop_height:(height + 1) * crop_height] elif height == gray.shape[1]//crop_height: #prob = np.sum(gray[gray.shape[1]-crop_height-1:gray.shape[0]-1, :], axis=0) / np.sum(gray[gray.shape[1]-crop_height-1:gray.shape[1]-1, :]) flag1=np.sum(gray[:,gray.shape[1]-crop_height-1:gray.shape[1]-1])/(560*560) print(flag1) print("~~~~~~~~~~~~~") if(flag1<40): continue else: prob = np.sum(gray[:,gray.shape[1]-crop_height-1:gray.shape[1]-1], axis=1) / np.sum(gray[:,gray.shape[1]-crop_height-1:gray.shape[1]-1]) print(prob.shape) expect = np.sum(np.multiply(range(gray.shape[0]), prob)) image_save_gray = gray[ int(expect - crop_weight // 2):int(expect + crop_weight // 2),gray.shape[1]-crop_height-1:gray.shape[1]-1] else: flag2 = np.sum(gray[:,height*crop_height-crop_overlap:(height+1)*crop_height-crop_overlap])/(560*560) print(flag2) print("~~~~~~~~~~~~~") if(flag2<40): continue else: prob = np.sum(gray[:,height*crop_height-crop_overlap:(height+1)*crop_height-crop_overlap], axis=1) / np.sum(gray[:,height*crop_height:(height+1)*crop_height]) expect = np.sum(np.multiply(range(gray.shape[0]), prob)) image_save_gray = gray[ int(expect - crop_weight // 2):int(expect + crop_weight // 2),height * crop_height:(height + 1) * crop_height]image_save = cv2.cvtColor(image_save_gray, cv2.COLOR_GRAY2BGR) # print(save_id) if save_id < 10: io.imsave(crop_image_folder + '0000' + str(save_id) + '.bmp', image_save) elif save_id < 100: io.imsave(crop_image_folder + '000' + str(save_id) + '.bmp', image_save) elif save_id < 1000: io.imsave(crop_image_folder + '00' + str(save_id) + '.bmp', image_save) elif save_id < 10000: io.imsave(crop_image_folder + '0' + str(save_id) + '.bmp', image_save) elif save_id < 100000: io.imsave(crop_image_folder + str(save_id) + '.bmp', image_save) save_id = save_id+1for file in os.listdir(ori_image_folder): print(file) nw = "E:/ATL350/"+file+"/" print(nw)for filename in os.listdir(nw): print(filename) image = cv2.imread(nw + filename) Image_Crop(image)

2448中间切割
import cv2 import numpy as np import os save_id = 0 img_path = 'F:\\cpmdata\\ATL350_croptest\\'def dev(img_path,name): img = cv2.imread(img_path) cl = np.zeros([560,1224,3]) cr = np.zeros([560,1224,3]) cl = img[:,0:1224] cr = img[:,1224:2448]cv2.imwrite('F:\\cpmdata\\1224_560\\'+name.split('.')[0]+'L.bmp',cl) cv2.imwrite('F:\\cpmdata\\1224_560\\'+name.split('.')[0]+'R.bmp',cr)if __name__ == '__main__': for root,dirs,files in os.walk(img_path): print(root) for name in files: print(os.path.join(root,name)) dev(os.path.join(root,name),name)

标注文件 坐标反算
import os def wtxt(rtxt,ltxt): rtxt ='F:\\txttest\\annotation\\'+rtxt ltxt = 'F:\\txttest\\annotation\\'+ltxt input_file = open(rtxt, 'r+') f_new = open(ltxt, 'a')for line in input_file: line1 = line.strip() line1 = line.split(' ') line2 = line1if(int(line1[-4])==0): s = str(int(line1[1])+1224) line2[1] = str(s) print(line2) tems=' ' tema = tems.join(line2)#tema = tema.replace(line1[1], s) #f_new.write('\n') f_new.write(tema) if (int(line1[-4]) == 1):tem = int(line1.index(line1[-3])) for i in range(1, tem - 1, 2):s = str(int(line1[i]) + 1224) line2[i] = str(s) tems = ' ' tema = tems.join(line2)f_new.write(tema) input_file.close() f_new.close() img_path= 'F:\\txttest\\annotation' L = [] R = [] for root,dirs,files in os.walk(img_path): for i in range(0,len(files),2): L.append(files[i]) for i in range(1,len(files),2): R.append(files[i]) for i in range(len(L)): wtxt(R[i],L[i])

copy rename files
import shutil import os#img_path= 'F:\\txttest\\annotation' img_path1= 'F:\\txttest\\2448annotation' des_path = 'F:\\txttest\\2448data' L=[] # '''rename''' # for root,dirs,files in os.walk(img_path1): #for file in files: #os.rename(os.path.join(root,file),os.path.join(root,file.split('L')[0]+file.split('L')[1])) # '''movefile''' # for root,dirs,files in os.walk(img_path1): #for i in range(0,len(files),2): #shutil.copy(img_path+'\\'+files[i],'F:\\txttest\\2448annotation')for root,dirs,files in os.walk(img_path1): for i in files: print('F:\\cpmdata\\ATL350_2448\\'+i.split('.')[0]+'.bmp') shutil.copy('F:\\cpmdata\\ATL350_2448\\'+i.split('.')[0]+'.bmp','F:\\txttest\\2448data')

    推荐阅读