在摳圖技術中三分圖(trimap)經常被用到,通常使用的方法是膨脹腐蝕(一般在去除雜訊的時候先腐蝕再膨脹)。
import os
import numpy as np
import cv2
defrandom_dilate
(alpha, low=
1, high=
5, mode=
'constant'):
"""dilation. erode"""
iterations = np.random.randint(1,
20)erode_ksize = np.random.randint(low=low, high=high)
dilate_ksize = np.random.randint(low=low, high=high)
erode_kernel = cv2.getstructuringelement(
cv2.morph_ellipse,
(erode_ksize, erode_ksize)
)# 橢圓 (尺寸)
# 腐蝕
# 一般在去雜訊時先用腐蝕再用膨脹。
# cnt1 = len(np.where(msk >= 0)[0])
# cnt2 = len(np.where(msk == 0)[0])
# cnt3 = len(np.where(msk == 1)[0])
# #print("all:{} bg:{} fg:{}".format(cnt1, cnt2, cnt3))
# assert(cnt1 == cnt2 + cnt3) # 邊緣模糊所以存在兩者之間的畫素
## cnt1 = len(np.where(dilated >= 0)[0])
# cnt2 = len(np.where(dilated == 0)[0])
# cnt3 = len(np.where(dilated == 255)[0])
# #print("all:{} bg:{} fg:{}".format(cnt1, cnt2, cnt3))
# assert(cnt1 == cnt2 + cnt3)
## cnt1 = len(np.where(eroded >= 0)[0])
# cnt2 = len(np.where(eroded == 0)[0])
# cnt3 = len(np.where(eroded == 255)[0])
# #print("all:{} bg:{} fg:{}".format(cnt1, cnt2, cnt3))
# assert(cnt1 == cnt2 + cnt3)
res = dilated.copy(
)#res[((dilated == 255) & (msk == 0))] = 128
res[
((dilated ==
255)
&(eroded ==0)
)]=128
# alpha
return res
更新
from scipy.ndimage import morphology
defgettrimap
(self, alpha)
: fg = np.array(np.equal(alpha,
255)
.astype(np.float32)
) unknown = np.array(np.not_equal(alpha,0)
.astype(np.float32)
)# unknown = alpha > 0
unknown = unknown - fg
unknown = morphology.distance_transform_edt(unknown ==0)
<= np.random.randint(1,
20)trimap = fg
trimap[unknown]
=0.5
# print(trimap[:, :, :1].shape)
return trimap[:,
:,:1
]
二次更新:
from scipy.ndimage import grey_dilation, grey_erosion
low, high =2,
8### 根據具體情況再定義
d_size = np.random.randint(low=low, high=high)
e_size = np.random.randint(low=low, high=high)
matte = label /
255.
# $numpy array of your matte(with values between[0, 1])$
trimap =
(matte >=
0.9)
.astype(
'float32'
)not_bg =
(matte >0)
.astype(
'float32'
)trimap[np.where(
(grey_dilation(not_bg, size=
(d_size, d_size)
)- grey_erosion(trimap, size=
(e_size, e_size)))
!=0)]
=0.5
python - opencv 之影象形態學(膨脹與腐蝕) 遞推之生兔子,生小牛問題
time limit 1000ms memory limit 65536kb submit statistic problem description 一對成熟的兔子每天能且只能產下一對小兔子,每次都生一公一母,每只小兔子的成熟期是1天,小兔子出生後隔一天才能再生小兔子。第一天某人領養了一對成熟的兔...
pytest之allure pytest生成報告
mac環境相關 jdk1.8 python 3.7版本 pytest 6.1.2版本 allure pytest 2.8.29 最新版 如果報錯 attributeerror module allure has no attribute severity level 這個是之前 pytest all...
Mysql之wait timeout引數生效辦法
wait timeout的作用是,設定非互動連線 就是指那些連線池方式 非客戶端方式連線的 的超時時間,預設是28800,就是8小時,超過這個時間,mysql伺服器會主動切斷那些已經連線的,但是狀態是sleep的連線。同時設定 interactive timeout 300 這個wait timeo...