1. POIエリア:興味のあるエリア
2. エッジパディング
3. 数値演算
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
image1 = mpimg.imread('1.jpg')
image2 = mpimg.imread('2.jpg')
plt.imshow(image1)
plt.imshow(image2)
画像追加
image3 = image1+image2
plt.imshow(image3)
画像のトリミング
plt.imshow(image1[100:-100, 100:-100, :]);
画像のスケーリング
fx と fy はズーム率を表します
image4 = cv2.resize(image1, (0, 0), fx=3, fy=1)
plt.imshow(image4);
4. 画像の閾値処理
ret、dst = cv2.threshold(src、thresh、maxval、type)
- src: 入力画像、単一チャンネル画像のみ
- dst: 出力マップ
- thresh: しきい値
- maxval: ピクセル値が閾値を超えるか、閾値未満の場合の値
- type: 2値化のタイプ
- cv2.THRESH_BINARY は、しきい値を超えた場合は maxval を受け取り、それ以外の場合は 0 を受け取ります。
- cv2.THRESH_BINARY_INV THRESH_BINARY の反転
- cv2.THRESH_TRUNC はしきい値設定のしきい値より大きく、残りは変更されません。
- cv2.THRESH_TOZERO は変更されずにしきい値より大きく、残りは 0 に設定されます
- cv2.THRESH_TOZERO_INV THRESH_TOZERO の反転
image1_gray = image1[:,:,0]
res, thresh1 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_BINARY)
res, thresh2 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_BINARY_INV)
res, thresh3 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TRUNC)
res, thresh4 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TOZERO)
res, thresh5 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TOZERO_INV)
titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [image1_gray, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2,3,i+1)
plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()