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've been focusing on Python's software, breaking up and learning something about Python on the way. It feels like Python is a simple, easy-to-start scripting language, with many third-party libraries making python exceptionally powerful. Be able to deal with many different problems at the same time its many open source free libraries make Python's use is also very extensive.

      In the field of scientific computing, data processing and image, MATLAB has been used in the past. MATLAB is also a beautiful language, simple and convenient programming language, all said that Matlab and Python in some areas is very similar, indeed, on the scientific calculation, data processing is really, Matlab in a lot of scientific computing things in Python basically have the corresponding library package included, such as Numpy,matplotlib, and so on, these are powerful and applicable. But to say different. That's the application level. MATLAB is a commercial nature of software, genuine software is also abnormal expensive, personal feeling belongs to the research field of use more, after work seems generally not how to use this (may be more expensive reason). But in general, MATLAB is the same 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, now it seems that their very many functions are so spirit. However, OpenCV is a professional library package, many of the functions of MATLAB is not, and from its use of surface and promotion extent, OPENCV is indeed so powerful.

      So it is necessary to know OpenCV in the image domain (whether it is the Python version or the C + + version number), but for the learners who are simple to apply. Understanding the Python version is enough.

(i) About Python

The first thing that Python wants to say is. The mere download of Python on the official website is not big, it is an initial python. You can do some simple things. But when you're doing a little bit more complex, you need a lot of third-party installation packages. such as NumPy, and so on, these libraries installed in said is not very complex, there are a lot of tutorials on the web, but when many packages installed, and some of the library package does not have your computer version number, and so on there are many factors cause your library package is not installed, think it is quite troublesome. I used to install a variety of packages are going to collapse. Finally found an integrated software, that is, the frequently used packages are added to the Python, the software is Ipython. is an integrated scientific computing software. Inside the integration of a lot of packages, and is a likeness of Matlab software, debugging, testing. Watch the variable values and so on a lot of the same place, so suppose you want to study such a face and distressed install package, download this 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. About this type of topic. CSDN also have a lot of, also have 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
Then there is the following Blogger series blog, very good, able to learn:
http://blog.csdn.net/sunny2038/article/category/904451
Under a lot of information, simply record your own study notes.

(iii) Start from the beginning – Read the image

Read images using OpenCV under Python.

First need to import OPENCV package, the above said that the Ipython does not have OPENCV package, so you want to use the first correctly import OpenCV package again. As to how to import, first download a OPENCV package, there is a Python OpenCV package, this way to get into it (very easy, Baidu under. Skip it).
About the execution interface of Ipython, mine is this (name becomes Spyder):

Before you can read the image, you have to put the image of your experiment into the working folder beforehand. The Read function is Cv2.imread (), about the function description:
Cv2.imread (' image name '). ' Optional number of references ')
The optional number of parameters determines 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, just read in the result is none. All right. Reading into an image is this:
Import Cv2
img = cv2.imread (' flower.jpg ')
When your import cv2 executes without errors, the import succeeds.

(iv) Storage of images

Store image function: Cv2.imwrite (' Saved image name ', image, ' number of references ')
Normal image storage use is cv2.imwrite (' Saved image name '). Image). The next number of parameters is the default. The saved image name also takes the type, for example jpg,bmp and so on, OPENCV now supports reading BMP, JPG, PNG, TIFF and other frequently used 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, determines the image storage size, clarity, the default is very good. Want to scrutiny can see the following. Here are some of the following:
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

The OPENCV comes with the display image function. But it is more complicated and personal feeling is not stable. Here is a presentation method that uses the library package matplotlib (the Library package Ipython) to display the image.
First of all, the OPENCV comes with a function display, which typically displays an image. After the image has been made, it is then combined with the following functions:

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 differences on the system, and such a display method can not save the image, adjust the size of the image, and so on, there is a frequently used and powerful display image method, using the Pyplot child package in the Matplotlib library package to display, such a display method is not only stable. You can also resize, save the displayed image, and so on. So generally its usage is for example the following:

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

Here is the diagram shown above, which can be seen in a new interface:

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

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.