计算机视觉(实训一) 嗨宝子们小编也是来到了大专的大二期末了马上就要毕业了终于要毕业了。但是我们还有一次实训是关于计算机视觉及技术应用的实训。分享给大家~~~ 小编使用的是这本书哦import cv2 import os import random import matplotlib.pyplot as plt def display_image(path): filelistos.listdir(path) image[] for i in range(10): trandom.randint(0,len(filelist)-1) image.append(path/filelist[t]) i1 for img_path in image: plt.subplot(2,5,i) imgcv2.imread(img_path) plt.imshow(img) plt.axis(off) ii1 plt.show() SRC_PATH./data/ filelistos.listdir(SRC_PATH) print({}文件夹中共有{}个文件.format(SRC_PATH,len(filelist))) display_image(SRC_PATH) DST1_PATH./Resources/resized_data/ if not os.path.exists(DST1_PATH): os.makedirs(DST1_PATH) DST2_PATH./Resources/filped_data/ if not os.path.exists(DST2_PATH): os.makedirs(DST2_PATH) def rdnsize(image,width,height): h,w,dimage.shape if hheight or wwidth: dstcv2.resize(image,(width,height)) else: yrandom.randint(0,h-height) xrandom.randint(0,w-width) dstimage[y:yheight,x:xwidth,:] return dst for cnt,ff in enumerate(filelist): path_filenameos.path.join(SRC_PATH,ff) imagecv2.imread(path_filename) if image is None: print(Faild to read image:,path_filename) continue dstrdnsize(image,224,224) filenaame{}_{:03d}.jpg.format(resize,cnt) resizedos.path.join(DST1_PATH,filenaame) cv2.imwrite(resized,dst) print({}文件夹下共有{}个文件.format(DST1_PATH,cnt1)) display_image(SRC_PATH) def rdnflip(image): filpcoderandom.randint(-1,1) dstcv2.flip(image,filpcode) return dst for cnt,ff in enumerate(filelist): path_filenameos.path.join(SRC_PATH,ff) imagecv2.imread(path_filename) if image is None: print(Faild to read image:,path_filename) continue dstrdnflip(image) filenmae{}_{:03d}.jpg.format(flip,cnt) filpedos.path.join(DST2_PATH,filenmae) cv2.imwrite(filped,dst) print({}文件夹下共有{}个文件.format(DST2_PATH,cnt1)) display_image(DST2_PATH)