Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and display OpenCV4.X images

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about OpenCV4.X image reading and display, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

First, read pictures

Using imread () function to read image in opencv

Imread (filename, flags=None)

The path of the      filename picture

     flags image reading mode

● cv2.IMREAD_COLOR: load a color image, and any transparency of the image will be ignored (default).

● cv2.IMREAD_GRAYSCALE: loads the image in grayscale mode.

● cv2.IMREAD_UNCHANGED: loads the image that contains the Alpha channel.

You can also use 1, 0 or-1 instead, in the source code, IMREAD_COLOR = 1 IMREADCEGRAYSCALE = 0 min IMREADENTUNCHANGED =-1

NOTE:

The image data is stored in the order of B and G _ (R). The range of pixel values of the read image: [0255]. When the image path is incorrect, the program does not report an error, and the return value is None. When displaying the operation, it will report: error: (- 215:Assertion failed) size.width > 0 & & size.height > 0 in function 'cv::imshow'. Chinese cannot be included in the picture path, otherwise the error will be the same as 2.

Code

Import cv2

Img = cv2.imread ('test.jpg',1)

Second, display pictures

Use the imshow () function for image display.

     imshow (winname, mat)

The name of the      winname display window

Image data to be displayed by      mat

You need to use some auxiliary functions here. First, you need to create and destroy windows before and after displaying the image, using the functions namedWindow () and destroyAllWindows (), respectively.

NamedWindow (winname, flags=None)

The name of the      winname display window

Flag of the      flags display window

● cv2.WINDOW_AUTOSIZE: automatically adjusts the window according to the displayed image, and cannot manually change the window size (default).

● cv2.WINDOW_NORMAL: windows can be resized.

DestroyAllWindows () destroys all windows created.

Second, you need to use a function waitKey () that waits for keyboard input to ensure that the window is displayed, otherwise the window will flash by.

WaitKey (delay=None)

     delay delays in milliseconds, and 0 refers to the special value of "forever".

NOTE:

If the display fails, you can judge whether the displayed image data is None by print (mat). When the window name is the same as winname, the window displays only the latest image.

Code

Cv2.namedWindow ('image', cv2.WINDOW_NORMAL) # can be omitted

Cv2.imshow ("image", img)

Cv2.waitKey (0)

Cv2.destroyAllWindows ()

Third, save pictures

The picture is saved through the imwrite () function.

Imwrite (filename, img, params=None)

Name of the image saved by      filename

Image data saved by      img

     params for JPEG, it represents the quality of the image, expressed as an integer from 0 to 100, and the default is 95; for png, the third parameter represents the compression level. The default is 3.

Code

Cv2.imwrite ('test.jpg', img)

4. Application 1. Show saved picture

Load a grayscale image, display it, save the image and exit if you press's', and exit directly if you press the ESC key.

Code

Import cv2

Img = cv2.imread ('landscape.jpg', 0)

Cv2.imshow ('image', img)

K = cv2.waitKey (0)

If k = = 27: # wait for ESC key to exit

Cv2.destroyAllWindows ()

Elif k = = ord ('s'): # wait for's' key to save and exit

Cv2.imwrite ('gray.png', img)

Cv2.destroyAllWindows ()

two。 Use Matplotlib

Matplotlib is a drawing library of python, which provides a large number of graphics drawing API. Next, show the image read by opencv, enlarge the image, and save the image through Matplotlib.

Code

Import cv2

From matplotlib import pyplot as plt

Img = cv2.imread ('landscape.jpg', 0)

Plt.imshow (img, cmap = 'gray', interpolation =' bicubic')

Plt.xticks ([]), plt.yticks ([]) # to hide tick values on X and Y axis

Plt.show ()

The above is how OpenCV4.X images are read and displayed, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report