Python basic tutorial: cv2 and image loading, display and save examples

@This article comes from the public number: csdn2299, like to pay attention to the public number programmer academy
This article is the first article of the OpenCV 2 Computer Vision Application Programming Cookbook reading notes. In the notes, the code of each chapter will be rewritten in Python language.

The configuration of PythonOpenCV will not be introduced here.

Note that now OpenCV for Python is bound via NumPy. So you must master some NumPy related knowledge when using it!

The image is a matrix. In OpenCV for Python, the image is an array in NumPy!

If you want to read the image, you must first import the OpenCV package, the method is:

import cv2

Read and display the image

There is no need to declare variables in Python, so there is no need for cv :: Mat xxxxx in C ++. Just like this:

img = cv2.imread("D:\cat.jpg")

OpenCV currently supports reading common formats such as bmp, jpg, png, and tiff. For more details, please refer to the OpenCV reference document.

Then create a window

cv2.namedWindow("Image")

Then display the image in the window

cv2.imshow("Image", img)

Finally, add a sentence:

cv2.waitKey (0)

If you do not add the last sentence, the execution window in IDLE is directly unresponsive. When executed on the command line, it is a flash.

The complete procedure is:

import cv2 
  
img = cv2.imread("D:\\cat.jpg") 
cv2.namedWindow("Image") 
cv2.imshow("Image", img) 
cv2.waitKey (0) 
cv2.destroyAllWindows()

It is a good habit to finally release the window!

Create / Copy Image

There is no CreateImage interface in the new OpenCV interface. There is no such function as cv2.CreateImage. If you want to create an image, you need to use the numpy function (now using OpenCV-Python binding, numpy is required). as follows:

emptyImage = np.zeros(img.shape, np.uint8)

In the new OpenCV-Python binding, the image uses the attributes of the NumPy array to represent the image size and channel information. If you output img.shape, you will get (500, 375, 3), here is an example of cat.jpg that comes with OpenCV. The last 3 indicates that this is an RGB image.

You can also copy the original image to get a new image.

emptyImage2 = img.copy();

If you are not afraid of trouble, you can also use cvtColor to get a copy of the original image.

emptyImage3=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
#emptyImage3[...]=0

The following emptyImage3 […] = 0 is to convert it into a blank black image.

Save image

Saving images is simple, just use cv2.imwrite.

cv2.imwrite(“D:\cat2.jpg”, img)

The first parameter is the saved path and file name, and the second is the image matrix. Among them, imwrite () has an optional third parameter, as follows:

cv2.imwrite(“D:\cat2.jpg”, img,[int(cv2.IMWRITE_JPEG_QUALITY), 5])

The third parameter is for a specific format: For JPEG, it represents the quality of the image, expressed as an integer from 0-100, the default is 95. Note that the type of cv2.IMWRITE_JPEG_QUALITY is Long and must be converted to int. The following are two pictures stored in different qualities: Insert picture description here
For PNG, the third parameter indicates the compression level. cv2.IMWRITE_PNG_COMPRESSION, from 0 to 9, the higher the compression level, the smaller the image size. The default level is 3:

cv2.imwrite("./cat.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0]) 
cv2.imwrite("./cat2.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

The saved image sizes are as follows: Insert picture description here
There is also a supported image, which is generally not commonly used.

The complete code is:

import cv2 
import numpy as np 
  
img = cv2.imread("./cat.jpg") 
emptyImage = np.zeros(img.shape, np.uint8) 
  
emptyImage2 = img.copy() 
  
emptyImage3=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
#emptyImage3[...]=0 
  
cv2.imshow("EmptyImage", emptyImage) 
cv2.imshow("Image", img) 
cv2.imshow("EmptyImage2", emptyImage2) 
cv2.imshow("EmptyImage3", emptyImage3) 
cv2.imwrite("./cat2.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 5]) 
cv2.imwrite("./cat3.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) 
cv2.imwrite("./cat.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0]) 
cv2.imwrite("./cat2.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9]) 
cv2.waitKey (0) 
cv2.destroyAllWindows()

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] Want to be yourself, want to be a noble person, come on!

Published 50 original articles · 21 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105545096