首頁  >  文章  >  後端開發  >  Python 電腦視覺簡介(第 1 部分)

Python 電腦視覺簡介(第 1 部分)

PHPz
PHPz原創
2024-09-10 06:44:02803瀏覽

注意:在這篇文章中,我們將僅使用灰階影像以使其易於理解。

什麼是圖像?

影像可以被認為是值的矩陣,其中每個值代表像素的強度。影像格式主要分為三種:

  • 二進位:此格式的影像由值為 0(黑色)和 1(白色)的單一二維矩陣表示。這是最簡單的圖像表示形式。

Introduction To Computer Vision with Python (Part 1)

  • Grey-Scale:在此格式中,影像由值範圍為 0 到 255 的單一二維矩陣表示;其中 0 代表黑色,255 代表白色。中間值代表不同的灰色深淺。

Introduction To Computer Vision with Python (Part 1)

  • RGB Scale:這裡,影像由三個二維矩陣表示(每個顏色通道一個:紅色、綠色和藍色),值範圍從 0 到 255。每個矩陣包含以下像素值:一個顏色分量,結合這三個通道就可以得到全彩影像。

Introduction To Computer Vision with Python (Part 1)

過濾器

濾鏡是用於透過應用某些操作來修改影像的工具。濾波器是一個在影像上移動的矩陣(也稱為內核),對其視窗內的像素值執行計算。我們將介紹兩種常見類型的濾波器:均值濾波器和中值濾波器。

均值濾波器

均值濾波器用於透過對視窗內的像素值進行平均來減少雜訊。它將視窗中的中心像素替換為該視窗中所有像素值的平均值。 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)

閾值化

注意:在程式碼片段中,在分配閾值影像時,您將看到 _ ,影像。這是因為 cv2.threshold 函數傳回兩個值:使用的閾值和閾值化影像。由於我們只需要閾值影像,因此我們使用 _ 來忽略閾值。

閾值處理透過根據條件設定像素值將影像轉換為二值影像。閾值技術有多種類型:

全域閾值

簡單閾值處理

此方法為整個影像設定一個固定的閾值。值高於閾值的像素設定為最大值 (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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn