This article is reproduced from OpenCV python tutorial (1, image loading, display and save) author Daetalus
This is the first OPENCV 2 computer Vision Application Programming Cookbook reading notes. The code for each chapter will be rewritten in the notes in the Python language.
The configuration of the PYTHONOPENCV is not introduced here.
Note that the OPENCV for Python is now bound through NumPy. So in the use of some numpy must master some of the relevant knowledge!
The image is a matrix, in OpenCV for Python, the image is an array in NumPy!
If you are reading an image first import the OpenCV package by:
Import Cv2
Reading and displaying images
There is no need to declare variables in Python, so there is no need for cv::mat xxxxx in C + +. Just this:
img = Cv2.imread ("D:\cat.jpg"
OPENCV currently supports reading BMP, JPG, PNG, TIFF and other common formats. Please refer to the OPENCV reference documentation for more details.
Then create a window
Cv2.namedwindow ("Image")
Then display the image in the window
Cv2.imshow ("Image"
Finally, add one more sentence:
Cv2.waitkey (0)
If you do not add the last sentence, the execution window in idle is not responding directly. Executed at the command line, it is a flash.
The complete program is:
Import Cv2 = Cv2.imread ("d:\\cat.jpg") Cv2.namedwindow ("Image " ) cv2.imshow ("Image", img) cv2.waitkey (0) Cv2.destroyallwindows ()
Finally releasing the window is a good habit!
Create/Copy Images
There is no CreateImage interface in the new OPENCV interface. That is, there is no cv2. CreateImage such a function. If you want to create an image, you need to use NumPy's function (now using Opencv-python bindings, NumPy is required). As follows:
In the new Opencv-python binding, the image uses the properties of the NumPy array to represent the size and channel information of the image. If the output is img.shape, it will get (500, 375, 3), here is an example of the cat.jpg with OpenCV. The last 3 indicates that this is an RGB image.
You can also copy the original image to get a new image.
If you are not afraid of trouble, you can also use Cvtcolor to obtain a copy of the original image.
emptyimage3=Cv2.cvtcolor (img,cv2. Color_bgr2gray) #emptyimage3[...] =0
Behind the emptyimage3[...] =0 is a black image that turns it into a blank.
Save Image
Saving the image 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. where 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, represented by an integer of 0-100, and defaults to 95. Attention, Cv2. The imwrite_jpeg_quality type is long and must be converted to int. Here are two images stored in different quality:
For PNG, the third parameter represents 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 size is as follows:
There is also a supported image that is generally not commonly used.
The complete code is:
ImportCv2Importnumpy 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[...] =0Cv2.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 ()
Resources:
"OpenCV References Manuel"
"OpenCV 2 computer Vision application Programming Cookbook"
"OpenCV computer Vision with Python"
This article goes from http://blog.csdn.net/sunny2038/article/details/9057415 author Daetalus
OpenCV Python Tutorials (1, loading, displaying, and saving images)