当前位置:网站首页>数字图像处理:灰度化
数字图像处理:灰度化
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」,每日朋友圈更新一篇高质量博文。
↓扫描二维码添加小编↓

边栏推荐
- Jerry's ble transmission rate [chapter]
- RK3308--8声道改成双声道+录音增益
- Weilai quarterly report diagram: the revenue was 9.9 billion yuan, a year-on-year increase of 24%, and the operating loss was nearly 2.2 billion yuan
- SQL语言
- Explore the secrets behind the open source data visualization development platform flyfish!
- Software test architects, 16 pieces of advice for future generations, take a look and don't miss it
- Join operation cases in the map phase of MapReduce
- PV operation daily question - Restaurant Service
- Rk3308-- firmware compilation
- Nerf: the popularity of using deep learning to complete 3D rendering tasks
猜你喜欢

sm59远程连接,如果提示没有host,则在服务器上加上host,然后重启sap_SAP刘梦

Methods commonly used in uniapp (part) - timestamp problem and rich text parsing image problem
![leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]](/img/90/79c51f944709ef230f394e874aa9ac.png)
leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]
![[MySQL basics]](/img/fc/48e9c6b739e29472a7a103e47663af.png)
[MySQL basics]

leetcode:730. 统计不同回文子序列【由点及面区间dp + 三维dp + 对角线遍历】

Troubleshooting of cl210openstack operation -- troubleshooting of common core problems

Join operation cases in the map phase of MapReduce

Apache atlas quick start

Online document collaboration tool is the first step to improve work efficiency

Distribution aware coordinate representation for human pose estimation
随机推荐
「技术干货」工业触摸屏之驱动开发及异常分析(连载)
直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!
Scope and closure
Live broadcast preview | deconstruct OLAP! The new multidimensional analysis architecture paradigm is fully open! Apache Doris will bring five big issues!
【对象】。
Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
Use of hardware instruments
Kubernetes binary installation (v1.20.16) (V) verifying master deployment
Pytorch installation tutorial
Smart home (3) competitive product analysis of Intelligent Interaction
51 timer initial value calculation
Guanghetong cooperates with China Mobile, HP, MediaTek and Intel to build 5g fully connected PC pan terminal products
姿态估计之2D人体姿态估计 - Numerical Coordinate Regression with Convolutional Neural Networks(DSNT)
PV operation daily question - orange apple question (advanced version variant)
Android 13 re upgrade for intent filters security
Nerf: the popularity of using deep learning to complete 3D rendering tasks
姿态估计之2D人体姿态估计 - Simple Baseline(SBL)
【无标题】
PV operation daily question - Restaurant Service
RK3308--8声道改成双声道+录音增益