Python OpenCV using Notes (a) (simple image reading, display and storage)

Source: Internet
Author: User
Tags image processing library

    • Before you write it.
      Since last year, I began to focus on Python software, interrupted and learned some things about python on the way, feeling that Python is really a simple and graceful, easy-to-Learn scripting language, many third-party libraries make python exceptionally powerful, can handle many different problems, At the same time, many of its open source free libraries make Python's use very extensive. In the field of scientific computing, data processing and image, I have been using MATLAB, I feel matlab is a beautiful language, simple and convenient programming language, all said that Matlab and Python in some areas is very similar, it is true, on the scientific calculation, data processing is really, MATLAB in many scientific computing things in Python basically have a corresponding library package included, such as Numpy,matplotlib and so on, these are strong and applicable, but to say different, that is the application level, MATLAB is a commercial software, Genuine software is also abnormal expensive, personal feeling belongs to the research field of use more, in the work seems generally not how to use this (probably more expensive reason), but in general, MATLAB is also in the field of computing is very powerful.
      OpenCV is an open-source computer vision Library, from the beginning of the study of the image, and did not really touch it, the use of MATLAB with the image processing library, it seems that many of their functions are so spirit. But OpenCV, after all, is a professional library package, many of the functions of MATLAB, and from its use of surface and promotion extent, OPENCV is really so powerful. So it is necessary to know OpenCV (either Python or C + +) in the image domain, but it is enough for a simple-to-use learner to understand the Python version.
(i) About Python

The first thing that Python wants to say is that the mere download of Python is not very big, it's an initial python, it's easy to do some simple things, but when you do a bit more complex, you need a lot of third-party packages, like NumPy, These libraries installed in said is not very complex, there are many tutorials on the web, but when a lot of packages installed, and some of the library package does not have your computer version, and so many other factors cause your library package installation, think it is very troublesome, I have been installing a variety of packages are going to collapse. Finally simply found an integrated software, is the common package are added to the Python, the software is Ipython, is an integrated scientific computing software, which integrates a lot of packages, and is a spirit of Matlab software, debugging, testing, viewing variable values and so many of the same place, So if you want to study this and fret about installing the package, just download it and install it.

(ii) Use of OpenCV under Python

The focus of this series of blogs is to document the methods, notes and ideas of learning OpenCV under Python. On this type of theme, there are also a lot of csdn, but also very professional, the most important is the following this blogger wrote a PDF file, is really good things (big sense Bo main selfless):
http://bbs.csdn.net/topics/390837851
Http://pan.baidu.com/s/1jG62bhO
And then there is this blogger series blog, very good, can learn:
http://blog.csdn.net/sunny2038/article/category/904451
Under a lot of information, simply record your own study notes.

(iii) Start from scratch – read images

Read images using OpenCV under Python. First need to import OPENCV package, the above said that the Ipython is not OPENCV package, so want to use the correct first import OPENCV package again, as to how to import, first download a OPENCV package, there is about Python OpenCV package, Put this into a way to carry out (very simple, Baidu, skip it).
About Ipython's running interface, mine is this (name becomes Spyder):

Before you can read an image, you have to put the image of your experiment in the working directory beforehand. The Read function is Cv2.imread (), about the function description:
Cv2.imread (' Image name ', ' optional parameters ')
The optional parameters determine the mode in which the image is read:
0: Grayscale image read -in (even if the image is colored)
1: The read-in image is colored (default);
Note: Even if the image does not exist in the workspace, this function will not error, only the result of reading is none. Well, reading into an image is like this:
Import Cv2
img = cv2.imread (' flower.jpg ')
When your import cv2 run without errors, the import succeeds.

(iv) Storage of images

Store image function: Cv2.imwrite (' Saved image name ', image, ' parameter ')
Normal image storage use is cv2.imwrite (' Saved image name ', image), the following parameter default, save the image name also has a type, such as jpg,bmp and so on, OPENCV currently supports reading BMP, JPG, PNG, TIFF and other common formats, The third parameter is more complex, but also the score is JPG, BMP and so on the type of image and different, in fact, it is decided the image storage size, clarity, the default is very good, want to scrutiny can see below, this is part of the introduction:
OpenCV Python Tutorials (1, loading, displaying, and saving images)
Then get an image to store, for example directly: Cv2.imwrite (' good_gray.tif ', IMG)

(v) Image display

OpenCV comes with a display image function, but more complex and personal feeling is not stable, here is the introduction of a display method, is to use the library package matplotlib (this library package Ipython) to display the image.
The first is the OPENCV function display, generally to display an image, after the image has been, and then directly following the function together with:

cv2.imshow(‘image‘,img)cv2.waitKey(0)cv2.destroyAllWindows()

The first parameter of the function cv2.imshow is the name, and the second is the graph to be displayed. However, in order for the image to stay on the display, the following two words are used together. Because the system (32-bit and 64-bit) is not the same, sometimes appear not to appear, the personal feeling OpenCV itself C language written, there are system differences, and this display method can not save the image, adjust the size of the image, and so on, here is another common and powerful display image method, Using the Pyplot child package in the Matplotlib library package, this display method is stable, can be resized, saved the displayed image, and so on. Then generally it uses the following methods:

import cv2fromimportas pltimg = cv2.imread(‘good.jpg‘,0);#打开为灰度图像‘gray‘#必须规定为显示的为什么图像#隐藏坐标线 #显示出来,不要也可以,但是一般都要了

Here is the diagram shown above, which you can see displayed in a new interface:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python OpenCV using Notes (a) (simple image reading, display and storage)

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.