1
00:00:00,210 --> 00:00:02,160
Hi, welcome back to a new video.

2
00:00:02,640 --> 00:00:06,960
I have a new apple here, and these are actually have two images.

3
00:00:07,170 --> 00:00:12,300
This giraffe with a green screen as a background, you see.

4
00:00:12,630 --> 00:00:14,250
And we have this safari.

5
00:00:14,520 --> 00:00:20,880
And in this video, what we will do is we will remove the background, the green background from this

6
00:00:20,880 --> 00:00:27,090
picture and we will overlay of these giraffe on top of this software image.

7
00:00:27,810 --> 00:00:33,120
So the output will be this image with a giraffe here on top.

8
00:00:33,690 --> 00:00:42,210
So what we are doing is we are applying the typical green screen removal technique, which is typical

9
00:00:42,210 --> 00:00:44,010
in TV or other medium.

10
00:00:44,250 --> 00:00:47,430
And basically it works like this or in studio.

11
00:00:47,430 --> 00:00:52,380
They have a green screen on the back of van in post-processing.

12
00:00:52,530 --> 00:01:00,690
What they do is they replace those green pixels, which are very unique compared to the other pixels

13
00:01:00,690 --> 00:01:02,190
of the scene.

14
00:01:02,310 --> 00:01:05,400
And so they replace them with another image.

15
00:01:05,640 --> 00:01:08,130
So the output is something like this here.

16
00:01:08,130 --> 00:01:16,800
Below the rule here is that there shouldn't be any other green pixels except those of a green screen.

17
00:01:16,800 --> 00:01:23,460
So the person here shouldn't be dressed in green because the software has to distinguish has to remove

18
00:01:23,460 --> 00:01:24,480
all the green pixels.

19
00:01:24,480 --> 00:01:29,550
So if it removed the pixels of this dress, then they have.

20
00:01:29,550 --> 00:01:35,700
These backgrounds also be somewhere in here and not where it was supposed to be.

21
00:01:35,730 --> 00:01:37,200
So help you get the idea.

22
00:01:37,620 --> 00:01:38,580
That's what we're doing here.

23
00:01:38,580 --> 00:01:45,690
We are removing these green pixels and then this is a background where the giraffe will be placed.

24
00:01:45,930 --> 00:01:46,740
So let's do that.

25
00:01:48,620 --> 00:01:55,910
So I imported open Stevie and I created two image objects to the foreground image, which is a giraffe.

26
00:01:56,120 --> 00:02:02,030
So if I'm right, I loaded the giraffe, the JPEG image, and then I have another image of the background,

27
00:02:02,030 --> 00:02:05,300
which corresponds to the software that JPEG image.

28
00:02:05,960 --> 00:02:13,090
Now I told you that's the software, which is Python in this case has to remove those green pixels.

29
00:02:13,100 --> 00:02:19,130
So therefore the software has to know what is the value of those pixels.

30
00:02:19,430 --> 00:02:22,610
So let's find that out by printing out, perhaps.

31
00:02:23,060 --> 00:02:24,650
So we have a giraffe, right?

32
00:02:25,010 --> 00:02:28,760
Let's try to print out a pixel from somewhere in here.

33
00:02:29,360 --> 00:02:32,020
So let's try foreground.

34
00:02:32,270 --> 00:02:36,220
Let's print the Pixel four fully.

35
00:02:37,190 --> 00:02:42,020
So it should be somewhere near the corner somewhere here, I suppose.

36
00:02:47,250 --> 00:02:49,680
And so this is the value of that pixel.

37
00:02:49,890 --> 00:02:58,470
So the green pixels have these value 28 for reds to 55 for green and 76 for blue.

38
00:02:58,680 --> 00:02:59,890
So which makes sense.

39
00:02:59,910 --> 00:03:05,370
So I think this kind of color has this combination of colors, so it's not 100 percent green.

40
00:03:05,820 --> 00:03:13,740
It's not only 255 for green and zero for rates and zero for blue, it's somehow bluish.

41
00:03:14,430 --> 00:03:15,840
Anyway, that's not our job.

42
00:03:16,080 --> 00:03:22,830
Our job is that we know now what pixels we should remove and we are ready to go further and iterate

43
00:03:22,830 --> 00:03:24,990
through full ground.

44
00:03:26,070 --> 00:03:31,320
So for AI in what do we iterate through here?

45
00:03:31,620 --> 00:03:35,730
Well, I think we should iterate through range with.

46
00:03:37,190 --> 00:03:46,900
What is with well, with could be a variable we creates here equals to foreground shape.

47
00:03:47,510 --> 00:03:48,110
One.

48
00:03:49,100 --> 00:03:51,650
So which is let me print it out.

49
00:03:53,220 --> 00:03:59,120
Foreground that shape gives us this year.

50
00:03:59,300 --> 00:04:06,500
So for a is the height of the image eight fifty two is the width.

51
00:04:06,590 --> 00:04:07,460
Let's check that.

52
00:04:07,920 --> 00:04:08,170
Yeah.

53
00:04:08,180 --> 00:04:13,040
So this is the height for 80 and this is the width.

54
00:04:14,690 --> 00:04:15,440
Therefore.

55
00:04:16,500 --> 00:04:20,820
If we apply the index one, we get that value.

56
00:04:21,480 --> 00:04:23,500
So that's the width, right?

57
00:04:24,600 --> 00:04:26,190
That's uncommon that.

58
00:04:28,950 --> 00:04:40,170
What I am doing now here is that I am iterating over all the columns of the image so range width I can

59
00:04:40,170 --> 00:04:43,320
print out, I just to see what we get.

60
00:04:45,140 --> 00:04:45,800
So.

61
00:04:48,680 --> 00:04:53,360
We are iterating AIDS 51 times.

62
00:04:54,050 --> 00:04:55,820
So 851 times.

63
00:04:58,320 --> 00:05:08,460
Actually, 852, but this starts from zero, so this range is an object that starts from zero and goes

64
00:05:08,460 --> 00:05:14,820
up to eight hundred fifty one, so it tests eight hundred fifty two elements.

65
00:05:14,970 --> 00:05:20,730
That means we will iterate 850 times over the width.

66
00:05:21,900 --> 00:05:28,510
Right now, what do we do in each direction for each iteration, then?

67
00:05:29,370 --> 00:05:36,930
So let's say this is the first iteration somewhere in here, and in that situation, we iterate again

68
00:05:37,230 --> 00:05:40,440
for j in range.

69
00:05:41,970 --> 00:05:42,510
Height.

70
00:05:44,510 --> 00:05:50,390
So hides is full ground about sheep zero.

71
00:05:51,920 --> 00:05:53,930
So we have a nested loop here.

72
00:05:55,330 --> 00:06:00,430
Now we can extract each pixel in this iteration, foreground.

73
00:06:02,440 --> 00:06:03,030
J.

74
00:06:03,690 --> 00:06:10,270
I so that's it gives us the current pixel of the image.

75
00:06:11,140 --> 00:06:11,810
So, J.

76
00:06:11,830 --> 00:06:13,540
I not I g.

77
00:06:14,440 --> 00:06:17,590
Now let's see what this pixel prints out.

78
00:06:17,980 --> 00:06:19,120
So prints pixel.

79
00:06:20,460 --> 00:06:30,420
And you'll see that we get a long list of pixels, so it's still being printed out every single pixel

80
00:06:30,420 --> 00:06:38,550
of the image, which is basically it's around, I think it was four three nine, the width times eight

81
00:06:38,550 --> 00:06:40,560
fifty one.

82
00:06:40,560 --> 00:06:44,880
So that's a lot of pixels if you multiply these two.

83
00:06:45,090 --> 00:06:53,730
So there's a total amount of pixels and know that we have access to this pixel to the current pixel.

84
00:06:54,030 --> 00:07:05,850
We can write a loop, such as if pixel is equal to the list to eight to fifty seventy six.

85
00:07:06,150 --> 00:07:09,090
So that's towards the green pixel, right?

86
00:07:09,360 --> 00:07:10,890
So the loop and it's here.

87
00:07:11,430 --> 00:07:21,150
So if the current pixel is that pixel is a green pixel, let's say if the current loop is a this iteration

88
00:07:21,630 --> 00:07:23,400
and that is equal to that.

89
00:07:23,910 --> 00:07:30,340
In that case, what we do is we access the foreground image.

90
00:07:30,410 --> 00:07:35,870
So the giraffe we axes sell the pixel with these coordinates.

91
00:07:35,870 --> 00:07:41,550
So G i the current pixel and we change that pixel to something else.

92
00:07:41,670 --> 00:07:44,340
So this will modify the current pixel.

93
00:07:45,770 --> 00:07:50,580
We change that to back ground, J.

94
00:07:50,710 --> 00:08:00,730
I so for example, if the current Pixel is this one in here, somewhere here, then we assign that pixel,

95
00:08:00,740 --> 00:08:04,180
the new value, the new value is background J.

96
00:08:04,190 --> 00:08:09,590
I, which is something like this sky here, right?

97
00:08:10,490 --> 00:08:11,360
And that's happens.

98
00:08:11,360 --> 00:08:18,260
That replacement happens only when we're iterating over a green pixel, right?

99
00:08:18,290 --> 00:08:19,250
I hope that is clear.

100
00:08:19,700 --> 00:08:26,690
And then all that's left to do is go outside of the loop and say, CV two does.

101
00:08:26,690 --> 00:08:27,950
I'm right.

102
00:08:29,690 --> 00:08:37,190
Assign a new name for the ultimate image or the that back, and so we want to write.

103
00:08:37,760 --> 00:08:40,130
So guess what we want to write here?

104
00:08:40,130 --> 00:08:47,180
The foreground or the background image object, I think, is a foreground because it's the foreground

105
00:08:47,180 --> 00:08:48,380
that we are changing.

106
00:08:48,380 --> 00:08:51,410
We are replacing the green pixels with those pixels.

107
00:08:51,740 --> 00:08:54,440
So the foreground is written in that file.

108
00:08:54,830 --> 00:09:01,700
Let's run and fix a few errors that we will get because this is still not ideal.

109
00:09:02,300 --> 00:09:07,600
So I have to go down, down, down, down on my terminal.

110
00:09:07,970 --> 00:09:10,970
So this is a first error in this line.

111
00:09:10,970 --> 00:09:13,100
15 here.

112
00:09:15,080 --> 00:09:21,140
So the true value of an area with more than one element is ambiguous.

113
00:09:23,470 --> 00:09:31,150
What's happening here is the Pixel is actually a non-priority, so if you print out here.

114
00:09:32,020 --> 00:09:35,230
Print type of pixel.

115
00:09:38,820 --> 00:09:45,610
We're going to see that this is a non-priority, but then we are comparing that's not for a with a python.

116
00:09:45,930 --> 00:09:49,680
This is a play in Python loose and you cannot do that.

117
00:09:49,690 --> 00:09:55,290
So one workaround is to convert this pixel into a list.

118
00:09:55,800 --> 00:09:58,410
So this nampai array we converted into a list.

119
00:09:58,920 --> 00:10:05,520
Therefore, this thinks you know this is not binary that will be converted into something like this,

120
00:10:05,790 --> 00:10:07,560
at least with this Komen's.

121
00:10:07,950 --> 00:10:10,920
And that means this is comparable now.

122
00:10:11,010 --> 00:10:12,270
So we can run the script.

123
00:10:13,580 --> 00:10:15,200
Oh, we got this printed out.

124
00:10:15,290 --> 00:10:22,850
So let me remove that print function, comment it out because it takes a lot of resources and we don't

125
00:10:22,850 --> 00:10:23,360
want that.

126
00:10:23,810 --> 00:10:27,300
So Ron, again, you see it's running.

127
00:10:27,300 --> 00:10:27,620
No.

128
00:10:29,200 --> 00:10:30,970
It should take a few seconds.

129
00:10:32,650 --> 00:10:33,520
Yeah, it's finished.

130
00:10:34,090 --> 00:10:35,260
And let's check the altitude.

131
00:10:35,980 --> 00:10:36,530
OK.

132
00:10:37,090 --> 00:10:41,650
So something is working, but it's not ideal, but we can fix that.

133
00:10:41,860 --> 00:10:43,300
So we have two problems here.

134
00:10:43,310 --> 00:10:49,720
The first problem is obvious we still have these green areas around the giraffe.

135
00:10:49,900 --> 00:10:50,770
Why is that?

136
00:10:51,100 --> 00:11:01,420
Well, that is because not all the green here has a pixel value of twenty eight to fifty five and six.

137
00:11:01,720 --> 00:11:10,600
Some of the pixels, such as those who are very close to the giraffe so near its boundaries they will

138
00:11:10,600 --> 00:11:17,500
have a slightly different green, which is a bit interpolated with a giraffe.

139
00:11:17,710 --> 00:11:19,690
Brownish colors here.

140
00:11:20,560 --> 00:11:29,620
So this in other words, this pixels here are not the same as these ones in here, and our script here

141
00:11:29,620 --> 00:11:31,340
is very strict.

142
00:11:31,690 --> 00:11:38,320
So the conditional is only comparing those that particular pixel color.

143
00:11:38,410 --> 00:11:39,660
So that is one problem.

144
00:11:39,670 --> 00:11:47,680
The other problem then, is if you see the original image, we have more zebras here, but the output

145
00:11:48,250 --> 00:11:50,380
looks a bit zoomed in.

146
00:11:50,620 --> 00:12:01,630
So what's OpenCV did is that it tried to fit the original image, which has a small size into this bigger

147
00:12:01,630 --> 00:12:02,640
size image.

148
00:12:02,650 --> 00:12:10,780
So basically it zoomed out in the center and then placed this image so we can fix that to.

149
00:12:10,900 --> 00:12:15,190
Let's fix these issues for the Pixel issue, the green pixel issue.

150
00:12:15,580 --> 00:12:21,010
What you could do is you could see an MP for Nampai, which needs to be important to you.

151
00:12:21,280 --> 00:12:25,780
So import nonparty s and P so MP, that's all.

152
00:12:27,060 --> 00:12:28,560
And then you say pick, so.

153
00:12:29,650 --> 00:12:30,730
Is equal to that.

154
00:12:31,720 --> 00:12:33,070
And close the parentheses.

155
00:12:33,580 --> 00:12:35,140
Actually, any better?

156
00:12:35,500 --> 00:12:36,730
I'll explain your way.

157
00:12:37,240 --> 00:12:38,560
So I ran the script.

158
00:12:41,500 --> 00:12:46,120
And let's check the output, so now it's slightly better.

159
00:12:47,280 --> 00:12:53,760
We still have lost some pixels from the giraffe ears here.

160
00:12:55,640 --> 00:13:00,140
But I think it's still better than it was before.

161
00:13:00,740 --> 00:13:09,740
We can also improve this by seeing instead of, let's just say, one two five zero, for example, you

162
00:13:09,740 --> 00:13:14,060
can play around with different pixel values and see what works best.

163
00:13:14,360 --> 00:13:22,820
So what I just did here is that I put a more typical green because this and PND, what it checks is

164
00:13:22,820 --> 00:13:30,080
that it checks if the current Pixel has any value similar to this pixel.

165
00:13:30,320 --> 00:13:38,600
So for example, if the current Pixel has a value of hundreds here but it has 255 for green, then this

166
00:13:38,600 --> 00:13:43,550
will evaluate to true this expression, so it's less restrictive.

167
00:13:43,820 --> 00:13:45,500
Let's check the output now.

168
00:13:45,500 --> 00:13:53,630
It's much better and would say unless you are an image processing critic, you wouldn't notice that

169
00:13:53,630 --> 00:14:00,770
this giraffe doesn't belong to this group of zebras and this other fellow giraffe.

170
00:14:00,950 --> 00:14:05,270
So it's quite a good balance, but you can do better by experimenting with these values.

171
00:14:05,600 --> 00:14:13,610
Now, the next problem was that this image is bigger, but this one here is just a portion of the background.

172
00:14:13,610 --> 00:14:15,650
The image we can fix that boy.

173
00:14:17,420 --> 00:14:27,680
Using Siri, the true size, and we resize the background image to the width and height values of the

174
00:14:27,680 --> 00:14:28,700
foreground image.

175
00:14:29,090 --> 00:14:37,670
In other words, the background image will have the same size as the foreground image, so let's plays

176
00:14:37,670 --> 00:14:43,550
that into a variable resi resized background is equal to that.

177
00:14:44,300 --> 00:14:48,470
And then here we want to play with the resized backgrounds.

178
00:14:51,280 --> 00:15:00,130
Image so the current pixel off in the foreground, if it's green, it is replaced with the current pixel

179
00:15:00,140 --> 00:15:02,110
of the background image.

180
00:15:02,740 --> 00:15:03,370
Wrong, that's.

181
00:15:06,370 --> 00:15:07,480
And check the ultimate.

182
00:15:08,450 --> 00:15:11,030
And now I think we get what we wanted.

183
00:15:11,540 --> 00:15:17,210
So the giraffe was there, the safari was there, and this is the ultimate and this is our script.

184
00:15:18,290 --> 00:15:23,090
Thanks a lot for following this video, and let's move forward with more videos.

185
00:15:23,390 --> 00:15:23,750
See you.

