Thursday 23 June 2016

First Tutorial : Reading an Image

Hello everyone,
       This post is all about how to get started with OpenCV and python. We will start with very basic operation i.e "READING AN IMAGE". 

NOTE : I assume you have downloaded the required software to get started. If not, please click here to download them. I strongly recommend you to go through this post [click here] before attempting this program. There is an IDE called "IDLE" for python that we will be using to write our programs.

So let's start line by line.

1)Open IDLE(IDE for Python). First you need to import OpenCV library. To do this, type:

import cv2

2) Now import numpy library. It is used for arithmetic manipulation of images. We will use it in almost all our programs so remember to add this line in every program:

import numpy as np

Note : as np means we are giving numpy a short name np so that it is easy to use in programs. It has no other relevance than that.


3) We will now read an image using imread('imagename.extension') function provided by OpenCV library :

img  =  cv2.imread("hamster.jpg")

This line reads the image named "hamster.jpg" and stores it in the variable img. The image must be in the same folder as the program or else you need to give full path of the image. There is also a second parameter that can be passed to imread method to change the mode of image(grayscale, color, true color) but we will see that later.

4) Displaying an image:

cv2.imshow('img1', img)

Here is another method imshow('window name', image variable) which is used to show an image in a window named 'img1' as specified in the line of code above.

5) Hold the window

cv2.waitKey(0)
cv2.destroyAllWindows()

This is a special function which holds the screen for the number of milliseconds mentioned as parameter. For ex. cv2.waitKey(1000) would hold a window for 1 second. cv2.waitKey(0) will hold the window indefinitely until user closes the window himself. This can be used in more interesting ways that we will see later. cv2.destroyAllWindows(), as the name suggest destroy all the windows that were opened in a particular program. These two lines of code should be added to almost all the program we are going to do.

So that's the end of our first program. I am summing up whole program together. Here it goes:
After you have written the code press CTRL+S to save the code to a desired location. Remember to save the file using .py extension. After this, press F5 to run the code while your code is open in IDLE.

*************************************** Code:

import cv2
import numpy as np
                                                                    #this is a comment 
img = cv2.imread('hamster.jpg')                 #loads an image into img

cv2.imshow('img1', img)                            #display the image

cv2.waitKey(0)                                           #waits indefinitely
cv2.destroyAllWindows()                          #closes all the open windows

***************************************
Output:Output of the above code is an image. In my case it's a  hamster's image. Here it is: 














Image Source: smallanimalchannel.com

I hope you understood this basic tutorial on reading an image using OpenCV and Python.

Any queries please ask in the comments section or mail me at kumarankit0411@gmail.com

Thankyou.


No comments:

Post a Comment