今日は、画像の閾値処理とは何か、そしてどのようにpythonプログラミング言語で同じものを実装するかを理解しようと思います。
さっそくその概念に触れてみましょう。
こちらもご覧ください。
Pythonを使った画像のエッジ検出。
画像の閾値処理とは?
閾値処理とは、画像を前景と背景の2つに分割する処理です。
「前景 “と “背景 “の2つの部分に分割するプロセスであると定義されています。
これは、様々な画像処理タスクで使用され、より高度な画像認識やセグメンテーションなどを可能にします。
様々な種類の閾値処理技術
以下のような様々な閾値技術を実装することができます。
S.No. | 閾値処理技術名|機能名|説明|||1|バイナリ閾値処理 | ||
1|バイナリ閾値処理|cv2.THRESHEE_BINARY|1.(ピクセル強度) > set threshold : 255 (white)2.Else set to 0 (black). | |||
2 | Binary Inverted Thresholding | cv2.THRESH_BINARY_INV | cv2.THRESH_BINARY の逆のケース. |
3 | Tozero Thresholding | cv2.THRESHG_TOZERO | 1. (ピクセル強度) < しきい値 : 0 (黒)に設定する 2. それ以外の場合は白に設定|4|Tozero Inverted |
4 | Tozero Inverted Thresholding | cv2.THRESH_TOZERO_INV | cv2.THRESH_TOZERO と逆の場合. |
5 | Truncated Thresholding | cv2.THRESHG_TRUNC | 1. ( ピクセル強度 ) > しきい値.閾値まで切り捨て.2. 画素値は,閾値と同じになるように設定されます.3. それ以外の値はそのまま。 |
さまざまな閾値処理技術
こちらもお読みください。
フルコード
閾値メソッドは実装のための直接的な関数を持っているので、閾値メソッドのコード実装を直接見ることができます。
同じようにコーディング実装を理解していただければと思います。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import cv2
import numpy as np
img = cv2.imread( 'lori.jpg' )
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh_hold = cv2.threshold(img, 100 , 255 , cv2.THRESH_BINARY)
ret, thresh_hold1 = cv2.threshold(img, 100 , 255 , cv2.THRESH_BINARY_INV)
ret, thresh_hold2 = cv2.threshold(img, 100 , 255 , cv2.THRESH_TOZERO)
ret, thresh_hold3 = cv2.threshold(img, 100 , 255 , cv2.THRESH_TOZERO_INV)
ret, thresh_hold4 = cv2.threshold(img, 100 , 255 , cv2.THRESH_TRUNC)
thresh_hold = cv2.resize(thresh_hold, ( 960 , 540 ))
cv2.imshow( 'Binary Threshold Image' , thresh_hold)
thresh_hold1 = cv2.resize(thresh_hold1, ( 960 , 540 ))
cv2.imshow( 'Binary Threshold Inverted Image' , thresh_hold1)
thresh_hold2 = cv2.resize(thresh_hold2, ( 960 , 540 ))
cv2.imshow( 'Threshold Tozero Image' , thresh_hold2)
thresh_hold3 = cv2.resize(thresh_hold3, ( 960 , 540 ))
cv2.imshow( 'ThresholdTozero Inverted output' , thresh_hold3)
thresh_hold4 = cv2.resize(thresh_hold4, ( 960 , 540 ))
cv2.imshow( 'Truncated Threshold output' , thresh_hold4)
if cv2.waitKey( 0 ) & 0xff = = 25 :
cv2.destroyAllWindows()
|
サンプル出力 – 1
1. オリジナル画像出力

2. 2値閾値画像出力

3. 2値化逆しきい値画像出力

4. しきい値トゼロ出力

5. しきい値トゼロ反転出力

6. しきい値切り捨て画像出力

サンプル出力 – 2

まとめ
最後に、皆さんもいろいろな画像を自分で閾値処理してみて、いろいろな画像でどのような出力が得られるか見てみてください。
お読みいただきありがとうございました。