注意:在这篇文章中,我们将仅使用灰度图像以使其易于理解。
图像可以被认为是值的矩阵,其中每个值代表像素的强度。图像格式主要分为三种类型:
滤镜是用于通过应用某些操作来修改图像的工具。滤波器是一个在图像上移动的矩阵(也称为内核),对其窗口内的像素值执行计算。我们将介绍两种常见类型的滤波器:均值滤波器和中值滤波器。
均值滤波器用于通过对窗口内的像素值进行平均来减少噪声。它将窗口中的中心像素替换为该窗口内所有像素值的平均值。 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()
中值滤波器用于通过将每个像素的值替换为窗口中所有像素的中值来减少噪声。它对于消除椒盐噪声特别有效。 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()
您可以创建自定义过滤器以对图像应用特定操作。 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()
注意:在代码片段中,在分配阈值图像时,您将看到 _ ,图像。这是因为 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()
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()
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()
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()
以上是Python 计算机视觉简介(第 1 部分)的详细内容。更多信息请关注PHP中文网其他相关文章!