Http://blog.csdn.net/sunny2038/article/details/9057415 studied the author's article.
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:
[Python]View Plaincopy
- 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:
[Python]View Plaincopy
- 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
[Python]View Plaincopy
- Cv2.namedwindow ("Image")
Then display the image in the window
[Python]View Plaincopy
- Cv2.imshow ("Image", IMG)
Finally, add one more sentence:
[Python]View Plaincopy
- 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:
[Python]View Plaincopy
- Import Cv2
- img = 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:
[Python]View Plaincopy
- Emptyimage = Np.zeros (Img.shape, Np.uint8)
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.
[Python]View Plaincopy
- EmptyImage2 = Img.copy ();
If you are not afraid of trouble, you can also use Cvtcolor to obtain a copy of the original image.
[Python]View Plaincopy
- 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:
[Python]View Plaincopy
- 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:
[Python]View Plaincopy
- 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) )
- 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"
A first example