You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
912 B
28 lines
912 B
# 実験課題3
|
|
# 2値化
|
|
|
|
import cv2
|
|
|
|
# 画像の読み込み(3.2と同じ画像を使用)
|
|
image_path = 'cat.jfif'
|
|
|
|
# 第2引数に 0 (cv2.IMREAD_GRAYSCALE) を指定し、最初からグレースケールとして読み込む
|
|
gray_img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
|
|
|
# --- 2値化処理 ---
|
|
# 閾値(自身の画像に合わせて0〜255の間で適切な値に調整してください)
|
|
threshold_value = 127
|
|
# 閾値を超えた画素に割り当てる値(白 = 255)
|
|
max_value = 255
|
|
|
|
# 2値化を実行
|
|
# ret には使用された閾値が、binary_img には2値化後の画像が代入される
|
|
ret, binary_img = cv2.threshold(gray_img, threshold_value, max_value, cv2.THRESH_BINARY)
|
|
|
|
# --- 結果の表示 ---
|
|
cv2.imshow('Original Grayscale Image', gray_img)
|
|
cv2.imshow('Binarized Image (Threshold: 127)', binary_img)
|
|
|
|
# キー入力待ち
|
|
cv2.waitKey(0)
|
|
cv2.destroyAllWindows() |