Opencv-python: Image size, image reading, display, saving and copyingoriginal November 23, 2017 21:30:49
When you use the OpenCV method, you must first import the OPENCV package. The new OpenCV is imported into the CV2, and here is a comparison with the CV
[Python]View PlainCopy
- Import Cv2
First, the image size
The size of the image can be obtained by its Shape property, shape returns a tuple tuple, the first element represents the height of the image, the second represents the width of the image, and the third represents the number of channels of the pixel.
Example:
[Python]View PlainCopy
- if __name__ = = ' __main__ ':
- Dirfile = ' dataset/data/traindata/001.bmp '
- img = Cv2.imread (dirfile)
- Size = Img.shape
- Print size
Operation Result: (800,645,3)
In CV, it is through size = Cv2. GetSize (i) of the GetSize () function to obtain the
Second, read the image
In Python do not need to declare variables, know the exact location of the image can be read directly through Imread (), currently OPENCV support to read BMP, JPG, PNG and other commonly used formats, more detailed information refer to OPENCV reference documentation. Read:
[Python]View PlainCopy
- Image = Cv2.imread (' f:/001.nmp ')
CV corresponds to the method is Grey_image = Cv2. CreateImage (Size, 8, 1)
Third, display the image
First create a new window to display the image:
[Python]View PlainCopy
- Cv2.namedwindow (' showimage ')
CV-corresponding method is CV. Namedwindow ("Shape Model", CV.) Cv_window_autosize)
Then display the image in the window:
[Python]View PlainCopy
- Cv2.imshow ("Image", i)
- Cv2.waitkey (0)
If you do not add Cv2.waitkey (0), your execution window in idle does not respond directly, and it will blink when executed at the command line. The last sentence, plus cv2.destroyallwindows (), will release the window.
CV in CVS. ShowImage ("image", i) displays the image, CV. Waitkey ()
Iv. Saving Images
[Python]View PlainCopy
- Cv2.imwrite (F:/images ', Image,[int (cv2. imwrite_jpeg_quality),5]) three parameters corresponding to the saved path and file name, image matrix, the specified format (for JPEG, which represents the quality of the image,
[Python]View PlainCopy
- represented by an integer of 0-100, which defaults to 95. Attention, Cv2. The imwrite_jpeg_quality type is long and must be converted to int; for PNG, the third parameter represents the compression level. Cv2. Imwrite_png_compression,
[Python]View PlainCopy
- From 0 to 9, the higher the compression level, the smaller the image size. This is an optional parameter)
V. Copying images
CV can be used directly in the original OPENCV. CreateImage () creates an image, but Cv2 needs to use the NumPy function.
[Python]View PlainCopy
- Import NumPy as NP
[Python]View PlainCopy
- Image = Np.zeros (Img.shape, Np.uint8)
The image uses the properties of the NumPy array to represent the size of the image and the channel information.
Of course, you can also copy the original image to a new image directly:
[Python]View PlainCopy
- Image = Img.copy ()
You can also use Cvtcolor to get a copy of the original image:
[Python]View PlainCopy
- Image = Cv2.cvtcolor (img,cv2. Color_bgr2gray)
Opencv-python: Image size, image reading, display, saving and copying