訂閱
糾錯
加入自媒體

如何使用Python和OpenCV實現(xiàn)對象檢測任務的數據擴充過程?

def colorjitter(img, cj_type="b"):
   '''
   ### Different Color Jitter ###
   img: image
   cj_type: {b: brightness, s: saturation, c: constast}
   '''
   if cj_type == "b":
       # value = random.randint(-50, 50)
       value = np.random.choice(np.array([-50, -40, -30, 30, 40, 50]))
       hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
       h, s, v = cv2.split(hsv)
       if value >= 0:
           lim = 255 - value
           v[v > lim] = 255
           v[v <= lim] += value
       else:
           lim = np.absolute(value)
           v[v < lim] = 0
           v[v >= lim] -= np.absolute(value)
       final_hsv = cv2.merge((h, s, v))
       img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
       return img
   
   elif cj_type == "s":
       # value = random.randint(-50, 50)
       value = np.random.choice(np.array([-50, -40, -30, 30, 40, 50]))
       hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
       h, s, v = cv2.split(hsv)
       if value >= 0:
           lim = 255 - value
           s[s > lim] = 255
           s[s <= lim] += value
       else:
           lim = np.absolute(value)
           s[s < lim] = 0
           s[s >= lim] -= np.absolute(value)
       final_hsv = cv2.merge((h, s, v))
       img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
       return img
   
   elif cj_type == "c":
       brightness = 10
       contrast = random.randint(40, 100)
       dummy = np.int16(img)
       dummy = dummy * (contrast/127+1) - contrast + brightness
       dummy = np.clip(dummy, 0, 255)
       img = np.uint8(dummy)
       return img
添加噪聲通常,噪聲被認為是圖像中不可預料的因素,然而,有幾種類型的噪聲(如高斯噪聲、椒鹽噪聲)可以用于數據擴充,在深度學習中,添加噪聲是一種非常簡單而有益的數據擴充方法。在下面的例子中,為了增強數據,將高斯噪聲和椒鹽噪聲添加到原始圖像中。

對于那些無法識別高斯噪聲和椒鹽噪聲區(qū)別的人,高斯噪聲的取值范圍取決于配置,從0到255,因此,在RGB圖像中,高斯噪聲像素可以是任何顏色。相反,椒鹽噪聲像素只能有兩個值:0或255,分別為黑色(椒)或白色(鹽)。def noisy(img, noise_type="gauss"):
   '''
   ### Adding Noise ###
   img: image
   cj_type: {gauss: gaussian, sp: salt & pepper}
   '''
   if noise_type == "gauss":
       image=img.copy()
       mean=0
       st=0.7
       gauss = np.random.normal(mean,st,image.shape)
       gauss = gauss.astype('uint8')
       image = cv2.add(image,gauss)
       return image
   
   elif noise_type == "sp":
       image=img.copy()
       prob = 0.05
       if len(image.shape) == 2:
           black = 0
           white = 255            
       else:
           colorspace = image.shape[2]
           if colorspace == 3:  # RGB
               black = np.array([0, 0, 0], dtype='uint8')
               white = np.array([255, 255, 255], dtype='uint8')
           else:  # RGBA
               black = np.array([0, 0, 0, 255], dtype='uint8')
               white = np.array([255, 255, 255, 255], dtype='uint8')
       probs = np.random.random(image.shape[:2])
       image[probs < (prob / 2)] = black
       image[probs > 1 - (prob / 2)] = white
       return image
過濾本文介紹的最后一個數據擴充過程是過濾。與添加噪聲類似,過濾也很簡單,易于實現(xiàn)。在實現(xiàn)中使用的三種濾波類型包括模糊(均值)、高斯和中值。

def filters(img, f_type = "blur"):
   '''
   ### Filtering ###
   img: image
   f_type: {blur: blur, gaussian: gaussian, median: median}
   '''
   if f_type == "blur":
       image=img.copy()
       fsize = 9
       return cv2.blur(image,(fsize,fsize))
   
   elif f_type == "gaussian":
       image=img.copy()
       fsize = 9
       return cv2.GaussianBlur(image, (fsize, fsize), 0)
   
   elif f_type == "median":
       image=img.copy()
       fsize = 9
       return cv2.medianBlur(image, fsize)
總結

在這篇文章中,主要向大家介紹了一個關于對象檢測任務中數據擴充實現(xiàn)的教程。你們可以在這里找到完整實現(xiàn)。https://github.com/tranleanh/data-augmentation

<上一頁  1  2  3  
聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權或其他問題,請聯(lián)系舉報。

發(fā)表評論

0條評論,0人參與

請輸入評論內容...

請輸入評論/評論長度6~500個字

您提交的評論過于頻繁,請輸入驗證碼繼續(xù)

暫無評論

暫無評論

    掃碼關注公眾號
    OFweek人工智能網
    獲取更多精彩內容
    文章糾錯
    x
    *文字標題:
    *糾錯內容:
    聯(lián)系郵箱:
    *驗 證 碼:

    粵公網安備 44030502002758號