当前位置:网站首页>Digital image processing: graying

Digital image processing: graying

2022-06-10 16:23:00 woshicver

d305f0b945f9c6d0192485a9957dfea5.png

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 .

x0123->
y2243432212
12243432212
22243432212
32243432212
v2243432212

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 .

332f9175be7d619e15bc77ce7c15e7fb.gif

chart 2: Visualization of the conversion from the original version to the grayscale version .

Program

  1. The height and width of the loop image .

  2. Calculate the intensity of each pixel by photometry :

    Strength = 0.299×r + 0.587×g + 0.114×b

  3. 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] = value

Code 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 image

  • for x in range(image.shape[1]): The width of the loop image

  • value = (0.299*image[y,x,0])+(0.587*image[y,x,1])+(0.114*image[y,x,2]) Calculate new strength

  • image[y, x] = value Set 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-python

OpenCV 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 OpenCV

  • image = cv2.imread("<path-to-image.png">): Load the image as BGR

  • image = cv2.resize(image, (256, 256), interpolation = cv2.INTER_AREA): Resize image

  • image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB): Take the image from BGR Convert to RGB

  • rgb_to_grayscale(image): Use the previously defined grayscale method to grayscale the image

  • cv2.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 ↓

2c2491a929bf0b2cede1844a56e69d6b.png

原网站

版权声明
本文为[woshicver]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101542173232.html