0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

閾値処理(スレッショナルド)

Last updated at Posted at 2020-08-19

実行環境

Google Colaboratory

Google Colaboratoryで画像を読み込む為の準備

from google.colab import files
from google.colab import drive
drive.mount('/content/drive')

必要なライブラリの読み込み

import cv2 #opencv
import matplotlib.pyplot as plt 
%matplotlib inline

画像準備

img = plt.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

スレッショナルド処理

plt.figure(figsize=(9, 6), dpi=100,
           facecolor='w', linewidth=0, edgecolor='w')

# オリジナル画像
plt.subplot(2,5,1)
plt.imshow(gray)

# スレッショナルド処理:cv2.THRESH_BINARY #閾値を超えたものを1、他を0にする
plt.subplot(2,5,6)
ret,dst = cv2.threshold(gray,100,200,cv2.THRESH_BINARY)
plt.imshow(dst)

# スレッショナルド処理:cv2.THRESH_BINARY_INV #閾値を超えたものを0、他を1にする
plt.subplot(2,5,7)
ret,dst = cv2.threshold(gray,100,200,cv2.THRESH_BINARY_INV)
plt.imshow(dst)

# スレッショナルド処理:cv2.THRESH_TRUNC #閾値100を超えたものをmax値:200にする
plt.subplot(2,5,8)
ret,dst = cv2.threshold(gray,100,200,cv2.cv2.THRESH_TRUNC)
plt.imshow(dst)

# スレッショナルド処理:cv2.THRESH_TOZERO 閾値以下の輝度を0に変換
plt.subplot(2,5,9)
ret,dst = cv2.threshold(gray,100,200,cv2.THRESH_TOZERO)
plt.imshow(dst)

# スレッショナルド処理:cv2.THRESH_TOZERO_INV 閾値以上の輝度を0に変換
plt.subplot(2,5,10)
ret,dst = cv2.threshold(gray,100,200,cv2.THRESH_TOZERO_INV)
plt.imshow(dst)

plt.show()
plt.gray()

スレッショナルド結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?