LoginSignup
1
2

More than 3 years have passed since last update.

画像の膨張収縮処理

Last updated at Posted at 2020-08-22

実行環境

Google Colaboratory

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

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

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

import cv2 #opencv
import numpy as np
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.subplot(2,3,1)
plt.title("Original", fontsize=10)
plt.imshow(gray)

kernel = np.ones((3,3),np.uint8)

#膨張 カーネルサイズ領域から輝度の高いものを選ぶ
plt.subplot(2,3,4)
plt.title("dilate", fontsize=10)
dst = cv2.dilate(gray,kernel,iterations = 1) #iterations:膨張回数
plt.imshow(dst)

#収縮 カーネルサイズ領域から輝度の低いものを選ぶ
plt.subplot(2,3,5)
plt.title("erode", fontsize=10)
dst = cv2.erode(gray,kernel,iterations = 1) #iterations:収縮回数
plt.imshow(dst)

plt.show()

結果

image.png

うまく使えば欠損値の補完やノイズ除去に使える

1
2
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
1
2