iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 20
0
Software Development

【錢不夠買ps的我,只好用OpenCV來修圖了!】系列 第 20

【沒錢ps,我用OpenCV!】Day 20 - 花式修圖2,OpenCV 的各種 Threshold 方法整理,Otsu's Threshold 大津二值化,自動計算最佳閥值,做出最好的黑白效果圖!

先來看看今天的結果圖

比較 OpenCV 的不同 Threshold算法

https://ithelp.ithome.com.tw/upload/images/20201003/20120424a2T8zN6oum.png

Otsu's Threshold 大津二值化

https://ithelp.ithome.com.tw/upload/images/20201002/201204246CI3LLnL4R.png

-> 此篇文章的程式碼 github

Day20-1_大津二值化_Otsu_threshold.ipynb

Day20-2_二值化大全_Threshold_collection.ipynb

前言

花式修圖的這個系列...
主要會講的是一些比進階再更進階的內容,
會有比較多冷門的功能,或是更難的演算法。

Otsu's Threshold 大津二值化 是什麼

Otsu's Threshold 大津二值化,他用來幫助我們解決「手動設定閥值的選值困擾」,
我們透過 Otsu's Threshold 演算法,能夠「自動找出最佳的閥值」。

圖片示例:
(引用自:https://scikit-image.org/docs/0.13.x/auto_examples/xx_applications/plot_thresholding.html)

https://ithelp.ithome.com.tw/upload/images/20201002/201204249fg93JNiXe.png

參考右下角的圖片,我們自動找到了最佳閥值後,再進行二值化。

Otsu's Threshold 大津二值化,自動計算最佳閥值

# 先將圖片轉為灰階
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

# global thresholding
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

# Otsu's thresholding
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

我們看一下效果:

https://ithelp.ithome.com.tw/upload/images/20201002/20120424UC53vugIHo.png

我們可以發現在未做 Otsu's Threshold 的 histogram 中,
0的比例」遠遠比有做 Otsu's Threshold 還高,
從結果圖中我們也能發現這點,
如果我們只是單純要「提取人像」,
很明顯地透過 Otsu's Threshold 給了我們一個更好的答案。

Otsu's Threshold 的算法

重點程式碼也是一行,而有時我們也會先搭配模糊的方法先進行降噪。

ret, th = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

比較 OpenCV 的各種 Threshold算法

這裡就是這幾天文章的總整理啦~~~

【沒錢ps,我用OpenCV!】Day 18 - 進階修圖5,運用 OpenCV 做圖片二值化,產生黑白的圖片吧!cv2.threshold 各種選擇參數大全

【沒錢ps,我用OpenCV!】Day 19 - 花式修圖1,OpenCV 的圖片自適應二值化,產生更好效果的黑白圖片!cv2.adaptiveThreshold

【沒錢ps,我用OpenCV!】Day 20 - 花式修圖2,OpenCV 的 Threshold 方法整理,Otsu's Threshold 大津二值化,自動計算最佳閥值,做出最好的黑白效果圖片!

結果總圖: 大家可以自己比較不同 Threshold算法,選出自己最喜歡的效果哦!

https://ithelp.ithome.com.tw/upload/images/20201003/20120424XkCXmAKvOM.png

Reference

https://zh.wikipedia.org/wiki/%E5%A4%A7%E6%B4%A5%E7%AE%97%E6%B3%95

http://scipy-lectures.org/packages/scikit-image/auto_examples/plot_threshold.html

https://www.bogotobogo.com/python/OpenCV_Python/python_opencv3_Image_Global_Thresholding_Adaptive_Thresholding_Otsus_Binarization_Segmentations.php

https://docs.opencv.org/master/d7/d4d/tutorial_py_thresholding.html

http://gwang-cv.github.io/2017/08/25/python+opencv%E5%9B%BE%E5%83%8F%E4%BA%8C%E5%80%BC%E5%8C%96/

https://scikit-image.org/docs/0.13.x/auto_examples/xx_applications/plot_thresholding.html


上一篇
【沒錢ps,我用OpenCV!】Day 19 - 花式修圖1,OpenCV 的圖片自適應二值化,產生更好效果的黑白圖片!cv2.adaptiveThreshold
下一篇
【沒錢ps,我用OpenCV!】Day 21 - 花式修圖3,OpenCV 也有 photoshop 的魔術棒工具?! 漫水填充法 cv2.floodFill (Magic Wand Tool)
系列文
【錢不夠買ps的我,只好用OpenCV來修圖了!】30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言