1 00:00:00,000 --> 00:00:04,944 And yeah, cv2 is installed, and 2 00:00:04,950 --> 00:00:06,780 now what I can do with cv2, among 3 00:00:06,780 --> 00:00:08,640 many other things, what I can do the 4 00:00:08,670 --> 00:00:11,640 very first thing is to load an image in 5 00:00:11,640 --> 00:00:15,660 Python using cv2 methods. So I'll be 6 00:00:15,660 --> 00:00:17,670 loading that image, which is a very 7 00:00:17,670 --> 00:00:20,730 small image by the way. It only has 15 8 00:00:20,730 --> 00:00:24,420 pixels. You know, a normal image that you use to 9 00:00:24,420 --> 00:00:26,550 take pictures or on your phone or 10 00:00:26,550 --> 00:00:29,430 whatever, it has like a 1000 by 1000 11 00:00:29,460 --> 00:00:32,610 pixels, which is 1 million pixels, more 12 00:00:32,610 --> 00:00:34,800 or less. But we should keep things 13 00:00:34,800 --> 00:00:37,530 simple. So you understand what is going 14 00:00:37,530 --> 00:00:42,840 on cv2 import that, and yeah that was 15 00:00:42,840 --> 00:00:45,330 successful. You know, I'll create a 16 00:00:45,330 --> 00:00:48,630 variable and call this im for image, 17 00:00:48,930 --> 00:00:52,260 and g for great. You can call it whatever 18 00:00:52,260 --> 00:00:55,620 you want, and cv2 to create an image to 19 00:00:55,620 --> 00:00:59,055 hold it in Python, you want to use imageread 20 00:00:59,079 --> 00:01:01,440 method, and then you pass the 21 00:01:01,440 --> 00:01:03,780 name of your image, you can find this 22 00:01:03,780 --> 00:01:06,540 image in the resource section. So just 23 00:01:06,540 --> 00:01:08,340 go ahead and download it. And now this 24 00:01:08,340 --> 00:01:11,580 is here in the same directory with my 25 00:01:11,580 --> 00:01:13,860 Jupyter Notebook. So I can go ahead and 26 00:01:13,860 --> 00:01:16,710 points to the name of file, including the 27 00:01:16,740 --> 00:01:21,021 extension, smallgray.png, and here, the 28 00:01:21,046 --> 00:01:23,610 imageread method also expect one 29 00:01:23,610 --> 00:01:26,100 other argument from you, and that can be 30 00:01:26,100 --> 00:01:30,030 either 0 or 1. So 0 means you 31 00:01:30,030 --> 00:01:32,910 want to read the image in grayscale, and 32 00:01:32,910 --> 00:01:34,980 if you pass 1, you will reading the 33 00:01:34,980 --> 00:01:39,150 image in BGR, which means blue, green, 34 00:01:39,186 --> 00:01:42,126 and red, and you will understand what 35 00:01:42,210 --> 00:01:44,730 each of the versions mean. So let's do 36 00:01:44,730 --> 00:01:48,256 this one first, and yeah I'll print that, 37 00:01:48,280 --> 00:01:51,244 [No audio] 38 00:01:51,269 --> 00:01:54,570 and yeah, that was it, so what we got is a numpy 39 00:01:54,570 --> 00:01:57,450 array, its a 2-dimensional array, so it 40 00:01:57,450 --> 00:02:00,540 has dimensions 3 by 5, which is 41 00:02:00,540 --> 00:02:04,410 the same as our image, 3 rows and 42 00:02:04,410 --> 00:02:07,290 5 columns. Now the reason we pass 43 00:02:07,290 --> 00:02:09,990 0 there, so we reading that as a 44 00:02:09,990 --> 00:02:12,450 grayscale image is because you know this 45 00:02:12,450 --> 00:02:14,970 PNG file, in reality, it has 3 46 00:02:15,000 --> 00:02:17,850 bands, or you can say 3 layers, and 47 00:02:17,850 --> 00:02:20,760 each layer has a certain amount of 48 00:02:20,790 --> 00:02:24,870 colors. So here for the first layer has a 49 00:02:24,870 --> 00:02:27,060 certain amount of blue, and the second 50 00:02:27,060 --> 00:02:29,310 layer has a certain amount of green, and 51 00:02:29,310 --> 00:02:30,990 the last layer has a certain amount of 52 00:02:30,990 --> 00:02:33,840 red. So combining each pixel is a 53 00:02:33,840 --> 00:02:37,080 combination of the intensity of blue, 54 00:02:37,110 --> 00:02:39,390 green, and red. I know in other 55 00:02:39,390 --> 00:02:42,990 programs, they use RGB, in OpenCV, uses 56 00:02:42,990 --> 00:02:46,980 BGR. Now practically this file here, 57 00:02:46,980 --> 00:02:49,440 this image practically is a grayscale 58 00:02:49,440 --> 00:02:52,650 image. However, each of these pixel has 59 00:02:52,650 --> 00:02:55,440 its own bands. So and the combination of 60 00:02:55,440 --> 00:02:58,020 them always creates, in this case, these 61 00:02:58,050 --> 00:03:02,040 gray, white, and black colors. But if we 62 00:03:02,040 --> 00:03:04,500 had a color image of these, its 63 00:03:04,500 --> 00:03:06,780 converted to a grayscale, this function. 64 00:03:07,175 --> 00:03:11,615 So again, what we have here is, you 65 00:03:11,640 --> 00:03:15,510 know, 187 is the value intensity for 66 00:03:15,510 --> 00:03:19,200 this first pixel and then you have the other 67 00:03:19,200 --> 00:03:21,900 number, and for white, you see white 68 00:03:21,900 --> 00:03:24,990 here, we have 3 white pixels, and 69 00:03:24,990 --> 00:03:30,660 those are 255, all of them, and yeah 70 00:03:30,660 --> 00:03:34,350 colors range from 0, 0 would be black, pitch 71 00:03:34,350 --> 00:03:38,850 black, and 255 for white, and 72 00:03:38,850 --> 00:03:40,590 everything between them is seeing the 73 00:03:40,615 --> 00:03:43,622 grayscale. So now if I change this to 1, 74 00:03:43,652 --> 00:03:46,126 [No audio] 75 00:03:46,156 --> 00:03:47,855 I'll get the 3-dimensional array 76 00:03:47,880 --> 00:03:50,160 where this represents that blue layer, 77 00:03:50,735 --> 00:03:54,060 the green, and the red, or you can even print that out. 78 00:03:54,090 --> 00:04:00,269 [No audio] 79 00:04:00,300 --> 00:04:02,550 Be careful though that this haze a big 80 00:04:02,550 --> 00:04:06,210 transposed, so the matrices are 3 81 00:04:06,210 --> 00:04:08,790 columns in here and the rows are 82 00:04:08,790 --> 00:04:11,460 vertical, and basically, what this says 83 00:04:11,490 --> 00:04:14,310 is this big cell has this corresponding 84 00:04:14,310 --> 00:04:19,650 values. So it has an intensity of 187 85 00:04:19,650 --> 00:04:25,050 for blue, 198 for green, and 209 86 00:04:25,050 --> 00:04:27,570 for red, and yeah, that's how you create 87 00:04:27,570 --> 00:04:30,720 NumPy arrays out of images. But how 88 00:04:30,720 --> 00:04:33,840 about the other way around to creating 89 00:04:33,870 --> 00:04:37,890 images on of NumPy array? Well to 90 00:04:37,890 --> 00:04:42,210 do that, you'd use cv2 again, cv2 and 91 00:04:42,210 --> 00:04:44,400 then you'd points to the imwrite 92 00:04:44,400 --> 00:04:46,950 method, so imagewrite, and then you 93 00:04:46,950 --> 00:04:51,030 want to pass a name for your image. 94 00:04:51,725 --> 00:04:56,715 Let's say newsmallgray.png, and then 95 00:04:56,740 --> 00:04:58,860 of course, you need to pass your 96 00:04:58,899 --> 00:05:03,729 NumPy array, so let's pass image_gray, 97 00:05:04,860 --> 00:05:07,860 execute that. You'll get True, which 98 00:05:07,860 --> 00:05:09,510 means that your file should have been 99 00:05:09,510 --> 00:05:16,740 created now. Yeah newsmallgray, and yeah, 100 00:05:16,740 --> 00:05:19,230 there's a file, and I hope that 101 00:05:19,230 --> 00:05:21,898 strengthened your knowledge about NumPy. 102 00:05:22,392 --> 00:05:24,420 Now we'll go ahead and do some 103 00:05:24,420 --> 00:05:27,120 more things here. And specifically, in the 104 00:05:27,120 --> 00:05:29,400 next lecture, we'll learn how to do 105 00:05:29,400 --> 00:05:33,690 indexing and slicing and iterating of 106 00:05:33,720 --> 00:05:36,720 NumPy arrays. So basically, you'll 107 00:05:36,720 --> 00:05:39,450 learn how to access these values. See 108 00:05:39,450 --> 00:05:40,653 you there in the next lecture.