当前位置:网站首页>数字图像处理:灰度化
数字图像处理:灰度化
2022-06-10 15:42:00 【woshicver】

你有没有想过图像编辑软件中的方法是如何改变图像外观的?或者你是否正在寻找一种可用于灰度图像的简单方法?
本文将重点介绍数字图像处理的基础知识,并介绍一种 Python 中可用于对图像进行灰度化的方法。
什么是数字图像处理?
数字图像处理是指使用数字计算机处理图像。
例如,数字图像处理可用于改变图像的亮度或对比度,或扫描图像以查找特定图案,例如面部或物体。数字图像由包含表示图像强度的数字的像素组成。
例如,图 1 显示了一个表示图像中数据的表格。y 的长度取决于图像的高度,x 的长度取决于图像的宽度。用于指定强度的数字范围取决于它可以存储的位数,8 位图像可以存储 256 种颜色,而 1 位图像可以存储 2 种颜色。
| x | 0 | 1 | 2 | 3 | -> |
|---|---|---|---|---|---|
| y | 22 | 43 | 43 | 22 | 12 |
| 1 | 22 | 43 | 43 | 22 | 12 |
| 2 | 22 | 43 | 43 | 22 | 12 |
| 3 | 22 | 43 | 43 | 22 | 12 |
| v | 22 | 43 | 43 | 22 | 12 |
什么是灰度图像?
灰度图像是由灰度阴影组成的图像。它可以存储 8 位,强度是黑白之间的对比。图 2 显示了将图像转换为灰度的示例。

图 2:从原始版本到灰度版本的转换的可视化。
程序
循环图像的高度和宽度。
通过光度法计算每个像素的强度:
强度 = 0.299×r + 0.587×g + 0.114×b
将像素设置为新的强度。
Python 方法
def rgb_to_grayscale(image):
for y in range(image.shape[0]):
for x in range(image.shape[1]):
value = (0.299 * image[y, x, 0]) + (0.587 * image[y, x, 1]) + (0.114 * image[y, x, 2])
image[y, x] = value代码说明
def rgb_to_grayscale(image):定义了一个接受名为 image 的参数的方法。for y in range(image.shape[0]):循环图像的高度for x in range(image.shape[1]):循环图像的宽度value = (0.299*image[y,x,0])+(0.587*image[y,x,1])+(0.114*image[y,x,2])计算新的强度image[y, x] = value将像素设置为新的强度
如何使用方法
有很多方法可以将图像加载到 Python 中,下面的示例使用 OpenCV。
要导入 OpenCV,必须安装 OpenCV-Python库。在终端中输入以下命令来安装库:
$ pip install opencv-pythonOpenCV 是一个用于计算机视觉和机器学习的库,因此它也可以用于对图像进行灰度化,但由于本文关注的是用于灰度化的实际方法,因此该库仅用于加载、调整大小和显示图像。
import cv2
def rgb_to_grayscale(image):
# ...
# Use cv2 to load and resize an image
image = cv2.imread("<path-to-image.png>")
image = cv2.resize(image, (256, 256), interpolation = cv2.INTER_AREA)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Convert the image to grayscale
rgb_to_grayscale(image)
# Show image
cv2.imshow(image)代码说明
import cv2:导入 OpenCVimage = cv2.imread("<path-to-image.png">):将图像加载为 BGRimage = cv2.resize(image, (256, 256), interpolation = cv2.INTER_AREA):调整图像大小image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB):将图像从 BGR 转换为 RGBrgb_to_grayscale(image):使用前面定义的灰度方法对图像进行灰度化cv2.imshow(image):显示图像
总结
数字图像处理用于处理图像,例如,扫描对象或实现不同类型的过滤器。图像可以存储的位数指定了它可以显示的颜色数。灰度图像是转换为灰度的图像。亮度方法可用于对图像进行灰度化。OpenCV 可用于加载、调整大小和转换图像的色彩空间,以及许多其他与图像处理相关的操作。
参考
En.wikipedia.org. 2022a. Grayscale — Wikipedia. [online] Available at: <https://en.wikipedia.org/wiki/Grayscale> [Accessed 23 April 2022].
En.wikipedia.org. 2022b. Digital image processing — Wikipedia. [online] Available at: <https://en.wikipedia.org/wiki/Digital_image_processing> [Accessed 23 April 2022].
En.wikipedia.org. 2022c. Digital image — Wikipedia. [online] Available at: <https://en.wikipedia.org/wiki/Digital_image> [Accessed 24 April 2022].
En.wikipedia.org. 2022d. Color depth — Wikipedia. [online] Available at: <https://en.wikipedia.org/wiki/Color_depth> [Accessed 24 April 2022].
Opencv.org. 2022. About — OpenCV. [online] Available at: <https://opencv.org/about/> [Accessed 24 April 2022].
* END *
如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 woshicver」,每日朋友圈更新一篇高质量博文。
↓扫描二维码添加小编↓

边栏推荐
- Detailed explanation of RGB color space, hue, saturation, brightness and HSV color space
- 硬件仪器的使用
- Nerf: the popularity of using deep learning to complete 3D rendering tasks
- [section 7 function]
- Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (16)
- 直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!
- Missing setjarbyclass() when running MapReduce task. No class found
- MapReduce之排序及序列化案例的代码实现
- Duyuan outdoor sprint to Shenzhen Stock Exchange: the annual revenue is 350million, and the color of Lin Xizhen family is obvious
- [untitled] audio Bluetooth voice chip, wt2605c-32n real-time recording upload technical scheme introduction
猜你喜欢

Anti "internal roll", it is said that 360 enterprise security cloud will launch the "one click forced off duty" function, and the computer will automatically close the office software

Save a window with a specific size, resolution, or background color
![Jerry's interface for obtaining ble broadcast package and profile data [chapter]](/img/ae/70cde8684fa536c37387ce819a36eb.png)
Jerry's interface for obtaining ble broadcast package and profile data [chapter]
![Jerry's ble dynamic power regulation [chapter]](/img/29/22be6dca25c4e6502f076fee73dd44.png)
Jerry's ble dynamic power regulation [chapter]

Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (XVII)

姿态估计之2D人体姿态估计 - Distribution Aware Coordinate Representation for Human Pose Estimation【转-修改】

Implementation of word count case code in MapReduce

Solution to some problems of shadow knife RPA learning and meeting Excel

Anba cv2fs/cv22fs obtained ASIL C chip function safety certification, surpassing the level of similar chips in the market

Duyuan outdoor sprint to Shenzhen Stock Exchange: the annual revenue is 350million, and the color of Lin Xizhen family is obvious
随机推荐
Join operation cases in the reduce phase of MapReduce
idea新建项目报错org.codehaus.plexus.component.repository.exception.ComponentLookupException:
RK3308--8声道改成双声道+录音增益
Jerry's interface for obtaining ble broadcast package and profile data [chapter]
Unified certification center oauth2 certification pit
torch.utils.data.DataLoader()详解【Pytorch入门手册】
Jerry's ble PB2 cannot be hardware grounded or connected to triode base [chapter]
Aggregate sum of MapReduce cases
json. Load (s) and json dump(s)
Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (16)
2D human pose estimation for pose estimation - simdr: is 2D Heatmap representation even necessity for human pose estimation?
安霸CV2FS/CV22FS获得ASIL C芯片功能安全认证,超越市场同类芯片水平
Kubernetes binary installation (v1.20.16) (V) verifying master deployment
MapReduce案例之多Map阶段求共同好友
This and object prototypes
Jerry's long press reset and high level reset [chapter]
I used Matlab to reproduce the trembling sonic boom Fire Games Apple snake
Recommend an easy-to-use designer navigation website
Conversion between localdate and date
MapReduce之分区案例的代码实现