当前位置:网站首页>Digital image processing: graying
Digital image processing: graying
2022-06-10 16:23:00 【woshicver】

Have you ever thought about how the methods in image editing software can change the appearance of images ? Or are you looking for a simple way to use grayscale images ?
This paper will focus on the basic knowledge of digital image processing , And introduce a Python Can be used for image graying method .
What is digital image processing ?
Digital image processing refers to the use of digital computers to process images .
for example , Digital image processing can be used to change the brightness or contrast of an image , Or scan the image to find a specific pattern , For example, face or object . A digital image is composed of pixels containing numbers representing the intensity of the image .
for example , chart 1 Displays a table that represents the data in the image .y The length of depends on the height of the image ,x The length of depends on the width of the image . The range of numbers used to specify the intensity depends on the number of bits it can store ,8 Bit images can be stored 256 Color , and 1 Bit images can be stored 2 Color .
| 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 |
What is a grayscale image ?
A grayscale image is an image composed of grayscale shadows . It can store 8 position , Intensity is the contrast between black and white . chart 2 Shows an example of converting an image to grayscale .

chart 2: Visualization of the conversion from the original version to the grayscale version .
Program
The height and width of the loop image .
Calculate the intensity of each pixel by photometry :
Strength = 0.299×r + 0.587×g + 0.114×b
Set the pixel to the new intensity .
Python Method
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] = valueCode instructions
def rgb_to_grayscale(image):Defines an acceptance named image The parameter method of .for y in range(image.shape[0]):The height of the loop imagefor x in range(image.shape[1]):The width of the loop imagevalue = (0.299*image[y,x,0])+(0.587*image[y,x,1])+(0.114*image[y,x,2])Calculate new strengthimage[y, x] = valueSet the pixel to the new intensity
How to use
There are many ways to load images into Python in , The following example uses OpenCV.
To import OpenCV, Must install OpenCV-Python library . Enter the following command in the terminal to install the Library :
$ pip install opencv-pythonOpenCV Is a library for computer vision and machine learning , Therefore, it can also be used to gray the image , However, this paper focuses on the practical methods for grayscale , Therefore, the library is only used to load 、 Resize and display images .
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)Code instructions
import cv2: Import OpenCVimage = cv2.imread("<path-to-image.png">): Load the image as BGRimage = cv2.resize(image, (256, 256), interpolation = cv2.INTER_AREA): Resize imageimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB): Take the image from BGR Convert to RGBrgb_to_grayscale(image): Use the previously defined grayscale method to grayscale the imagecv2.imshow(image): Display images
summary
Digital image processing is used to process images , for example , Scan objects or implement different types of filters . The number of bits an image can store specifies the number of colors it can display . A grayscale image is an image converted to grayscale . The brightness method can be used to gray the image .OpenCV Can be used to load 、 Resizing and converting the image's color space , And many other operations related to image processing .
Reference resources
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 *
If you see this , Show that you like this article , Please forward 、 give the thumbs-up . WeChat search 「uncle_pn」, Welcome to add Xiaobian wechat 「 woshicver」, Update a high-quality blog post in your circle of friends every day .
↓ Scan QR code and add small code ↓

边栏推荐
- 【第六节 函数】
- MapReduce案例之聚合求和
- sm59远程连接,如果提示没有host,则在服务器上加上host,然后重启sap_SAP刘梦
- Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (16)
- Android 13 re upgrade for intent filters security
- 姿态估计之2D人体姿态估计 - Simple Baseline(SBL)
- 【无标题】
- kubernetes 二进制安装(v1.20.16)(五)验证 master 部署
- Scope and closure
- Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
猜你喜欢

RK3308--8声道改成双声道+录音增益

Use of hardware instruments

I used Matlab to reproduce the trembling sonic boom Fire Games Apple snake

袋鼠云数栈基于CBO在Spark SQL优化上的探索

MapReduce之排序及序列化案例的代码实现

Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?

MapReduce之Word Count案例代码实现

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

Solution to some problems of shadow knife RPA learning and meeting Excel
![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]
随机推荐
Interpretation of cube technology | past and present life of cube Rendering Design
STM32 printf garbled
Application scenario introduction of nixie tube driver chip + voice chip, wt588e02b-24ss
MapReduce案例之聚合求和
Add Anaconda's bin directory to path
Specific method of Modbus RTU communication between Velcro touch screen and delta inverter (picture and text)
[MySQL basics]
Many MapReduce cases seek common friends in the map stage
torch. nn. utils. rnn. pad_ Detailed explanation of sequence() [pytoch getting started manual]
Apple mailbox configures QQ mailbox, 163 mailbox, edu mailbox, Gmail mailbox, and obtains Gmail calendar
Detailed explanation of RGB color space, hue, saturation, brightness and HSV color space
Android 13 re upgrade for intent filters security
Missing setjarbyclass() when running MapReduce task. No class found
Thinking and precipitation after docking with hundreds of third-party APIs
Four Byron poems translated from heartless sword
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
2D human pose estimation for pose estimation - simdr: is 2D Heatmap representation even necessity for human pose estimation?
idea新建项目报错org.codehaus.plexus.component.repository.exception.ComponentLookupException:
STOP在屏幕程序的应用_SAP刘梦_
Unified certification center oauth2 certification pit