注意:在这篇文章中,我们将仅使用灰度图像以使其易于理解。
什么是图像?
图像可以被认为是值的矩阵,其中每个值代表像素的强度。图像格式主要分为三种类型:
- 二进制:此格式的图像由值为 0(黑色)和 1(白色)的单个二维矩阵表示。这是最简单的图像表示形式。
- Grey-Scale:在此格式中,图像由值范围为 0 到 255 的单个二维矩阵表示;其中 0 代表黑色,255 代表白色。中间值代表不同的灰色深浅。
- RGB Scale:这里,图像由三个二维矩阵表示(每个颜色通道一个:红色、绿色和蓝色),值范围从 0 到 255。每个矩阵包含以下像素值:一个颜色分量,结合这三个通道就可以得到全彩色图像。
过滤器
滤镜是用于通过应用某些操作来修改图像的工具。滤波器是一个在图像上移动的矩阵(也称为内核),对其窗口内的像素值执行计算。我们将介绍两种常见类型的滤波器:均值滤波器和中值滤波器。
均值滤波器
均值滤波器用于通过对窗口内的像素值进行平均来减少噪声。它将窗口中的中心像素替换为该窗口内所有像素值的平均值。 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 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()
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()
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()
References
- Encord.com
- Pyimagesearch.com
- OpenCV Thresholding
- OpenCV Filtering
以上是Python 计算机视觉简介(第 1 部分)的详细内容。更多信息请关注PHP中文网其他相关文章!

文章讨论了由于语法歧义而导致的Python中元组理解的不可能。建议使用tuple()与发电机表达式使用tuple()有效地创建元组。(159个字符)

本文解释了Python中的模块和包装,它们的差异和用法。模块是单个文件,而软件包是带有__init__.py文件的目录,在层次上组织相关模块。

文章讨论了Python中的Docstrings,其用法和收益。主要问题:Docstrings对于代码文档和可访问性的重要性。

本文讨论了Python中的“ Pass”语句,该语句是函数和类等代码结构中用作占位符的空操作,允许在没有语法错误的情况下实现将来实现。

文章在Python中讨论 /和//运营商: / for for True Division,//用于地板部门。主要问题是了解它们的差异和用例。Character数量:158


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

记事本++7.3.1
好用且免费的代码编辑器

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Atom编辑器mac版下载
最流行的的开源编辑器