Return to Video

Bluescreen

  • 0:01 - 0:06
    For this section we're going to take the
    techniques we've built up so far, with the
  • 0:06 - 0:10
    for-loop and the if-statement and the
    average technique to detect color areas,
  • 0:10 - 0:15
    and put them together to do a real movie
    special effect. So this special effect is
  • 0:15 - 0:19
    called blue screening. And I should point
    out that what you think of as video data
  • 0:19 - 0:24
    is really just made of a series of regular
    still images, just showed something like
  • 0:24 - 0:28
    twenty to 60 times per second. So for our
    purposes we'll just do the special effect
  • 0:28 - 0:33
    on still images, and you can see how it'll
    generalize to movies. So the idea is it'll
  • 0:33 - 0:38
    be a very simple model. Imagine I have the
    stop sign image. So we've, we've talked
  • 0:38 - 0:42
    about the code to detect the red area
    inside of here. So for the blue screen
  • 0:42 - 0:46
    effect, we're gonna have a second image
    which I'll call the, the background image.
  • 0:46 - 0:50
    And the idea is, we'll detect the red
    areas, as we've done before. But whenever
  • 0:50 - 0:54
    I get a pixel over here on the red area, I
    wanna think about the sort of
  • 0:54 - 0:59
    corresponding pixel over from this other
    image. And I'm gonna copy that pixel over
  • 0:59 - 1:04
    to sorta fill in all the red area over
    here with pixels over here from the other
  • 1:04 - 1:08
    image. Sorry, it's a slightly more
    detailed diagram of it. So, I'll do
  • 1:08 - 1:12
    examples in a little bit. But we're going
    to have a for-loop where we're kind of
  • 1:12 - 1:16
    looping through this image. And we'll have
    an if-statement which is going to identify
  • 1:16 - 1:21
    some of the pixels up here. So let's say
    we've identified this pixel in the left
  • 1:21 - 1:25
    image. So that pixel has some x-y-coordinate and we haven't messed with the
  • 1:25 - 1:29
    x-y very much up to now but it, you know,
    maybe x is 200 and y is 100 or whatever.
  • 1:29 - 1:33
    So what we want to do is get those two
    numbers. And then find the corresponding
  • 1:33 - 1:38
    pixel over here from the other image. So,
    maybe a (200,100), or whatever it is.
  • 1:38 - 1:43
    There's some pixel that matches up. So
    once we've got that corresponding image, a
  • 1:43 - 1:48
    corresponding pixel from the other image.
    Then we wanna copy it over, to set the
  • 1:48 - 1:53
    pixel over here. And you know, how's that
    going to work, how do you copy a pixel
  • 1:53 - 1:57
    over? You have to think about, 'Well, what
    defines a pixel? What makes a pixel look
  • 1:57 - 2:01
    the way it looks?' And the answer is it's
    just the three numbers. It's just red,
  • 2:01 - 2:05
    green and blue. So we're going to write
    code to sort of get the red value from
  • 2:05 - 2:09
    this pixel over here and then set that as
    the red value over here. Likewise for
  • 2:09 - 2:13
    green and blue. And by setting all those
    three numbers over we're going to make the
  • 2:13 - 2:17
    pixel over on this image just look exactly
    the same as the pixel over here on this
  • 2:17 - 2:22
    side. So, here's some code, here's our
    first code example to do this. And rather
  • 2:22 - 2:27
    than sorta describe the effect, I think in
    this case I'll just run it so we can see
  • 2:27 - 2:32
    it, and then I'll talk about what the
    lines do. So, here we see the sorta as
  • 2:32 - 2:38
    outlined before, the stop sign with all
    its all the red areas of the stop sign.
  • 2:38 - 2:43
    We've substituted in pixels from the leaves
    image instead. So let me point out the,
  • 2:43 - 2:49
    the, how the parts of this work. So first
    off In all of examples up to now, I think
  • 2:49 - 2:53
    we've just opened one image, but it turns
    out you can have multiple images. So here
  • 2:53 - 2:57
    I open a second image, for the leaves.jpeg, and I store that in a variable
  • 2:57 - 3:01
    called back. So then all this code we've
    seen before. I'm just looping over the
  • 3:01 - 3:06
    stop sign and detecting the red pixels. So
    the interesting action is here inside of
  • 3:06 - 3:11
    the if-statement. What do we do when we
    have a red pixel. And it sorta breaks down
  • 3:11 - 3:17
    to three parts. So these first two lines
    just call pixel.getX() and pixel.getY(), and
  • 3:17 - 3:22
    store the resulting values in variables x
    and y. Now I don't think I've used getX()
  • 3:22 - 3:27
    and getY() up to now, but what tho-, what
    those do is just go to a pixel and
  • 3:27 - 3:32
    retrieve whatever its x, y is out of it,
    so very analogous to getRed() and getBlue().
  • 3:32 - 3:38
    So I'm just gonna store those in these
    variables x y. And then on this line. I
  • 3:38 - 3:43
    take those two numbers, x and y. Actually
    I'll sorta read it left to right here. So
  • 3:43 - 3:47
    I go to the back image, so that's the, the
    leaves image. And I call getPixel, so I
  • 3:47 - 3:52
    wanna retrieve a pixel out of it. And then
    I have to give it some x, y, and in this
  • 3:52 - 3:57
    case the x and y I wanna use is the x, y
    that was set right here. So essentially,
  • 3:57 - 4:01
    it's the x, y of the pixel from the stop
    sign image, saying, whatever that x, y is,
  • 4:01 - 4:07
    go get that s, the pixel at the same x, y
    from the leaves image. So. Once we've got
  • 4:07 - 4:12
    that pixel, then I'm just going to start
    another variable called pixel2. So
  • 4:12 - 4:16
    a natural question would be like, oh well
    couldn't we just call that pixel? Well we
  • 4:16 - 4:20
    can't call it pixel, because we're already
    using pixel to refer to the pixels from
  • 4:20 - 4:24
    the stop sign image. So, pixel2 just
    seemed like sorta the most obvious other
  • 4:24 - 4:29
    name to use. All right, so at this point,
    I've got pixel2 and pixel2 refers to
  • 4:29 - 4:34
    the pixel from the background image, from
    the other image. And now, these three
  • 4:34 - 4:39
    lines do what I was describing before as a
    copy. So let's just look at the first one.
  • 4:39 - 4:43
    So it says pixel.setRed. So we've
    seen that, like, a 100 times. I'm gonna set
  • 4:43 - 4:48
    the red value of the pixel from the stop
    sign image. And what, what am I gonna set
  • 4:48 - 4:53
    that red value to be? And what I'm gonna
    set it to be, is, pixel2.getRed().
  • 4:53 - 4:58
    So I'm getting the pixel from the other
    image, getting it's red value. So, 160, or
  • 4:58 - 5:02
    whatever it is. And I'm just gonna set
    that into the stop sign image. So
  • 5:02 - 5:07
    repeating that for green and blue, we're
    just, essentially just, we're copying the
  • 5:07 - 5:12
    three numbers over. So, in effect, this
    copies the pixel. So, this is fairly
  • 5:12 - 5:17
    complicated. I want to do a few examples
    of this. So let me do a second example.
  • 5:17 - 5:22
    This is a picture of baby Abby, when she's
    like six months old. Happy little baby on
  • 5:22 - 5:28
    her bouncy chair. And later when she's a
    teenager she can be mad at me for using
  • 5:28 - 5:33
    this example. So what I'm going to do is,
    or, what I want to do in this case is,
  • 5:34 - 5:39
    notice the green areas of the bouncy
    chair. I want to kind of copy over the
  • 5:39 - 5:45
    leaves so we get this kind of nature baby
    leafy effect. And, as, as I recall, there
  • 5:45 - 5:51
    were basically two things that I needed to
    do in the loop here. Well first I should
  • 5:51 - 5:57
    point out, so I'm, I'm going to call pixel.getGreen(), for the, the if-test. So
  • 5:57 - 6:02
    the first problem was this. Get the, get
    pixel2. Get the corresponding pixel.
  • 6:02 - 6:07
    And here I'm going to write it just as one
    line. So I say, back - so I go to the other
  • 6:07 - 6:12
    image - back.getPixel. And I'll sort
    of space it out here. I want to get the
  • 6:12 - 6:17
    pixel from the other image, and then I
    have to specify the x, y. And here what I
  • 6:17 - 6:21
    want to do is, well, I wanna specify the
    x, y, of the pixel from the stop sign
  • 6:21 - 6:26
    image. And previously I did that by having
    x, y variables, and well, that's fine. But
  • 6:26 - 6:30
    I'm, in this case I'm gonna compact it
    down to just do it in one line. So really
  • 6:30 - 6:35
    I can just say, pixel.getX(). So pixel is
    the pixel from the stop sign image. And
  • 6:35 - 6:40
    I'll just call getX() and that gets the
    x-value and then I'll just, I'll put that
  • 6:40 - 6:46
    directly here inside of. The back.getPixel
    call. So this it's the same idea as before
  • 6:46 - 6:51
    but I've just compacted it down to one
    line. All right, so that one line sets
  • 6:51 - 6:56
    pixel2 to be the corresponding pixel.
    And then the second thing we need to do in
  • 6:56 - 7:02
    the loop is this copy over operation, and
    that is just literally the same so I'll
  • 7:02 - 7:09
    just, I'll copy that from up here.
    Alright. So now get rid of the blank.
  • 7:09 - 7:15
    These blank lines are harmless. Alright,
    so let's try, let's try that. Alright. So
  • 7:15 - 7:22
    you can see we have, we're getting a
    little bit of the leaf data in here but
  • 7:22 - 7:29
    it's not it's a little thin. So we need to
    make this a little smaller. .4 it's a
  • 7:29 - 7:46
    little bit more, I want more. .2. Just a little bit more and.
  • 7:46 - 7:54
    Too much. [laugh] All right. So let's try
    1.05. Too much for me. Okay, I think
  • 7:54 - 7:58
    that's pretty good. So you could see, it's
    sort of, you know, there was green arrows
  • 7:58 - 8:02
    over here. There's sort of the shadow area
    that's, I mean, maybe just barely
  • 8:02 - 8:05
    greenish. And then there was some green
    blankets over here that we sort of
  • 8:05 - 8:09
    sprinkled these leaves in. So it's, it's a
    nice effect. Or, it's sort of eerie,
  • 8:09 - 8:13
    right, that we've gotten these pixels from
    the leaf image, and sort of put them into
  • 8:13 - 8:17
    this other image, and it looks, well, not
    totally realistic. But you could see
  • 8:17 - 8:21
    where, with tuning, you could get this
    real effect. Alright, so let's try one
  • 8:21 - 8:29
    like the movies. So here is our movie star,
    monkey. And I've taken a picture of monkey
  • 8:29 - 8:34
    in front of a blue background. It's just a
    blue towel. You'll see that monkey is
  • 8:34 - 8:39
    brown and has kind of a light tummy here
    and here, this banana is sort of a light
  • 8:39 - 8:45
    yellow. So if you think of brown and tan
    and yellow, those are all kind of. On near
  • 8:45 - 8:49
    yellow, right? So there's a lot of
    red/green used to make this up. Not a lot
  • 8:49 - 8:53
    of blue. So separating the monkey from the
    blue background, in terms of, thinking of
  • 8:53 - 8:57
    it in terms of RGB, is gonna work pretty
    well. So here's what I wanna use as the
  • 8:57 - 9:02
    background. This is a candidate for one of
    the most famous pictures ever taken. This
  • 9:02 - 9:06
    is a picture taken by the Apollo 8
    astronauts as they were, they were in
  • 9:06 - 9:10
    orbit. They were flying around the moon,
    and as they came around here's, the Earth
  • 9:10 - 9:15
    loomed. Sort of showing the Earth just
    over the horizon of the moon. Emphasizing it's
  • 9:15 - 9:20
    just a little lifeboat we're all on. So
    what I want to do is start with the monkey
  • 9:20 - 9:25
    image, and for all these blue pixels, I
    want to pull in, pixels from the moon
  • 9:25 - 9:32
    image. And so it'd look like the monkey is
    like, on vacation on the moon. And so I'll
  • 9:32 - 9:40
    just grab. This code to get started. Let's
    see. Okay, so what I wanna do is, for the
  • 9:40 - 9:47
    if, I wanna say if getBlue(). So I wanna
    detect the blue background right? And if
  • 9:47 - 9:54
    it's the blue background, then I wanna
    pull it over pixels from the, the moon. So
  • 9:54 - 10:00
    here our image is monkey back is moon.jpg I've got my if- statement. And
  • 10:00 - 10:04
    then, this code is unchanged,
    right? It's just, get the corresponding
  • 10:04 - 10:08
    from, it's unchanged from the earlier
    example. So just get the corresponding
  • 10:08 - 10:13
    pixel, copy over red, green, blue. So
    that, that requires no change at all.
  • 10:13 - 10:18
    Alright, so let's try it. Alright, so it,
    at 1.5 what's happening here is the if
  • 10:18 - 10:24
    statement is never firing. I've specified,
    I've made the hurdle too high. Okay so
  • 10:24 - 10:33
    let's try 1.3. Oh, just a little teeny
    hunt. So I'm too high. Let's try 1.1. Huh,
  • 10:33 - 10:38
    okay. So now you can see, you know, for s,
    the bluishness of this part of the blue
  • 10:38 - 10:44
    background was maybe just a little bit
    more, so we're getting that, but not down
  • 10:44 - 10:49
    here. So I need to try 1.0. Now it's
    getting there. We have a little bit less,
  • 10:49 - 10:55
    so actually we can go under we can go
    under 1 here so I'll try 0.9 as the
  • 10:55 - 11:00
    hurdle where I'm just lowering the hurdle.
    Ooh, that's pretty good. See, there's a
  • 11:00 - 11:06
    little teeny bit of moon on his chin
    there. 0.95. There we go, that's pretty
  • 11:06 - 11:10
    good. We could, we could tweak this
    [inaudible], I think, three, there's a
  • 11:10 - 11:16
    little bit of, there we go. I think that's
    perfect. So, you can see that's, you know,
  • 11:16 - 11:23
    movie star in laser battles, spaceship,
    whatever, like. All right. So just to
  • 11:23 - 11:27
    kinda summarize. In the code you're gonna
    have two images instead of one and then
  • 11:27 - 11:31
    you loop through the main image and just
    kinda the way we've done before,
  • 11:31 - 11:36
    identifying areas of some color. And then,
    once you've identified an area that's in
  • 11:36 - 11:41
    the color you care about, then there's
    this operation to locate the corresponding
  • 11:41 - 11:45
    pixel from the other image, and then copy
    over its red, green, blue values to, to
  • 11:45 - 11:50
    get the effect done. And as you can
    imagine. You know, it's pretty easy to
  • 11:50 - 11:52
    make up exercises that work on this
    technique.
Title:
Bluescreen
Video Language:
English
duvin1 edited English subtitles for Bluescreen
duvin1 edited English subtitles for Bluescreen
duvin1 edited English subtitles for Bluescreen
stanford-bot edited English subtitles for Bluescreen
stanford-bot edited English subtitles for Bluescreen
stanford-bot added a translation

English subtitles

Revisions