ホームページ  >  記事  >  バックエンド開発  >  Python+OpenCVでのモルフォロジーの演算方法とは

Python+OpenCVでのモルフォロジーの演算方法とは

WBOY
WBOY転載
2023-05-28 15:16:061367ブラウズ

1. 腐食と拡張

1.1 腐食操作

import cv2
import numpy as np

img = cv2.imread('DataPreprocessing/img/dige.png')

cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

dige.png 元の画像 1 表示 (注: 元の画像がない場合は、スクリーンショットを撮って、ローカルに保存します。

Python+OpenCVでのモルフォロジーの演算方法とは

1 ラウンドの腐食後~ (反復 = 1)

kernel = np.ones((3, 3), np.uint8)
erosion = cv2.erode(img, kernel, iterations=1)

cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()

腐食結果の表示画像 2:

Python+OpenCVでのモルフォロジーの演算方法とは

円を複数回腐食する効果と腐食の原理

pie = cv2.imread('DataPreprocessing/img/pie.png')

cv2.imshow('pie', pie)
cv2.waitKey(0)
cv2.destroyAllWindows()

pie.png元画像 3:

Python+OpenCVでのモルフォロジーの演算方法とは

##腐食原理。フィルターのサイズが大きくなるほど、腐食の度合いも大きくなります。

図 4:

Python+OpenCVでのモルフォロジーの演算方法とは

kernel = np.ones((30, 30), np.uint8)
erosion_1 = cv2.erode(pie, kernel, iterations=1)
erosion_2 = cv2.erode(pie, kernel, iterations=2)
erosion_3 = cv2.erode(pie, kernel, iterations=3)
res = np.hstack((erosion_1, erosion_2, erosion_3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

3 つの円形腐食結果の表示

図 5:

Python+OpenCVでのモルフォロジーの演算方法とは

1.2 拡張操作

kernel = np.ones((3, 3), np.uint8)
dige_dilate = erosion
dige_dilate = cv2.dilate(erosion, kernel, iterations=1)

cv2.imshow('dilate', dige_dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()

拡張前 (図 2)線が太くなり、元の画像とほぼ同じになっていることがわかりましたが、消えていました。あの長いひげのノイズ、

図 6:

Python+OpenCVでのモルフォロジーの演算方法とは

円を複数回拡張すると、拡張原理が腐食とは逆になり、白ドット フィルタリングを使用すると、フィルタ内のすべてのデータが白になります。

pie = cv2.imread('DataPreprocessing/img/pie.png')

kernel = np.ones((30, 30), np.uint8)
dilate_1 = cv2.dilate(pie, kernel, iterations=1)
dilate_2 = cv2.dilate(pie, kernel, iterations=2)
dilate_3 = cv2.dilate(pie, kernel, iterations=3)
res = np.hstack((dilate_1, dilate_2, dilate_3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

拡張の結果3 回円を描きます。図 7:

Python+OpenCVでのモルフォロジーの演算方法とは

## 2. 開く動作と閉じる動作

2.1 開く動作

# 开:先腐蚀,再膨胀
img = cv2.imread('DataPreprocessing/img/dige.png')

kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

cv2.imshow('opening', opening)
cv2.waitKey(0)
cv2.destroyAllWindows()

最初に元のピクチャ 1 を腐食し、次にそれを拡張してオープニング操作の結果を取得します。

図 8:

Python+OpenCVでのモルフォロジーの演算方法とは

2.2 クローズド操作

# 闭:先膨胀,再腐蚀
img = cv2.imread('DataPreprocessing/img/dige.png')

kernel = np.ones((5, 5), np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

cv2.imshow('closing', closing)
cv2.waitKey(0)
cv2.destroyAllWindows()

最初に元の画像 1 を展開し、次に腐食して、オープン操作の結果を取得します

図 9:

Python+OpenCVでのモルフォロジーの演算方法とは

3. グラデーション操作

元の画像 3 の円を 5 回拡大、5 回侵食し、減算して輪郭を取得します。

# 梯度=膨胀-腐蚀
pie = cv2.imread('DataPreprocessing/img/pie.png')
kernel = np.ones((7, 7), np.uint8)
dilate = cv2.dilate(pie, kernel, iterations=5)
erosion = cv2.erode(pie, kernel, iterations=5)

res = np.hstack((dilate, erosion))

cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)

cv2.imshow('gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()

勾配演算結果を取得します

図 10:

Python+OpenCVでのモルフォロジーの演算方法とは

Python+OpenCVでのモルフォロジーの演算方法とは##4. シルクハットとブラックハット

4.1 シルクハット

#シルクハット=元の入力オープン操作の結果

# 礼帽
img = cv2.imread('DataPreprocessing/img/dige.png')
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('tophat', tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()
トップハット結果の取得

図 11:

Python+OpenCVでのモルフォロジーの演算方法とは4.2 Black Hat

Black Hat = クローズド操作 - 元の入力

# 黑帽
img = cv2.imread('DataPreprocessing/img/dige.png')
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('blackhat ', blackhat)
cv2.waitKey(0)
cv2.destroyAllWindows()
トップハット結果の取得

図 12:

以上がPython+OpenCVでのモルフォロジーの演算方法とはの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。