一、imguag簡介
備選參考的圖片擴大框架:kears Imagedatagenerator
參考文檔
https://imgaug.readthedocs.io/en/latest/
https://github.com/aleju/imgaug
python3.7
numpy1.17.0
參數選擇:
- 如果可能,應使用最近鄰插值或線性插值,因為它們比其他選項要快得多。使用插值的大多數增強器提供
order
參數(0 =最近鄰,1 =線性)或interpolation
參數(“最近鄰”,“線性”)。 keep_size=True
在所有更改圖像尺寸的增強器中,默認設置為使用。這很方便,因為它可以確保圖像尺寸不會因擴展而改變。但是,它的確會導致明顯的性能下降,通常不僅僅使帶寬減半。keep_size=False
盡可能嘗試 。您仍然可以在擴充后或使用來手動調整圖像的大小KeepSizeByResize(Sequential(<augmenters>))
。- 當增強器提供以用戶定義的方式填充新創建的像素的模式(例如
pad_mode=constant
,Pad
以指定的恆定顏色填充所有填充的像素)時,使用edge
代替constant
通常不會帶來明顯的性能損失。
具體的增強器建議:
- 對於存在元素級同級的增強器(例如
Multiply
和MultiplyElementwise
),元素級增強器通常比非元素級的顯着慢。 - 如果需要模糊處理,
AverageBlur
是最快的選擇,其次是GaussianBlur
。 - 在較粗糙的圖像(例如
CoarseDropout
vsDropout
)上運行的增強器可能比其非粗略的兄弟姐妹快得多。 - 對比度歸一化增強器在性能上均具有可比性,但基於直方圖的增強器明顯較慢。
PiecewiseAffine
是一個非常慢的增幅器,通常應由ElasticTransformation代替,ElasticTransformation可以實現類似的輸出,並且速度要快得多。Superpixels
是一個相當緩慢的增強器,通常應該包裝起來,例如Sometimes
不要經常使用它並降低其性能影響。- 除天氣
FastSnowyLandscape
增速器外,其他增速器都相當緩慢,僅在合理時才使用。
圖片
以下數字代表小圖像(64x64x3
)和大圖像(224x224x3
)。B=1
表示的批量大小1
,B=128
其中一個128
。
https://imgaug.readthedocs.io/en/latest/source/performance.html
二、安裝imguag
官網文檔:https://imgaug.readthedocs.io/en/latest/source/installation.html
在anaconda上安裝
conda config --add channels conda-forge
conda install imgaug
三、imguag使用方法
https://blog.csdn.net/limiyudianzi/article/details/86497305
https://blog.csdn.net/qq_38451119/article/details/82417412
例一:邊界框編#邊界框
import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage ia.seed(1) image = ia.quokka(size=(256, 256)) bbs = BoundingBoxesOnImage([ BoundingBox(x1=65, y1=100, x2=200, y2=150), BoundingBox(x1=150, y1=80, x2=200, y2=130) ], shape=image.shape) seq = iaa.Sequential([ iaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect BBs iaa.Affine( translate_px={"x": 40, "y": 60}, scale=(1.2, 1.2) ) # 在x / y軸上平移40 / 60px,縮放到50-70%,影響BB ]) # Augment BBs and images. image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) # print coordinates before/after augmentation (see below) # use .x1_int, .y_int, ... to get integer coordinates for i in range(len(bbs.bounding_boxes)): before = bbs.bounding_boxes[i] after = bbs_aug.bounding_boxes[i] print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % ( i, before.x1, before.y1, before.x2, before.y2, after.x1, after.y1, after.x2, after.y2) ) # image with BBs before/after augmentation (shown below) image_before = bbs.draw_on_image(image, size=2) image_after = bbs_aug.draw_on_image(image_aug, size=2, color=[0, 0, 255]) path_name='F:/esint/smoking_Recognition/test/starzhai' #設置自己的保存路徑
img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image_before) time.sleep(1) #以當前時間命名,每隔一秒保存一次圖片 img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image_after) #能夠進行的操作 平移,放縮,旋轉(?怎么整?)
實現效果:
例二:熱力圖變換
#熱力圖 import imageio import numpy as np import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.heatmaps import HeatmapsOnImage ia.seed(1) # Load an example image (uint8, 128x128x3). image = ia.quokka(size=(128, 128), extract="square") depth = np.linspace(0, 50, 128).astype(np.float32) # 128 values from 0.0 to 50.0 depth = np.tile(depth.reshape(1, 128), (128, 1)) # change to a horizontal gradient depth[64-2:64+2, 16:128-16] = 0.75 * 50.0 # line from left to right depth[16:128-16, 64-2:64+2] = 1.0 * 50.0 # line from top to bottom depth = HeatmapsOnImage(depth, shape=image.shape, min_value=0.0, max_value=50.0) depth = depth.avg_pool(2) # Define our augmentation pipeline. seq = iaa.Sequential([ iaa.Dropout([0.05, 0.2]), # drop 5% or 20% of all pixels iaa.Sharpen((0.0, 1.0)), # sharpen the image iaa.Affine(rotate=(-45, 45)), # rotate by -45 to 45 degrees (affects heatmaps) iaa.ElasticTransformation(alpha=50, sigma=5) # apply water effect (affects heatmaps) ], random_order=True) # Augment images and heatmaps. images_aug = [] heatmaps_aug = [] for _ in range(5): images_aug_i, heatmaps_aug_i = seq(image=image, heatmaps=depth) images_aug.append(images_aug_i) heatmaps_aug.append(heatmaps_aug_i) cells = [] for image_aug, heatmap_aug in zip(images_aug, heatmaps_aug): cells.append(image) # column 1 cells.append(image_aug) # column 2 cells.append(heatmap_aug.draw_on_image(image_aug)[0]) # column 3 cells.append(heatmap_aug.draw(size=image_aug.shape[:2])[0]) # column 4 cells.append(heatmap_aug.draw(size=image_aug.shape[:2], cmap=None)[0]) # column 5 path_name='F:/esint/smoking_Recognition/test/starzhai' img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image_aug) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,heatmap_aug.draw_on_image(image_aug)[0]) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,heatmap_aug.draw(size=image_aug.shape[:2])[0]) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,heatmap_aug.draw(size=image_aug.shape[:2], cmap=None)[0]) time.sleep(1)
voc數據集的擴增方法
https://blog.csdn.net/coooo0l/article/details/84492916
2.數據變換類別
iaa.AdditiveGaussianNoise(scale=0.2*255)1#加噪聲
iaa.GaussianBlur(sigma=(0.0, 3.0)1#變模糊
iaa.AllChannelsCLAHE(clip_limit=(1, 10))#圖像增強
iaa.Affine(rotate=-45)#左旋轉45度
iaa.Affine(rotate=45)#右旋轉45度
iaa.Affine(scale=(0.4, 0.7))#Scale images to a value of 50 to 150% of their original size變大
iaa.Affine(scale=(1.3, 1.6))#Scale images to a value of 50 to 150% of their original size#縮小
iaa.GammaContrast((0.5, 2.0), per_channel=True)#色相變暗
iaa.Grayscale(alpha=(0.0, 1.0))#色相變灰
iaa.PiecewiseAffine(scale=(0.01, 0.05))#扭曲圖像
四、批量處理圖片生成對應xml文件詳細實現過程
①默認老鼠圖片以及標注框坐標,輸出10張圖片及其標注框坐標
#邊界框 import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage import time import cv2 #圖像歸一化 ia.seed(1) image = ia.quokka(size=(256, 256)) bbs = BoundingBoxesOnImage([ BoundingBox(x1=65, y1=100, x2=200, y2=150), BoundingBox(x1=150, y1=80, x2=200, y2=130) ], shape=image.shape) #aug1=iaa.Affine(rotate=45)#右旋轉45度 #seq = iaa.Sequential([ aug1=iaa.AdditiveGaussianNoise(scale=0.2*255)#加噪聲 aug2=iaa.GaussianBlur(sigma=(0.0, 3.0)) #變模糊 aug3=iaa.AllChannelsCLAHE(clip_limit=(1,10))#圖像增強 aug4=iaa.Affine(rotate=-45)#左旋轉45度 aug5=iaa.Affine(rotate=45)#右旋轉45度 aug6=iaa.Affine(scale=(0.4, 0.7))#Scale images to a value of 50 to 150% of their original size變大 aug7=iaa.Affine(scale=(1.3, 1.6))#Scale images to a value of 50 to 150% of their original size#縮小 aug8=iaa.GammaContrast((2.5, 2.5), per_channel=True)#色相變暗 aug9=iaa.Grayscale(alpha=(0.9))#色相變灰 aug10=iaa.PiecewiseAffine(scale=(0.08))#扭曲圖像 #]) #image_aug, bbs_aug = aug1(image=image, bounding_boxes=bbs) image1,bbs1=aug1(image=image, bounding_boxes=bbs) image2,bbs2=aug2(image=image, bounding_boxes=bbs) image3,bbs3=aug3(image=image, bounding_boxes=bbs) image4,bbs4=aug4(image=image, bounding_boxes=bbs) image5,bbs5=aug5(image=image, bounding_boxes=bbs) image6,bbs6=aug6(image=image, bounding_boxes=bbs) image7,bbs7=aug7(image=image, bounding_boxes=bbs) image8,bbs8=aug8(image=image, bounding_boxes=bbs) image9,bbs9=aug9(image=image, bounding_boxes=bbs) image10,bbs10=aug10(image=image, bounding_boxes=bbs) for i in range(len(bbs.bounding_boxes)): before = bbs.bounding_boxes[i] after = bbs_aug.bounding_boxes[i] print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % ( i, before.x1, before.y1, before.x2, before.y2, after.x1, after.y1, after.x2, after.y2) ) #畫標注框 #image_before = bbs.draw_on_image(image, size=2) #image_after = bbs_aug.draw_on_image(image_aug, size=2, color=[0, 0, 255]) image1 = bbs1.draw_on_image(image1, size=2) image2 = bbs2.draw_on_image(image2, size=2) image3 = bbs3.draw_on_image(image3, size=2) image4 = bbs4.draw_on_image(image4, size=2) image5 = bbs5.draw_on_image(image5, size=2) image6 = bbs6.draw_on_image(image6, size=2) image7 = bbs7.draw_on_image(image7, size=2) image8 = bbs8.draw_on_image(image8, size=2) image9 = bbs9.draw_on_image(image9, size=2) image10 = bbs10.draw_on_image(image10, size=2) path_name='F:/esint/smoking_Recognition/test/imgaug' img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image1) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image2) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image3) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image4) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image5) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image6) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image7) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image8) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image9) time.sleep(1) img_name = '%s/%s.jpg'%(path_name,time.strftime("%Y%m%d%H%M%S",time.localtime())) cv2.imwrite(img_name,image10)
②更改輸入圖片,更改色階(不要發紫的顏色)
③通過xml文件中的矩形框信息在圖片上畫框
④生成圖片和對應的xml文件
⑤檢測xml文件中的信息是否准確的落在原圖合適的位置上
⑥批量生成圖片及其對應的xml文件(使xml文件名與圖片名一一對應)