Top 5 inevitable methods for beginners in OpenCV using Python

Vignesh Kathirkamar
Analytics Vidhya
Published in
4 min readAug 28, 2020

Introduction:

As a beginner when you start using OpenCV, it is necessary to know some of the methods which will be almost everytime used in your opencv projects. Listed below are the most mostly used five methods in OpenCV (Remember OpenCV is imported as cv2)

1. cv2.imread()

The method imread, more commonly called as image read, in opencv is used for reading the images from a file path

Parameters: cv2.imread(filename[,flags])

In the above parameter you can see that the mandatory requirement is the filename which can be passed as a string. The filename is the image which you want to be read by the computer.

Opencv, Mr.Bean, Transport Pythonified, Vignesh Kathirkamar

Note: alpha channel is the one which decides the opacity/transparency of an image

Example: img = cv2.imread(r’C:\Users\MrBean.webp’,0)

2. cv2.imshow()

As the name suggests this method is used for showing the images

Parameters: cv2.imshow(window_name,image)

The window_name should be a string value and hence will be in enclose in double or single quotes. The image shall be the image which should be

Example: cv2.imshow(“MrBean”,img)

3. cv2.cvtColor()

cvtColor can be interpreted as convert color and this method is used for converting the image from one color space to another color space (Find what is a color space here if you don’t know about it)

Parameters: cv2.cvtColor(src, code[, dst[, dstCn]])

src is the source image whose color space you intend to change. code is the one which specifies “to which color space should the source image should be converted”. Since other parameters are not mandatory, as a beginner you can skip it for time being

Example: cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

4. cv2.waitKey()

This function is very important, without this function cv2.imshow() won’t work properly.

Parameters: cv2.waitkey(wait time in milliseconds)

Thus if the wait time is entered as 6000, the picture will be displayed for 6s and then get closed (provided you have cv2.destroyAllWindows() in the script). If you use ‘0’ as the parmater then the image will be displayed for infinite time until you press the esc key.

Example: cv2.waitKey(0)

5. cv2.destroyAllWindows()

This method destroys (in other words “closes”) all the windows created using the opencv methods. If you want to close a specific window, then you can pass the window name as the argument within this function

Parameters: name of a window opened using opencv (not mandatory)

Missing to provide the cv2.destroyAllWindows() at the end of the script might make the window opened to crash

Summing up the examples:

Find the above mentioned methods in action, shown in the examples below:

Opencv, Mr.Bean, Transport Pythonified, Vignesh Kathirkamar
Opencv, Mr.Bean, Transport Pythonified, Vignesh Kathirkamar
Opencv, Mr.Bean, Transport Pythonified, Vignesh Kathirkamar

Did you note the flag values for cv2.imread() in the above three pictures. If not scroll up and see how the output varies for different flag values.

Now have a look at how cv2.cvtColor works in the following two images

Opencv, Mr.Bean, Transport Pythonified, Vignesh Kathirkamar
Opencv, Mr.Bean, Transport Pythonified, Vignesh Kathirkamar
Blue Bean :P

Haha, saw how c2.cvtColor changed our Mr.Bean to Blue Bean?, also you can explore methods which converts Mr.Bean to Red Bean and Green Bean too!!

Hopefully the opencv methods are getting interesting already.

Now let’s see how the specifying values within cv2.waitkey() works, using the following gif image

Opencv, Mr.Bean, Transport Pythonified, Vignesh Kathirkamar

Conclusion:

As you have a grip on the basic methods now to read, show, convert color and make a image wait for few seconds before it closes, you can start exploring other methods which are used for image processing.

If this was useful for you, don’t forget to leave two claps :)

Regards,

Vignesh Kathirkamar

AI Pylinux

--

--