OpenCV Learning 02--image reading and output under Python

Source: Internet
Author: User

  OPENCV provides a number of functions for image manipulation, the most basic of which is the reading and output of the picture.

First, read the picture

Using OpenCV to read a picture is very easy, just need to use the imread () function, we enter Ipython, enter Help (Cv2.imread) Get the document of the function, get:

Imread (...) Imread (filename[, Flags]), retval

Visible, Imread needs to provide two parameters, the first is the path of the picture, the second is the picture reading mode (flags), the function returns a matrix that stores the image pixel data.

The flags have a value of three:

    • Cv2. Imread_color loading color images, transparency will be discarded
    • Cv2. Imread_grayscale Grayscale Mode
    • Cv2. imread_unchanged full loading of a picture, including alpha channel values (used to represent transparency)

These three flags can also be replaced with 1,0,-1, respectively.

img = Cv2.imread ("test.jpg"= Cv2.imread ("test.jpg" , 0) # these two are equivalent.

Now, we have obtained the image test.jpg data, stored in the IMG. IMG is actually an array of NumPy, which contains the data for each pixel (in the case of a color pattern, the BGR value is included, and grayscale is the grayscale value). We can access the data of each pixel by subscript and make changes to each pixel point.

Second, display the picture in the window

  After loading the image, we can use the imshow () function to show the loaded picture, and again, we use Help (cv2.imshow) to see how the function is used:

imshow (...)     -None

Imshow requires two parameters, the first is the name of the window (Winname), the second is the picture's Pixel matrix (MAT), and the function has no return value. Imshow () Creates a window and displays the picture in that window, and we don't have to worry about whether the picture is colored or gray, and OpenCV automatically deduces the pattern of the image data. Let's now show the images stored in IMG:

Cv2.imshow ("test", img) cv2.waitkey (0)            #  wait for the user to press the key Cv2.destroyallwindows ()   #  close all Windows

Display Effect: Original:

We can see that the original transparent pixels have become black, because the transparency information is discarded in grayscale mode, the original transparent pixel value becomes 0, it becomes black (255 is white)

Three, the output picture

  We got a gray picture, what should we do if we want to save it? At this point you need to use the imwrite () function, Ipython input help (Cv2.imwrite), get:

imwrite (...) Imwrite (filename, img[, params]) , retval

FileName is the name of the output image, IMG is the image to be exported, and the params is the parameter of the picture format, and returns True if the picture is successfully written, otherwise false.

We save this grayscale image as Gray.jpg:

Cv2.imwrite ("gray.jpg", IMG)

OpenCV Learning 02--image reading and output under Python

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.