And yeah, CV2 installed and I what I can with CV2 among many other things what I can do the
very first thing is to load an image in Python from using CV2 methods.
So I will be loading that image which is a very small image and by the way it only has 15 pixels. You know
a normal image that you use to take pictures on your phone or whatever it has like a thousand by a thousand
pixels which is one million pixels more or less.
But we should keep things simple so you understand what is going on.
CV to import that.
Yup, that was successful.
Now I'll create a variable and call this im for image and for gray.
You can call it whatever you want. And CV2 to get an image to load it in Python.
You want to use image read method and than you pass the name of your image. You can find this image in the
resource section.
So just go ahead and download it.
And now this is here in the same directory with my Jupiter Notebook.
So I can go ahead and point to the name of the file including the extension smallgray.png.
And here is image read method
also expects one other argument from you that can be either 0 or 1 so 0 means you want to read the image
in grey scale and if you pass the 1 you are reading the image in BGR which means blue green and red.
And you'll understand what each of the versions mean.
So let's do this all first.
Yeah I'll print that.
And yeah, that was it.
So what we got is a Numpy array. It's a two dimensional array.
So it has dimensions 3 by 5 which is the same as our image, three rows and five columns.
Now the reason we pass 0 there so we are reading that as a grey scale image is because you know this
png file
in reality it has three bands.
Or you can say three layers and each layer has a certain amount of color.
So yeah, the first layer has a certain amount of blue.
And the second layer has a certain amount of green.
And the last layer has a certain amount of red.
So combining each pixel is a combination of all the intensity of blue green and red.
I know in other programs they use RGB and OpenCV uses BGR.
Now practically this file here, this image practically is a gray scale image.
However each of these pixeles has its own band so the combination of them always creates
in this case lays gray or white and black colors.
But if we had a color image this would convert it to a grayscale, this function.
So again what we have here is you know 187 is the value of the intensity for this first pixel.
And then you have the other number and for white you see what white here? I think we have three white pixels.
Now those are 255.
All of them. And yeah, colors range from zero. Zero would be black pitch black and 255 for white. And everything
between them
is in the gray scale.
If I change this to 1, I'll get a three dimensional array where this represents the blue layer the
green and the red.
Or you can even print that out.
Be careful though that this hays a bit transposed.
So the matrices are three columns in here and the rows are vertical.
And basically what this say is this pixel has this corresponding values.
So it has an intensity of 187
for blue 198 for green and 209 for red and yeah that's how you create Numpy arrays out of images.
But how about the other way around, so creating images out of an Numpy array? Well to do that you'd use
CV2 again .CV2 and then you would point to the
imwrite method so image write.
And then you want to pass a name for your image.
Let's say newsmallgrey.png.
And of course you need to pass your Numpy array. So let's pass image gray. Execute that, you'll get two.
Which means that your file should have being created now. And yeah, new small gray and there's the file.
And I hope that strengthen your knowledge about Numpy.
Now we can go ahead and do some more things here.
Specifically the next lecture we will learn how to do indexing and slicing and iterating of Numpy arrays.
So basically you learn how to access these values.
See you there in the next lecture.