Python によるコンピューター ビジョンの概要 (パート 1)

PHPz
リリース: 2024-09-10 06:44:02
オリジナル
709 人が閲覧しました

: この投稿では、わかりやすくするためにグレースケール画像のみを使用します。

画像とは何ですか?

画像は値の行列として考えることができ、各値はピクセルの強度を表します。画像形式には主に 3 つのタイプがあります:

  • バイナリ: この形式の画像は、0 (黒) と 1 (白) の値を持つ単一の 2 次元行列で表されます。これは画像表現の最も単純な形式です。

Introduction To Computer Vision with Python (Part 1)

  • グレースケール: この形式では、画像は 0 ~ 255 の範囲の値を持つ単一の 2 次元行列で表されます。ここで、0 は黒を表し、255 は白を表します。中間値は、さまざまなグレーの色合いを表します。

Introduction To Computer Vision with Python (Part 1)

  • RGB スケール: ここで、画像は 0 から 255 の範囲の値を持つ 3 つの 2 次元行列 (カラー チャネルごとに 1 つずつ、赤、緑、青) で表されます。各行列には、次のピクセル値が含まれています。 1 つのカラー コンポーネントであり、これら 3 つのチャネルを組み合わせるとフルカラー画像が得られます。

Introduction To Computer Vision with Python (Part 1)

フィルター

フィルターは、特定の操作を適用して画像を変更するために使用されるツールです。フィルターは、画像上を移動するマトリックス (カーネルとも呼ばれます) であり、ウィンドウ内のピクセル値の計算を実行します。平均値フィルターと中央値フィルターという 2 つの一般的なタイプのフィルターについて説明します。

平均フィルター

平均フィルターは、ウィンドウ内のピクセル値を平均化することでノイズを低減するために使用されます。ウィンドウ内の中心ピクセルを、そのウィンドウ内のすべてのピクセル値の平均に置き換えます。 cv2.blur() 関数は、カーネル サイズ 3x3 の平均フィルターを適用します。これは、平均を計算するために各ピクセルの周囲のピクセルの 3x3 ウィンドウを考慮することを意味します。これは画像を滑らかにするのに役立ちます。

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

# Applies a Mean Filter of size 3 x 3
blurred_image = cv2.blur(image, (3, 3))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(blurred_image, cmap='gray')
plt.title('Mean Filtered Image')
plt.axis("off")
plt.show()
ログイン後にコピー

Introduction To Computer Vision with Python (Part 1)

メディアンフィルター

メディアン フィルターは、各ピクセルの値をウィンドウ内のすべてのピクセルの中央値で置き換えることによってノイズを低減するために使用されます。特にごま塩ノイズの除去に効果的です。 cv2.medianBlur() 関数は、カーネル サイズ 3 のメディアン フィルターを適用します。このメソッドは、各ピクセルをその近傍のピクセル値の中央値に置き換えます。これは、ノイズを除去しながらエッジを維持するのに役立ちます。ここで、カーネル サイズが大きいほど、画像はよりぼやけます。

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

# Applies a Median Filter with a kernel size of 3
blurred_image = cv2.medianBlur(image, 3)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(blurred_image, cmap='gray')
plt.title('Median Filtered Image')
plt.axis("off")
plt.show()
ログイン後にコピー

Introduction To Computer Vision with Python (Part 1)

カスタムフィルター

カスタム フィルターを作成して、画像に特定の操作を適用できます。 cv2.filter2D() 関数を使用すると、任意のカスタム カーネルをイメージに適用できます。 cv2.filter2D() 関数は、カスタム カーネル (フィルター) をイメージに適用します。カーネルは、ピクセル値に対して実行される操作を定義する行列です。この例では、カーネルは指定された値に基づいて画像の特定の特徴を強化します。

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

# Define a custom filter kernel
kernel = np.array([[2, -1, 5],
                   [-5, 5, -1],
                   [0, -1, 0]])

filtered_image = cv2.filter2D(image, -1, kernel)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(filtered_image, cmap='gray')
plt.title('Filtered Image')
plt.axis('off')

plt.show()
ログイン後にコピー

Introduction To Computer Vision with Python (Part 1)

閾値処理

: コード スニペットでは、しきい値処理されたイメージを割り当てるときに _ , image が表示されます。これは、 cv2.threshold 関数が、使用されたしきい値としきい値処理されたイメージの 2 つの値を返すためです。閾値処理された画像のみが必要なので、_ を使用して閾値を無視します。

閾値処理は、条件に基づいてピクセル値を設定することにより、画像を 2 値画像に変換します。しきい値処理手法にはいくつかの種類があります:

グローバルしきい値

単純なしきい値処理

このメソッドは、画像全体に固定のしきい値を設定します。しきい値を超える値を持つピクセルは最大値 (255) に設定され、それ以下のピクセルは 0 に設定されます。 cv2.threshold() 関数は単純なしきい値処理に使用されます。強度が 127 より大きいピクセルは白 (255) に設定され、強度が 127 以下のピクセルは黒 (0) に設定され、バイナリ イメージが生成されます。

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

_, thresholded_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(thresholded_image, cmap='gray')
plt.title('Thresholded Image')
plt.axis("off")
plt.show()
ログイン後にコピー

Introduction To Computer Vision with Python (Part 1)

Otsu Thresholding

Otsu's method determines the optimal threshold value automatically based on the histogram of the image. This method minimizes intra-class variance and maximizes inter-class variance. By setting the threshold value to 0 and using cv2.THRESH_OTSU, the function automatically calculates the best threshold value to separate the foreground from the background.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

_, otsu_thresholded_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(otsu_thresholded_image, cmap='gray')
plt.title("Otsu's Thresholded Image")
plt.axis("off")
plt.show()
ログイン後にコピー

Introduction To Computer Vision with Python (Part 1)

Adaptive Thresholding

Mean Adaptive Thresholding

In Mean Adaptive Thresholding, the threshold value for each pixel is calculated based on the average of pixel values in a local neighborhood around that pixel. This method adjusts the threshold dynamically across different regions of the image. The cv2.adaptiveThreshold() function calculates the threshold for each pixel based on the mean value of the pixel values in a local 11x11 neighborhood. A constant value of 2 is subtracted from this mean to fine-tune the threshold. This method is effective for images with varying lighting conditions.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

mean_adaptive_thresholded_image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(mean_adaptive_thresholded_image, cmap='gray')
plt.title('Mean Adaptive Thresholded Image')
plt.axis("off")
plt.show()
ログイン後にコピー

Introduction To Computer Vision with Python (Part 1)

Gaussian Adaptive Thresholding

Gaussian Adaptive Thresholding computes the threshold value for each pixel based on a Gaussian-weighted sum of the pixel values in a local neighborhood. This method often provides better results in cases with non-uniform illumination. In Gaussian Adaptive Thresholding, the threshold is determined by a Gaussian-weighted sum of pixel values in an 11x11 neighborhood. The constant value 2 is subtracted from this weighted mean to adjust the threshold. This method is useful for handling images with varying lighting and shadows.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

gaussian_adaptive_thresholded_image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(gaussian_adaptive_thresholded_image, cmap='gray')
plt.title('Gaussian Adaptive Thresholded Image')
plt.axis("off")
plt.show()
ログイン後にコピー

Introduction To Computer Vision with Python (Part 1)

References

  • Encord.com
  • Pyimagesearch.com
  • OpenCV Thresholding
  • OpenCV Filtering

以上がPython によるコンピューター ビジョンの概要 (パート 1)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!