Opencv-python: Image size, image reading, display, saving and copying

Source: Internet
Author: User

Opencv-python: Image size, image reading, display, saving and copyingoriginal November 23, 2017 21:30:49
    • 4440

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
    1. 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
    1. if __name__ = = ' __main__ ':
    2. Dirfile = ' dataset/data/traindata/001.bmp '
    3. img = Cv2.imread (dirfile)
    4. Size = Img.shape
    5. 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
    1. 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
    1. 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
    1. Cv2.imshow ("Image", i)
    2. 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
    1. 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
    1. 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
    1. 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
    1. Import NumPy as NP
[Python]View PlainCopy
    1. 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
    1. Image = Img.copy ()
You can also use Cvtcolor to get a copy of the original image:

[Python]View PlainCopy
    1. Image = Cv2.cvtcolor (img,cv2. Color_bgr2gray)

Opencv-python: Image size, image reading, display, saving and copying

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.