And yeah, cv2 is installed, and
now what I can do with cv2, among
many other things, what I can do the
very first thing is to load an image in
Python using cv2 methods. So I'll be
loading that image, which is a very
small image by the way. It only has 15
pixels. You know, a normal image that you use to
take pictures or on your phone or
whatever, it has like a 1000 by 1000
pixels, which is 1 million pixels, more
or less. But we should keep things
simple. So you understand what is going
on cv2 import that, and yeah that was
successful. You know, I'll create a
variable and call this im for image,
and g for great. You can call it whatever
you want, and cv2 to create an image to
hold it in Python, you want to use imageread
method, and then 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
Jupyter Notebook. So I can go ahead and
points to the name of file, including the
extension, smallgray.png, and here, the
imageread method also expect one
other argument from you, and that can be
either 0 or 1. So 0 means you
want to read the image in grayscale, and
if you pass 1, you will reading the
image in BGR, which means blue, green,
and red, and you will understand what
each of the versions mean. So let's do
this one first, and yeah I'll print that,
[No audio]
and yeah, that was it, so what we got is a numpy
array, its a 2-dimensional array, so it
has dimensions 3 by 5, which is
the same as our image, 3 rows and
5 columns. Now the reason we pass
0 there, so we reading that as a
grayscale image is because you know this
PNG file, in reality, it has 3
bands, or you can say 3 layers, and
each layer has a certain amount of
colors. So here for 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 the intensity of blue,
green, and red. I know in other
programs, they use RGB, in OpenCV, uses
BGR. Now practically this file here,
this image practically is a grayscale
image. However, each of these pixel has
its own bands. So and the combination of
them always creates, in this case, these
gray, white, and black colors. But if we
had a color image of these, its
converted to a grayscale, this function.
So again, what we have here is, you
know, 187 is the value intensity for
this first pixel and then you have the other
number, and for white, you see white
here, we have 3 white pixels, and
those are 255, all of them, and yeah
colors range from 0, 0 would be black, pitch
black, and 255 for white, and
everything between them is seeing the
grayscale. So now if I change this to 1,
[No audio]
I'll get the 3-dimensional array
where this represents that blue layer,
the green, and the red, or you can even print that out.
[No audio]
Be careful though that this haze a big
transposed, so the matrices are 3
columns in here and the rows are
vertical, and basically, what this says
is this big cell 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 to creating
images on of NumPy array? Well to
do that, you'd use cv2 again, cv2 and
then you'd points to the imwrite
method, so imagewrite, and then you
want to pass a name for your image.
Let's say newsmallgray.png, and then
of course, you need to pass your
NumPy array, so let's pass image_gray,
execute that. You'll get True, which
means that your file should have been
created now. Yeah newsmallgray, and yeah,
there's a file, and I hope that
strengthened your knowledge about NumPy.
Now we'll go ahead and do some
more things here. And specifically, in the
next lecture, we'll learn how to do
indexing and slicing and iterating of
NumPy arrays. So basically, you'll
learn how to access these values. See
you there in the next lecture.