Friday, December 13, 2019

6. Using Numpy with OpenCV-Python

Hey, welcome back its been a while since I have been active on this blog, but yes i was preparing something great, that is a free YouTube course on Advanced Computer Vision using OpenCV-Python.


I have already released one section of the course you can find it on this
URL: https://www.youtube.com/playlist?list=PLwRoxHWReaEhVFjTeKlifKUimbw6ZyV7K 


Now about Numpy, Numpy stands for Numeric Python, it is highly optimized library for python for doing numeric operations, that is addition multiplication, subtraction and division not only on numbers but n-dimentional array or matrix you can say.

So OpenCV uses Numpy to represent the images, now whatever operation you do, you'll be doing with Numpy Array or matrix you can say.

To prove this I'm going to show you a small code snippet which reads image using opencv and displays the data type of the image variable.



As you can see it shows numpy.ndarray which means numpy n-dimentional array because the image is RGB image, it has width,height and channels.


Can we create an image using Numpy then??


The answer is Yes, we can create a blank image using Numpy, let see how to do it.


The first step is to import numpy library with alias, alias is nothing but the custom name you give to your package which is to be considered throughout the program life cycle. It can be np, it can be num or anything you like, the best practice is to keep it a short name because we will be needing the alias name to access numpy methods.

In can second step we are creating a blank image, we are using np alias of numpy package to access zeros method what it means that whatever size of the array will be created it will be filled with zero(0) values. In OpenCV 0 value means black Pixel, so our image will be of black pixel.

Zeros method takes two arguments 1. Shape 2. data type.

1. Shape: shape is nothing the the dimension or size of our image, if its gray or single channel image the shape will be (width,height) and if your image is RGB then the shape will be  (width,height,channels).

Important thing to not here is these values are passed as tuples, tuples are nothing but the type of array you can say except its values can not be changed.

2. dtype (data type): opencv uses uint8 data type to represent the images, because the pixel values ranges from 0 to 255. any addition or  or subtraction from the values should be result in circular increment or decrements . that means the maximum value of the pixel is 255 if you add +1 to the pixel value dtype="uint8" will reset the value to 0 so 256 value will be considered as 0 and 257 as 1 and so on.

Same when subtracting value from 0 it will reset the value to 255 like 0 - 1 = 255, 0 - 2 = 254 and so on.



 Changing the value of x=0 y=0 c=0 by adding 265 to its original value it results in 9. its circular.

In other words, 0 + 265 = 9 when uint8 data type is used, because 255 is max value for uint8 data type. when the values reaches 256 its sets its value to 0 and then again the increment starts so result is 9.

I hope you understood the concept. Let me know in case of any doubts.


Until then Happy Learning.


Jaimin Bhoi
Assistant System Engineer/Computer Vision Researcher
Tata Consultancy Services.





#opencv #python #ai #ml #dl #machinelearning #deeplearning #keras #tensorflow #pytorch #anaconda #conda #spyder #jupyter #notebook #opencv #computervision #logo #deeplearning #artificialintelligence #ai #python #aipython #machinelearning #deeplearning #photography #photoshop

No comments:

Post a Comment