修改影像飽和度問題
2019/11/19 下午 06:33
電腦視覺深度學習討論版
許義翔
觀看數:11
回答數:1
收藏數:2
cvdl-1
cvdl-1-d03
# 為了要改變飽和度,我們先把 color space 轉成 HSL 格式 (OpenCV 表示順序是 HLS,只有 HSL 格式有差異)
img_hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
# 這裡設置 Saturation 的兩成
change_percentage = 0.2
# 針對飽和度的值做改變,超過界線 0~1 的都會 bound
# 在 HLS color space 減 2 成少飽和度
# float32 轉換成百分比(小數點)
img_hls_down = img_hls.astype('float32')
img_hls_down[..., -1] = img_hls_down[..., -1]/255 - change_percentage
img_hls_down[img_hls_down[..., -1] < 0] = 0
img_hls_down[..., -1] = img_hls_down[..., -1]*255
img_hls_down = img_hls_down.astype('uint8')
# 在 HLS color space 增加 2 成飽和度
img_hls_up = img_hls.astype('float32')
img_hls_up[..., -1] = img_hls_up[..., -1]/255 + change_percentage
img_hls_up[img_hls_up[..., -1] > 1] = 1
img_hls_up[..., -1] = img_hls_up[..., -1]*255
img_hls_up = img_hls_up.astype('uint8')
想請教一下,change_percentage 指的是在這個範例中想要取得的值是 Saturation 的兩成,對嗎?
而 img_hls_down[..., -1],其中 -1 指的是什麼?
img_hls_down[img_hls_down[..., -1] < 0] = 0,我也不太能理解這是什麼意思?謝謝!
回答列表
-
2019/11/20 上午 11:16Chen-Ming Yang贊同數:0不贊同數:0留言數:0
Hello,
1. img_hls_down[..., -1]
在 Python 的 list 或是矩陣裏面 -1 代表「最後一個」
在這邊我們知道 img_hls_down 是一個三維矩陣
前面兩個維度代表長跟寬,第3個維度在這邊分別是 H, L, S channel
所以 img_hls_down[..., -1] 這邊應該要看兩件事情
[..., -1] 代表我們 -1 指的是「最後一個維度」的「最後一個 channel」
img_hls_down[..., -1] 代表我們希望拿這個矩陣的 S channel (飽和度)
Note:「...」 的用法在 Day4 會有可以再看看
2. img_hls_down[img_hls_down[..., -1] < 0] = 0
這邊是 img_hls_down[條件],根據裏面設定的條件過濾出想要的值
換句話說,我們透過 img_hls_down[img_hls_down[..., -1] < 0] 找出所有 S channel 的值小於零的,再把他的值設為零,這樣就可以確保 S channel 所有的值都一定是 >= 0
3. change_percentage
因為這邊他的範圍是 0~1
所以的確可以解釋成減少兩成