< Return to Video

Grayscale

  • 0:00 - 0:06
    In this section, I'm gonna talk about how
    to make grayscale images and code to
  • 0:06 - 0:12
    carry out that idea. So the first question
    is how do you make a gray color, so to ex,
  • 0:12 - 0:18
    clear off that, I'm gonna go over here to
    the RGB explorer and it turns out in the
  • 0:18 - 0:23
    RGB scheme, the way to make a gray color,
    one that lacks any hue, is for all three
  • 0:23 - 0:29
    numbers, red, green and blue to be the
    same. So I can demonstrate that here, I
  • 0:29 - 0:34
    can put red and green. Up to be, the I'll
    put them all at 190. So you can see that's
  • 0:34 - 0:39
    kind of, you know fairly bright yellow.
    And blue is down here. So watch what
  • 0:39 - 0:44
    happens as I drag blue up towards being
    the same as red and green you can see the
  • 0:44 - 0:49
    color is kind of drained out of it and
    just as blue gets to be exactly the same
  • 0:49 - 0:54
    you can actually use the arrow keys in
    these controls. There once, now that all
  • 0:54 - 0:59
    three are exactly a 190 what were
    left with is just grey. So we can, you
  • 0:59 - 1:04
    know, I'll do another example. So, so if I
    put, I'll put green and blue down here
  • 1:04 - 1:09
    sort of at the middle so they're both at
    99. It's kind of a, I don't know, a dark
  • 1:09 - 1:15
    turquoise, if I put right at zero. So then
    I'll bring red up to be closer and closer.
  • 1:15 - 1:20
    And then once it is the same as the other
    two then, again, I'm left with gray. In
  • 1:20 - 1:25
    this case it's kind of a, a dark gray. So
    the pattern is, if red, green, and blue
  • 1:25 - 1:31
    values are all equal, they're all the
    same, then that's gonna be it, some shade
  • 1:31 - 1:37
    of gray. And actually, the, our original
    cases of pure black and pure white, I'll
  • 1:37 - 1:43
    make black here. They follow that too,
    right. And black it's 0,0,0. They're all the
  • 1:43 - 1:49
    same, so that's one endpoint of the, of
    the, the grayscale spectrum. So I'll go
  • 1:49 - 1:54
    back here and I'll, we'll say that, you
    know, so we can make these arg-, if we put
  • 1:54 - 1:59
    all, the RGB values to be equal, that'll
    make a shade of gray. And we can make dark
  • 1:59 - 2:04
    gray and light gray and black or white or
    whatever. We'll say that those, those
  • 2:04 - 2:08
    shades of gray. Another way of saying it
    is that they lack hue. So they're not
  • 2:08 - 2:12
    towards red or green or anything like
    that. They're just this colorless
  • 2:13 - 2:17
    brightness. So in this table I just have
    some examples. So for example if I had
  • 2:17 - 2:22
    something that was 50, 50, 50, that's sort
    of a dark gray. Or 120, 120, 120, that's
  • 2:22 - 2:27
    medium gray, or 200, 200, 200 that'd be a
    light gray. As I said before the pure
  • 2:27 - 2:32
    black and pure white cases that we talked
    about before, those, those fit th-, this
  • 2:32 - 2:37
    pattern as well. Alright. So how can I do
    something useful with this? So I'm gonna
  • 2:37 - 2:42
    look at this unusual image here. The, the
    liberty-red jpeg. And I'll just run this
  • 2:42 - 2:46
    code. Right now, there's no code in here,
    so we'll just. We'll just see it natural.
  • 2:46 - 2:51
    So the, the liberty-red image here it's,
    it's a picture of the Statue of Liberty.
  • 2:51 - 2:56
    But all of the data is in the red values
    of the pixels. So the red values are, you
  • 2:56 - 3:01
    know, 37 or 200, or whatever. They're,
    they're varying to show this image. The
  • 3:01 - 3:06
    green and the blue values are just zero
    everywhere. There's nothing there. So, I
  • 3:06 - 3:11
    mean, whatever. It, it looks bad, or it
    certainly looks wrong. So I wanna think
  • 3:11 - 3:17
    about, well, how could I fix that? I'd
    like to fix this to look like a, a
  • 3:17 - 3:23
    grayscale picture of Statue of Liberty, not
    this, not this red thing. So, the way to
  • 3:23 - 3:29
    do this. What I'm, I'm going to use this
    the fact that if the green, blue, and red
  • 3:29 - 3:34
    are all the same, that's going to be
    shaded grey. Now in this case, the data is
  • 3:34 - 3:41
    in. For each pixel is in the red value. So
    if I say, pixel.setGreen. And what
  • 3:41 - 3:47
    I'm gonna do is I'm gonna make the green
    value, I'm gonna change it to be the same
  • 3:47 - 3:51
    as the red value. So I'll do that by
    saying, pixel.getRed here. So what
  • 3:51 - 3:56
    this does. Is with the pixel.getRed
    that sort of picks the number out of for,
  • 3:56 - 4:01
    you know, for each pixel. It picks up the
    red value. So it's 27 or 100 or whatever
  • 4:01 - 4:06
    it is. And then sets it in to be the green
    value. Now this is a little unusual.
  • 4:06 - 4:12
    Usually what we call setGreen we've mixed
    it with getGreen and setBlue we've mixed
  • 4:12 - 4:17
    with getBlue. But this is a valid
    combination as well and it happens in this
  • 4:17 - 4:23
    case it does what I want. So then I'll do
    it again for blue. Whoops. So, I will say
  • 4:23 - 4:31
    pixel dot getRed. And then I'll set that
    into blue. I'll neaten this up, now let's
  • 4:31 - 4:35
    try that. Oh, there we go. So, this is
    just an application of this feature of the
  • 4:35 - 4:39
    RGB space that, when the numbers are
    equal, it's grayscale. And so I, I sort
  • 4:39 - 4:44
    of retrieve the value out of the red, and
    I set it over to blue and green. And so
  • 4:44 - 4:49
    now, it, it sort of fixes the image, so it
    looks, It's black and white, but at least
  • 4:49 - 4:55
    it looks okay. So a related question. Or
    maybe a more practical question is, well,
  • 4:55 - 5:00
    if I've got a color image. Like, here's,
    here's our old flowers image. How could I
  • 5:00 - 5:05
    convert it to gray scale? And, the way I'm
    gonna think about that, is, you know, if
  • 5:05 - 5:09
    we're looking here at these flowers, it's,
    like, well. I wanna drain the hue out it.
  • 5:09 - 5:14
    I just wanna think of each one of these
    pixels as being. Dark, or, dark or light.
  • 5:14 - 5:19
    Just having some amount of brightness, and
    obviously, there's, you know, there's many
  • 5:19 - 5:23
    examples in this, in this image. So
    suppose, so the problem is gonna be
  • 5:23 - 5:27
    looking at a pixel, how dark or light is
    it? That's what I wanna [inaudible],
  • 5:27 - 5:32
    reduce this down to. So suppose I, I
    picked three pixels. Out of that image and
  • 5:32 - 5:37
    I put them in this table and now I want to
    think about which one of these pixels is
  • 5:37 - 5:41
    darkest and lightest. So here's the first
    pixel and here's the second pixel and
  • 5:41 - 5:46
    here's the third pixel. And here, I'll
    zoom in on this a little bit. So I'm just
  • 5:46 - 5:51
    trying to judge light versus dark. Who's
    the, what's the lightest, what's the
  • 5:51 - 5:56
    darkest here? So suppose, if I was just
    looking at the red values. I'd see that
  • 5:56 - 6:01
    this first pixel has a red of 200. And
    then that's, that's just much brighter
  • 6:01 - 6:05
    than the others, right? I mean, high
    numbers are bright, 255 is the max. And
  • 6:05 - 6:10
    zero is black, so. It looks like, oh yeah,
    this first pixel. 200, that's clearly the
  • 6:10 - 6:15
    brightest. But then, if I look over. In
    the at the green value. And then it's
  • 6:15 - 6:20
    like, oh gosh, but this third pixel
    actually has a really high value for green
  • 6:20 - 6:25
    even though it's kinda low for red. So I,
    I'm not quite sure how that balances
  • 6:25 - 6:30
    against the other pixels. But it's hard
    because what it, essentially what we see
  • 6:30 - 6:35
    is that you can't just look at one of red,
    green, blue to judge how bright the thing
  • 6:35 - 6:40
    is, you sorta want to count them all. So,
    our solution in this case, which I have in
  • 6:40 - 6:46
    the, the fourth column here, is to compute
    the average for each pixel. So each pixel
  • 6:46 - 6:51
    has a red, green and blue value. What I'm
    gonna do is just compute the average of
  • 6:51 - 6:56
    those three numbers, and the way to do
    that. Is you just add'em up, so I'm just
  • 6:56 - 7:02
    gonna compute red plus green plus blue for
    each pixel, and then just divide by three.
  • 7:02 - 7:07
    So that'll give us the average value of
    the red, green and blue. And that average
  • 7:07 - 7:12
    value is gonna work as a pretty nice
    summary of just how bright the pixel is.
  • 7:12 - 7:17
    Right, so that the average is zero or ten
    or twenty, some low number. We don't know
  • 7:17 - 7:21
    what hue it is exactly, but we know it's
    dark. And at the other end, if the average
  • 7:21 - 7:26
    is 220 or 240, again we don't know what
    the hue is but, but we know it's bright.
  • 7:26 - 7:30
    So the average is going to work as a kind
    of a summary of the pixel, where it drops
  • 7:30 - 7:35
    out the hue and just gives us one number,
    zero to 255, that just captures the
  • 7:35 - 7:39
    brightness. Now I should say, there's
    other ways of doing this but just
  • 7:39 - 7:43
    computing the average is simple and it's
    fine. It wor-, it's gonna work fine for
  • 7:43 - 7:47
    our purposes. So in this case, I, I had
    these three pixels and so here on the
  • 7:47 - 7:52
    right hand side I just compute the
    average. And what we see is, really the
  • 7:52 - 7:57
    third one is by far the brightest. If you,
    you know look at all three of red, green
  • 7:57 - 8:01
    and blue, and then the middle one is the
    darkest and the first one is kind of
  • 8:01 - 8:08
    medium. Alright. So I can use this. This
    idea that I've got the average, and it
  • 8:08 - 8:14
    sort of makes this nice one number
    summary. I can use this to, convert
  • 8:14 - 8:22
    something to, gray. So let's try this. So
    here I've got the, I'll just run this. So
  • 8:22 - 8:27
    in, inside the loop there's no code here
    so, if I just run it now we just see the
  • 8:27 - 8:32
    image unchanged. So I'd like to do is add
    the code here. To change this to grayscale.
  • 8:32 - 8:37
    So I should say what my strategy is
    going to be. So what I'm going to do is in
  • 8:37 - 8:41
    the loop for each pixel I'm going to
    compute this average number; so just get
  • 8:41 - 8:46
    one number. So it might be 27 or 100 or
    whatever for each pixel. And then I'm
  • 8:46 - 8:51
    gonna take that number, and set it into
    the red, green, and blue. So if the
  • 8:51 - 8:56
    average is 27, I'm gonna make red, green
    and blue all be 27. And if the average is
  • 8:56 - 9:02
    211, then I'm gonna make red, green, and
    blue all be 211. So that, that converts
  • 9:02 - 9:07
    each pixel into the, the grayscale
    spectrum we saw before. So first off I
  • 9:07 - 9:12
    have to, compute, the average here. So I'm
    just gonna. As I said, add'em up.
  • 9:12 - 9:18
    So I'll say pixel.getRed() + pixel.getGreen() +
    pixel.getBlue(). And
  • 9:18 - 9:24
    then I'm going to put those three inside
    of this outer set of parentheses just for
  • 9:24 - 9:29
    the order of operations. So I want to do
    the addition and then I'll say slash
  • 9:29 - 9:34
    three. So I'll divide by three. So this
    does the addition in the parentheses. And
  • 9:34 - 9:39
    having gotten that sum, divide by three.
    And I'm just going to store that in a
  • 9:39 - 9:44
    variable called avg. And this line, it's
    gonna turn out to be kind of a stock line
  • 9:44 - 9:48
    for us. There's, there's many little
    techniques or problems we might wanna do
  • 9:48 - 9:53
    in the future, where we wanna compute the
    average, and it, inevitably, the line
  • 9:53 - 9:57
    would come out like that. Alright, so
    what'd I say? So my strategy is, I compute
  • 9:57 - 10:03
    the average, and then I set red, green,
    and blue to all use that. So
  • 10:03 - 10:09
    I'm gonna say, pixel.setRed. And
    then here, I've stored the average. This
  • 10:09 - 10:15
    is just a variable. So I've stored the
    number in there. And then, inside here, so
  • 10:15 - 10:21
    I can just say avg. I'll just go to that
    variable, and get the number back out. And
  • 10:21 - 10:27
    likewise, I can say, setGreen(avg) and
    pixel.setBlue(avg), okay, so let's
  • 10:27 - 10:32
    try that. Very good. So you can see, this
    is it works. So it's gone through all
  • 10:32 - 10:37
    these pixels, you know, red, green or
    whatever. It's reduced it down to this one
  • 10:37 - 10:42
    average brightness number and then set
    that back. So we get this nice sorta gray
  • 10:42 - 10:49
    scale. So actually. Having tested it on
    flowers, I'm gonna go back to...
  • 10:49 - 10:54
    The poppy image which I think appears in
    one of the exercises. So here if I comment
  • 10:54 - 10:58
    these three lines out and just run it
    right now we'll, we'll just see the poppy
  • 10:59 - 11:03
    image, there it is. So we'll scroll to the
    right here a little bit. So you can see
  • 11:03 - 11:08
    that it's obviously, you know, it's got
    this sorta orange California poppy look
  • 11:08 - 11:13
    and the faded background here. So now I'll
    put these lines back. And we can try our
  • 11:13 - 11:18
    oops, algorithm on that one. There we go,
    that works very nicely. So you can see,
  • 11:18 - 11:24
    obviously the, the orange and the green
    it's all been drained out and each pixel
  • 11:24 - 11:28
    has been reduced to just, just a
    brightness. And I like here how the, the
  • 11:28 - 11:34
    texture on the front of the of the poppy
    is still, still kinda visible. So, there
  • 11:34 - 11:41
    you have it, suitable for framing.
    Alright. So a, a question that often comes
  • 11:41 - 11:49
    up. Looking at this code. Is. That line,
    average equals, and then this formula for the
  • 11:49 - 11:55
    average. Does that need to be inside the
    loop? It, it, it, it feels like, perhaps,
  • 11:55 - 12:01
    it could be just here. Up, up, up after
    the, the image equals sign. And the answer is.
  • 12:01 - 12:07
    No. It can't be up there, it does need to
    be inside the loop. And the reason is.
  • 12:07 - 12:14
    What you could think. That this line sort
    of sets up a relationship about average
  • 12:14 - 12:20
    that, must be true for all time like as if
    its just true at all times as the program
  • 12:20 - 12:26
    runs. And that is not how a computer code
    works. A computer code is less
  • 12:26 - 12:32
    sophisticated than that. What this line
    does with equals, is it just evaluates the
  • 12:32 - 12:37
    right hand side. And assigns it into this
    variable on the left hand side when this
  • 12:37 - 12:42
    line is run. So if we were to put this up
    at the top it would just run once and it
  • 12:42 - 12:48
    would store some number in avg and that
    would be it. In this case remember we're
  • 12:48 - 12:53
    inside of this for-loop here. Let me kinda
    highlight the body. And so this code it
  • 12:53 - 12:57
    running thousands or maybe millions of
    times, once for each pixel. And those
  • 12:57 - 13:02
    pixels each have different red, green, and
    blue values. Alright, so this sum. Is
  • 13:02 - 13:07
    different thousands of times. And so what
    we need is to compute this sum anew for
  • 13:07 - 13:12
    each pixel. Each time we see a new pixel,
    we need to redo this math. And so, the way
  • 13:12 - 13:17
    the equals sign works is it's, it's, it's
    just evaluated when the computer runs
  • 13:17 - 13:22
    across it. And so for that reason, because
    each, each one of these pixels is
  • 13:22 - 13:27
    different, we, we'll, we need to put it,
    very often we'll just put it as the first
  • 13:27 - 13:32
    line inside the for. Just compute
    the average. So that's why, that's why
  • 13:32 - 13:38
    that needs to be there. Alright. So, just
    to kinda summarize. So we've learned this
  • 13:38 - 13:42
    quality that, if the red, green, and blue,
    red, green, and blue are all equal, that's
  • 13:42 - 13:45
    a shade of gray. And we've got this
    technique where we could compute the
  • 13:45 - 13:49
    average. And that just gives us this one
    brightness number, basically, from zero to
  • 13:49 - 13:53
    255. And that's gonna be, I'm using it here
    for grayscale. But actually, in the
  • 13:53 - 13:57
    future, we're gonna use that for some
    other stuff. It's just a, a useful thing
  • 13:57 - 14:01
    to know. And, finally I'll say that this
    line, which I was highlighting before,
  • 14:01 - 14:06
    average equals add the three up and divide
    it by three. That's gonna be kind of a
  • 14:06 - 14:11
    stock line, and so I'm gonna, we're gonna end
    up using exactly that line later, and in
  • 14:11 - 14:13
    fact it should show up in some exercises.
Title:
Grayscale
Video Language:
English
duvin1 edited English subtitles for Grayscale
duvin1 edited English subtitles for Grayscale
duvin1 edited English subtitles for Grayscale
duvin1 edited English subtitles for Grayscale
duvin1 edited English subtitles for Grayscale
duvin1 edited English subtitles for Grayscale
stanford-bot edited English subtitles for Grayscale
stanford-bot edited English subtitles for Grayscale
Show all

English subtitles

Revisions