1
00:00:01,350 --> 00:00:08,730
And yeah, CV2 installed and I what I can with CV2 among many other things what I can do the

2
00:00:08,850 --> 00:00:14,620
very first thing is to load an image in Python from using CV2 methods.

3
00:00:14,730 --> 00:00:22,080
So I will be loading that image which is a very small image and by the way it only has 15 pixels. You know

4
00:00:22,080 --> 00:00:29,550
a normal image that you use to take pictures on your phone or whatever it has like a thousand by a thousand

5
00:00:29,580 --> 00:00:33,450
pixels which is one million pixels more or less.

6
00:00:33,840 --> 00:00:38,290
But we should keep things simple so you understand what is going on.

7
00:00:38,310 --> 00:00:41,270
CV to import that.

8
00:00:42,170 --> 00:00:44,200
Yup, that was successful.

9
00:00:44,230 --> 00:00:51,150
Now I'll create a variable and call this im for image and for gray.

10
00:00:51,540 --> 00:00:56,720
You can call it whatever you want. And CV2 to get an image to load it in Python.

11
00:00:56,730 --> 00:01:04,740
You want to use image read method and than you pass the name of your image. You can find this image in the

12
00:01:04,740 --> 00:01:05,900
resource section.

13
00:01:06,270 --> 00:01:07,810
So just go ahead and download it.

14
00:01:07,900 --> 00:01:12,840
And now this is here in the same directory with my Jupiter Notebook.

15
00:01:13,050 --> 00:01:19,260
So I can go ahead and point to the name of the file including the extension smallgray.png.

16
00:01:19,650 --> 00:01:22,080
And here is image read method

17
00:01:22,080 --> 00:01:31,170
also expects one other argument from you that can be either 0 or 1 so 0 means you want to read the image

18
00:01:31,200 --> 00:01:40,080
in grey scale and if you pass the 1 you are reading the image in BGR which means blue green and red.

19
00:01:41,040 --> 00:01:43,920
And you'll understand what each of the versions mean.

20
00:01:44,070 --> 00:01:46,990
So let's do this all first.

21
00:01:47,020 --> 00:01:51,250
Yeah I'll print that.

22
00:01:51,540 --> 00:01:52,420
And yeah, that was it.

23
00:01:52,500 --> 00:01:57,000
So what we got is a Numpy array. It's a two dimensional array.

24
00:01:57,270 --> 00:02:06,420
So it has dimensions 3 by 5 which is the same as our image, three rows and five columns.

25
00:02:06,420 --> 00:02:12,510
Now the reason we pass 0 there so we are reading that as a grey scale image is because you know this

26
00:02:12,550 --> 00:02:13,380
png file

27
00:02:13,380 --> 00:02:15,770
in reality it has three bands.

28
00:02:15,780 --> 00:02:21,740
Or you can say three layers and each layer has a certain amount of color.

29
00:02:22,530 --> 00:02:26,150
So yeah, the first layer has a certain amount of blue.

30
00:02:26,190 --> 00:02:29,110
And the second layer has a certain amount of green.

31
00:02:29,130 --> 00:02:31,990
And the last layer has a certain amount of red.

32
00:02:32,130 --> 00:02:38,870
So combining each pixel is a combination of all the intensity of blue green and red.

33
00:02:38,940 --> 00:02:44,420
I know in other programs they use RGB and OpenCV uses BGR.

34
00:02:44,960 --> 00:02:50,910
Now practically this file here, this image practically is a gray scale image.

35
00:02:50,970 --> 00:02:56,660
However each of these pixeles has its own band so the combination of them always creates

36
00:02:56,660 --> 00:03:01,080
in this case lays gray or white and black colors.

37
00:03:01,530 --> 00:03:07,140
But if we had a color image this would convert it to a grayscale, this function.

38
00:03:07,410 --> 00:03:18,170
So again what we have here is you know 187 is the value of the intensity for this first pixel.

39
00:03:18,200 --> 00:03:24,760
And then you have the other number and for white you see what white here? I think we have three white pixels.

40
00:03:24,850 --> 00:03:27,960
Now those are 255.

41
00:03:28,200 --> 00:03:39,330
All of them. And yeah, colors range from zero. Zero would be black pitch black and 255 for white. And everything

42
00:03:39,330 --> 00:03:40,030
between them

43
00:03:40,050 --> 00:03:42,210
is in the gray scale.

44
00:03:42,300 --> 00:03:50,990
If I change this to 1, I'll get a three dimensional array where this represents the blue layer the

45
00:03:51,030 --> 00:03:52,780
green and the red.

46
00:03:52,820 --> 00:03:54,120
Or you can even print that out.

47
00:03:54,120 --> 00:04:00,480

48

00:04:00,480 --> 00:04:03,770
Be careful though that this hays a bit transposed.

49
00:04:04,170 --> 00:04:10,150
So the matrices are three columns in here and the rows are vertical.

50
00:04:10,440 --> 00:04:16,080
And basically what this say is this pixel has this corresponding values.

51
00:04:16,620 --> 00:04:19,160
So it has an intensity of 187

52
00:04:19,270 --> 00:04:29,400
for blue 198 for green and 209 for red and yeah that's how you create Numpy arrays out of images.

53
00:04:30,330 --> 00:04:38,850
But how about the other way around, so creating images out of an Numpy  array? Well to do that you'd use

54
00:04:38,880 --> 00:04:43,930
CV2 again .CV2 and then you would point to the

55
00:04:43,970 --> 00:04:46,230
imwrite method so image write.

56
00:04:46,590 --> 00:04:51,640
And then you want to pass a name for your image.

57
00:04:52,010 --> 00:04:56,160
Let's say newsmallgrey.png.

58
00:04:56,630 --> 00:05:07,620
And of course you need to pass your Numpy array. So let's pass image gray. Execute that, you'll get two.

59
00:05:07,710 --> 00:05:17,470
Which means that your file should have being created now. And yeah, new small gray and there's the file.

60
00:05:17,470 --> 00:05:17,950

61

00:05:18,660 --> 00:05:22,640
And I hope that strengthen your knowledge about Numpy.

62
00:05:22,650 --> 00:05:26,220
Now we can go ahead and do some more things here.

63
00:05:26,490 --> 00:05:34,970
Specifically the next lecture we will learn how to do indexing and slicing and iterating of Numpy arrays.

64
00:05:35,000 --> 00:05:35,550

65

00:05:36,090 --> 00:05:39,360
So basically you learn how to access these values.

66
00:05:39,360 --> 00:05:40,650
See you there in the next lecture.

