如何使用Python和OpenCV實現(xiàn)對象檢測任務(wù)的數(shù)據(jù)擴充過程?
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.a(chǎn)rray([-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.a(chǎn)bsolute(value)
v[v < lim] = 0
v[v >= lim] -= np.a(chǎn)bsolute(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.a(chǎn)rray([-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.a(chǎn)bsolute(value)
s[s < lim] = 0
s[s >= lim] -= np.a(chǎn)bsolute(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
添加噪聲通常,噪聲被認為是圖像中不可預(yù)料的因素,然而,有幾種類型的噪聲(如高斯噪聲、椒鹽噪聲)可以用于數(shù)據(jù)擴充,在深度學(xué)習(xí)中,添加噪聲是一種非常簡單而有益的數(shù)據(jù)擴充方法。在下面的例子中,為了增強數(shù)據(jù),將高斯噪聲和椒鹽噪聲添加到原始圖像中。
對于那些無法識別高斯噪聲和椒鹽噪聲區(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.a(chǎn)stype('uint8')
image = cv2.a(chǎn)dd(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.a(chǎn)rray([0, 0, 0], dtype='uint8')
white = np.a(chǎn)rray([255, 255, 255], dtype='uint8')
else: # RGBA
black = np.a(chǎn)rray([0, 0, 0, 255], dtype='uint8')
white = np.a(chǎn)rray([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
過濾本文介紹的最后一個數(shù)據(jù)擴充過程是過濾。與添加噪聲類似,過濾也很簡單,易于實現(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)
總結(jié)
在這篇文章中,主要向大家介紹了一個關(guān)于對象檢測任務(wù)中數(shù)據(jù)擴充實現(xiàn)的教程。你們可以在這里找到完整實現(xiàn)。https://github.com/tranleanh/data-augmentation

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
推薦專題
- 1 AI 眼鏡讓百萬 APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語權(quán)
- 3 深度報告|中國AI產(chǎn)業(yè)正在崛起成全球力量,市場潛力和關(guān)鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級獨角獸:獲上市公司戰(zhàn)投,干人形機器人
- 5 國家數(shù)據(jù)局局長劉烈宏調(diào)研格創(chuàng)東智
- 6 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 7 百億AI芯片訂單,瘋狂傾銷中東?
- 8 Robotaxi新消息密集釋放,量產(chǎn)元年誰在領(lǐng)跑?
- 9 格斗大賽出圈!人形機器人致命短板曝光:頭腦過于簡單
- 10 一文看懂視覺語言動作模型(VLA)及其應(yīng)用