Return to Video

If Logic

  • 0:00 - 0:05
    So one theme in computers is this idea of
    bulk operation. So we have this ...
  • 0:05 - 0:09
    right now with the for-
    loop. So you can write a little bit of
  • 0:09 - 0:14
    code, and the computer takes care of
    running that code thousands or millions of
  • 0:14 - 0:18
    times across some big data set. So another
    theme is, this idea of logic or
  • 0:18 - 0:22
    selectivity. So, for this, we're gonna be
    able to write a test, which is true or
  • 0:22 - 0:27
    false, and then use that to control if a
    particular piece of code runs or not. And
  • 0:27 - 0:32
    so, that, in JavaScript is done with a
    thing called the if-statement, and that'll
  • 0:32 - 0:36
    be the subject of this section. Combining
    the if-statement, with the for-loop gives us
  • 0:36 - 0:41
    a very powerful combination. Both the sort
    of reach to cover a lot of data, and the
  • 0:41 - 0:45
    selectivity to sorta pick exactly what we
    want to do with, different parts of data.
  • 0:45 - 0:50
    So that'll work. That'll allow us to work
    on many interesting problems. So here's the
  • 0:50 - 0:56
    syntax of an if-statement shown in the
    context of a for-loop. So, it's right here.
  • 0:56 - 1:02
    So, it starts with the word if. And then,
    that's followed by a part called the test,
  • 1:02 - 1:06
    which appears inside of parentheses. And
    we're actually gonna spend most of our
  • 1:06 - 1:11
    energy sort of playing around with the
    test. In this case, the test says, if
  • 1:11 - 1:16
    pixel.getRed is greater than 160, So
    for now, our tests will, will pretty much
  • 1:16 - 1:20
    have this form. Basically, comparing one
    number, versus another number. In this
  • 1:20 - 1:25
    case, I'm using greater than or the other
    one. The one going the other way is the
  • 1:25 - 1:30
    less than. So following the, so what this
    does is, obviously, just tests, for this
  • 1:30 - 1:35
    pixel, is the red value over 160 or not?
    So that, that's just true or false. The
  • 1:35 - 1:40
    way the if-statement works is that the
    test is followed by a set of curly braces,
  • 1:40 - 1:46
    just like for the for-loop. And inside
    the curly braces, I'm gonna call that the
  • 1:46 - 1:51
    "then" code. And the "then" code could just be
    any lines of code we want. So the way
  • 1:51 - 1:55
    the if-statement works, is that when it
    runs, it evaluates the test. And the
  • 1:55 - 1:59
    tests can be, essentially for each
    pixel, this test will be either true or
  • 1:59 - 2:03
    false. If it's true, the if-statement then
    goes ahead and runs the "then"-code,
  • 2:03 - 2:08
    whatever that says to do. If the test is
    false, then the "then"-code is just skipped.
  • 2:08 - 2:13
    So the if statement works as kind of a
    switch. To just either enable or disable,
  • 2:13 - 2:18
    the code, inside the, inside the "then"-block here. So that, this pattern of having
  • 2:18 - 2:23
    the outer loop look at all the pixels, and
    then inside of it putting an if-statement
  • 2:23 - 2:28
    to sort of control what we're going to do
    in certain cases, that's going to work,
  • 2:28 - 2:32
    to solve, many interesting situations. So,
    the, first problem I'll look at here. Is
  • 2:33 - 2:39
    suppose, is the, the stop sign. So suppose
    I wanna take this stop sign and I wanna
  • 2:39 - 2:45
    change all of the red in the sign to be
    blue, so it just looks like it looks like
  • 2:45 - 2:51
    a stop sign from Mars. So my first attempt
    at this is. Gonna write the test. I'll
  • 2:51 - 2:58
    just, my test will be if pixel.getRed is
    greater than 160. So that's just checking
  • 2:58 - 3:04
    if the red value is kind of large. And, so
    that's here. And I can do whatever I want
  • 3:04 - 3:10
    in the "then"-code here. In this case
    I'll just set red and green to zero and
  • 3:10 - 3:15
    blue to 255, so that will give us that
    kind of blue appearance. Let's just try
  • 3:15 - 3:19
    that. So if I run it. So you can see
    it's... It kind of works. The red parts of
  • 3:19 - 3:24
    the sign have been certainly obliterated
    to look all blue. And then we've got some
  • 3:24 - 3:28
    areas over here and areas over here as
    well. I should say this technique is, it's
  • 3:28 - 3:32
    an okay technique but we're going to
    refine it to come up with something a
  • 3:32 - 3:37
    little bit better. One thing I want to
    show is that this 160 here, there's
  • 3:37 - 3:41
    nothing really magic about that value. For
    code like this, you would expect to sort
  • 3:41 - 3:46
    of play around with different values and
    sort of adjust it so it looks the way you
  • 3:46 - 3:52
    want. So let's say I wanna... I wanna cover,
    I wanna have less blue here. Then what I
  • 3:52 - 3:56
    would want is to make this test more
    restrictive. Now, the way you can think
  • 3:56 - 4:00
    about it, is that, it's going through
    thousands of pixels, and for each one,
  • 4:00 - 4:05
    it's checking the red value. And, in this
    case, to say that the red value is over
  • 4:05 - 4:09
    160, it's sort of a hurdle. It's like, it
    has to get over the 160. So if I were to
  • 4:09 - 4:14
    put a bigger number here, like, let's say,
    200. And I run that. Well that's not much
  • 4:14 - 4:19
    difference. Alright, lets try even bigger.
    How about 240. I run that. There we go.
  • 4:20 - 4:25
    You see what's happened is I've made the if, the if-test more restrictive. Sort of,
  • 4:25 - 4:30
    fewer pixels get by. And if I make it up
    to 250. Really raise that thing. Then all,
  • 4:30 - 4:34
    the great majority of the pixels don't get
    past the if-statement. There's just a few
  • 4:34 - 4:39
    remaining pixels scattered around here
    that are, red enough to get past that
  • 4:39 - 4:43
    test. So it can go the other way too. So
    instead of 160, I could go down to like,
  • 4:43 - 4:47
    let's say 100. I'm gonna lower the, the
    hurdle. In that case, then. Yeah, we know.
  • 4:47 - 4:52
    Way to, many more pixels than I want end
    up making the test. So that's kind of a
  • 4:52 - 4:56
    typical step for a lot the, the problems
    we're gonna look at where we'll have some
  • 4:56 - 5:00
    num-, we'll, we'll have some structure we
    want and then we'll be left with some
  • 5:00 - 5:05
    number that we kinda tweak up or down a
    little bit to, to get it the way we want.
  • 5:05 - 5:10
    The other thing I can show here,
    I'll set this back to 160 is the code, the
  • 5:10 - 5:14
    "then"-code could just do whatever. Now it's
    under the control of the if-test. Here it
  • 5:14 - 5:19
    just sets it to blue but I could just as
    easily say well, let's set red, green, and
  • 5:19 - 5:24
    blue all to zero. And if I run that, we
    get a sort of a black stop sign instead. So
  • 5:24 - 5:30
    we'll, we'll use that sort of creativity
    later to put different sorts of code in
  • 5:30 - 5:36
    the "then"-section. Alright, so scroll back
    here, so this kinda works, I'm, I'm
  • 5:36 - 5:40
    getting the red sign. But I'm also
    getting, it turns out I'm getting all the
  • 5:40 - 5:44
    white parts of the sign as well. This, our
    test effectively gets both red and white.
  • 5:44 - 5:49
    And you can see that most clearly if you
    just scroll back to see what the sign
  • 5:49 - 5:53
    looked like originally, right? It's got
    these red areas, but it's also got,
  • 5:53 - 5:58
    obviously, these big white letters. And
    we're just getting all of that. And. If I
  • 5:58 - 6:02
    look at this test again, you can sorta see
    why that is. Well, I'm checking if just,
  • 6:02 - 6:06
    if red is greater than 160. And so when
    does that happen? Well, one time that
  • 6:06 - 6:10
    happens is for a red pixel, you know, a
    pixel that we think of as reddish. Where
  • 6:10 - 6:14
    the red value's gonna be high, and the
    green and the blue will be kind of low. So
  • 6:14 - 6:19
    it'll be t-, let's say red is 200. It'll
    be true then. But what about the white
  • 6:19 - 6:24
    pixels? So, a white pixel might have
    values like, maybe, red, green, and blue
  • 6:24 - 6:30
    are all 220. All three numbers are high.
    And because my test just looks at red, and
  • 6:30 - 6:35
    says, is it greater than 160? It's gonna
    trip for the, the white pixels as well.
  • 6:35 - 6:40
    Both the white and the red pixels for both
    of those this test will appear true. It
  • 6:40 - 6:44
    fails to distinguish those cases and
    that's exactly what's happening here. The
  • 6:44 - 6:49
    red areas and the white areas both have
    red values of high, that are high. And so
  • 6:49 - 6:54
    this test just... It's not, it's not quite
    distinguishing in those cases. So, we want
  • 6:54 - 6:59
    to do a little bit better. So I'm going to
    build up a better strategy that we could
  • 6:59 - 7:05
    use to solve this problem. So to get
    started I want to make sort of a graphical
  • 7:05 - 7:09
    view. Of a pixel. Suppose I've got three
    pixels. And each pixel obviously is going
  • 7:09 - 7:14
    to have red, green and blue values. And so
    suppose what I did, is I made this little
  • 7:14 - 7:18
    bar chart. So here's the pixel on the
    left, and here's the pixel in the middle,
  • 7:18 - 7:23
    and here's the pixel on the right. And all
    I've done is, I've just graphed, the red,
  • 7:23 - 7:27
    green and blue values. And so, for any one
    of these, I could ask, well, for that
  • 7:27 - 7:32
    pixel. Which color predominates? What sort
    of cast does it have? So here for this
  • 7:32 - 7:37
    pixel, I'd say it's red. You can see the
    red just sticks up the most. For this
  • 7:37 - 7:42
    middle pixel I would say blue. It has kind
    of a blue cast. And here this pixel, even
  • 7:42 - 7:48
    though the pixel itself is much darker, I
    would say that it has a greenish cast. In
  • 7:48 - 7:52
    all three of those cases what's going on
    is. I think intuitively, we're kinda
  • 7:52 - 7:57
    looking at the three bars and asking well
    which one pokes above the other two. And
  • 7:57 - 8:03
    that, that is a reasonable way of thinking
    about well what color cast does that have?
  • 8:03 - 8:10
    So, I'll remind you, sort of have to bring
    in here, in last section we had this idea
  • 8:10 - 8:15
    of the average of a-, of a pixel, so the
    average was going to be, averaging the
  • 8:15 - 8:21
    red, green, and blue values to come up
    with sort of an average between those
  • 8:21 - 8:25
    three. So I can combine that. With my
    little bar chart here. Where for this
  • 8:25 - 8:29
    first pixel, I could take the red, green,
    and blue values and average them and I
  • 8:29 - 8:33
    could think of that as sort of a line. And
    the line is going to be kind of in the
  • 8:33 - 8:38
    middle of the heights of the bars. Because
    it is right? It's going to be shorter than
  • 8:38 - 8:42
    the tallest one and taller than the
    shortest one it's going to be kind of in
  • 8:42 - 8:46
    the middle somewhere. So with the average.
    It, it allows us to pursue the earlier
  • 8:46 - 8:49
    strategy, but now it's a little more
    concrete. We'd say, like, well, right.
  • 8:49 - 8:54
    This first one is reddish, and we see that
    the red bar is poking above the average,
  • 8:54 - 8:58
    and the other two aren't. In this middle
    one, we see that the blue bar is poking
  • 8:58 - 9:02
    above the average. And here, here we see
    the green bar is poking above the average.
  • 9:02 - 9:06
    So, that just gives a kind of. A little
    intuition about how it is I could decide
  • 9:06 - 9:12
    what cast a color has. The nice thing
    about the average is it's just a number.
  • 9:12 - 9:17
    It's just a single number. And so it's
    gonna be pretty easy to incorporate into
  • 9:17 - 9:22
    an if test. So for example, I, I could
    write the tests. If pixel.getRed is
  • 9:22 - 9:26
    greater than average. Right, getRed is a
    number. Average is a number. And then, if
  • 9:26 - 9:31
    you look at this left pixel, it's going to
    be true. Yeah, the red is greater than the
  • 9:31 - 9:36
    average. And for these other two pixels,
    it's going to be false. In both cases, the
  • 9:36 - 9:40
    red is below the average. And so, this
    suggests sort of, the strategy I'm going
  • 9:40 - 9:46
    to pursue. For detecting what cast a pixel
    has by first computing its average, and if
  • 9:46 - 9:51
    I wanna look for red or green or whatever
    comparing that to the average to decide if
  • 9:54 - 9:56
    that cast is present. And this is
    gonna work pretty well. So let me revisit
  • 9:56 - 10:03
    my stop sign problem. And now what I'm
    going to do is for the test, as I was
  • 10:03 - 10:09
    describing earlier, I'm going to write the
    test as pixel.getRed, greater than
  • 10:09 - 10:13
    average. And then, actually, I'm gonna
    multiply times, sort of, an adjustment
  • 10:13 - 10:17
    factor. I'm just using the average, just
    by itself, is, really is absolutely what
  • 10:17 - 10:22
    we want. We want some sort of a number
    that we can tweak. So here's, here's the,
  • 10:22 - 10:27
    here's the real code down. So I'm, I'm
    looping over the whole image. As in the
  • 10:27 - 10:31
    last section, I'm gonna just compute the
    average between the red, green, and blue,
  • 10:31 - 10:35
    so I can use that for my comparisons. And
    then here, I'm gonna write the if-test,
  • 10:35 - 10:39
    as, as, I've said. Is the red greater than
    the average, and then, this is sort of the
  • 10:39 - 10:43
    adjustment factor that we'll play with.
    Alright, so let's try that. All right. So
  • 10:43 - 10:48
    what we see is, I'm, we're getting the
    sign pretty nicely, but, you know, there's
  • 10:48 - 10:53
    way too much extra blue over here. This is
    just, we have to fix the adjustment
  • 10:53 - 10:58
    factor. So I'm just going to try, making
    the hurdle a little higher. So if I say
  • 10:58 - 11:03
    1.3 here, make it a little higher, and
    that cuts it down. We still have a little
  • 11:03 - 11:09
    bit over here. I could go up to, 1.8. Make
    the hurdle much higher. Okay well you see
  • 11:09 - 11:16
    I get the sign but I, I lose this 4-way
    thing here. Apparently those are slightly
  • 11:16 - 11:22
    different shades of red, so I'll go down
    to, let's try, 1.4. There. I think, I think
  • 11:22 - 11:26
    1.4 looks pretty good. Right, where
    there's a little bit of blue here, a
  • 11:26 - 11:32
    little bit of extra coloring over there
    that I'm gonna tolerate, but it seems like
  • 11:32 - 11:36
    in terms of the sign, we've dialed in on
    the red areas pretty nicely. So. So that
  • 11:36 - 11:43
    shows this technique, working pretty well.
    I should just show that this factor, you
  • 11:43 - 11:49
    could also try numbers less than one, so I
    could try like 0.9 and then it's like
  • 11:49 - 11:55
    super impressive. So I'll put this back to
    1.4. There we go. That's nice. That's dialed in on the sign.
  • 11:55 - 12:01
    Alright, so let me try a second example of
    this technique. I want you to imagine that
  • 12:01 - 12:07
    you're visiting Stanford sometime and you
    park here and you get a parking ticket.
  • 12:07 - 12:13
    Now philosophically we know it's best to
    sort of take in the world as it has
  • 12:13 - 12:19
    actually happened. Don't think too much
    about what you wish had happened. However
  • 12:19 - 12:25
    in this case, we're gonna try and fix it,
    fix history in code. So what I wanna do is
  • 12:25 - 12:31
    take this picture and fix the red curb to
    not be red anymore, to be gray. So here's
  • 12:31 - 12:36
    the code for that. We'll try different
    variants of this. So I've got a curve.jpg
  • 12:36 - 12:41
    is this image. I'm going to loop
    through all the pixels and compute the
  • 12:41 - 12:47
    average. And so now we're left with. The
    test. So I'll say if pixel.getRed is
  • 12:47 - 12:53
    greater than average. Whatever here, we'll
    start with a factor of 1.5. And as my
  • 12:53 - 13:00
    first attempt at this problem, what I'm
    gonna to do is set the pixels to just sort
  • 13:00 - 13:06
    of a middle gray, so 120, 120, 120. We'll
    just sort of paint gray on there. Alright,
  • 13:06 - 13:12
    so let's try that. Alright so what we see
    is, we're getting too, too little gray
  • 13:12 - 13:16
    here. So when you're adjusting one of
    these if, if-tests, you always wanna
  • 13:16 - 13:21
    think, well, do you wanna make it more or
    less restrictive? In this case, I wanna
  • 13:21 - 13:26
    make it less restrictive, right? I wanna
    get the grey going along here. So I'm
  • 13:26 - 13:34
    gonna make the hurdle lower. So I'll try
    1.4. Hm, that's getting there, let's try
  • 13:34 - 13:45
    1.3. Yeah keep going, how about 1.1. Oh,
    too far. Okay, 1.2. Alright, so
  • 13:45 - 13:52
    this does a pretty good job. I've, I've
    identified the red curb over here, and,
  • 13:52 - 13:56
    like, put this sort of flat 120-120-120
    gray on there. If you look carefully, I'm
  • 13:56 - 14:01
    also catching a little bit of parts of the
    plant that I guess had a little bit of red
  • 14:01 - 14:06
    cast. And actually, over here on the right
    hand side, there's these plants that I
  • 14:06 - 14:10
    guess had sort of a red cast. And I'm, I'm
    clobbing them up with this, right? With
  • 14:10 - 14:15
    this gray. So this is, you know, it's
    pretty good. But what it says in the
  • 14:15 - 14:20
    problem statement here, is, actually, for
    part (b). What I'd like to do is instead of
  • 14:20 - 14:26
    just changing this to 120-120-120 I wanna
    use the technique from the previous
  • 14:26 - 14:31
    section, where I say "Well, look, let's
    take the red parts of this image, and
  • 14:31 - 14:37
    change just those parts to be grayscale."
    Previously I did an example where I would
  • 14:37 - 14:42
    change an entire image to grayscale. Now
    I'm using the if-test to just pick out
  • 14:42 - 14:46
    certain regions of the image and just
    change them to grayscale. And it just turns
  • 14:46 - 14:51
    out, this is gonna, this'll look better.
    Beac.. All of this. Let's just see. So
  • 14:51 - 14:56
    remember the algorithm to change a pixel
    to grayscale, is to first compute the
  • 14:56 - 15:01
    average for that pixel, which actually I'm
    already doing here. And then with that
  • 15:01 - 15:06
    average number in hand then set the red,
    green and blue values of the pixel to all
  • 15:06 - 15:11
    be that average. It happens for this code
    that's a very small change, instead of
  • 15:11 - 15:20
    saying setRed(120). I'll just say setRed(avg), and likewise setGreen(avg) and setBlue(avg).
  • 15:20 - 15:29
    Let's try that. There we go. So you can
    see here, I've mapped right, sorta put
  • 15:29 - 15:35
    gray. On here. And it looks a lot better.
    Because, really, this red curb does have a
  • 15:35 - 15:41
    pattern of light and dark on it. And by
    changing each pixel to be gray, but we're
  • 15:41 - 15:47
    still respecting the light and dark. So
    that's why, that's why it looks better
  • 15:47 - 15:52
    here. Also notice the plants, over on the
    right hand side here, that looked so bad
  • 15:52 - 15:56
    before. Well they were red plants before.
    Now we've changed them to kind of gray
  • 15:56 - 16:01
    plants. But I think, you sort of can't
    tell, like where it looks fine. So I think
  • 16:01 - 16:05
    that's a, you know, I think that's a nice
    solution. All right here. So this just
  • 16:05 - 16:10
    restates, what I was just saying there.
    So, I'd say the conclusion for the section
  • 16:10 - 16:14
    is, mainly, the, just the idea of, of an
    if-statement, that combines a test with
  • 16:14 - 16:18
    some then code under the control of that
    test. And then, I've done a couple
  • 16:18 - 16:24
    examples using this particular technique
    to detect. Pixels of a certain color in an
  • 16:24 - 16:29
    image. So, you know, my standing example
    of pixel.getRed greater than average times
  • 16:29 - 16:34
    some factor and using that's kinda dial in
    on pixels of a certain color. And then
  • 16:34 - 16:39
    finally we've seen that the then code. Can
    sort of do anything. It can change it to
  • 16:39 - 16:44
    blue or change it to red or whatever and
    certainly we'll take advantage of that
  • 16:44 - 16:46
    flexibility for solving lots of problems.
Title:
If Logic
Video Language:
English
duvin1 edited English subtitles for If Logic
duvin1 edited English subtitles for If Logic
duvin1 edited English subtitles for If Logic
duvin1 edited English subtitles for If Logic
duvin1 edited English subtitles for If Logic
duvin1 edited English subtitles for If Logic
duvin1 edited English subtitles for If Logic
duvin1 edited English subtitles for If Logic
Show all

English subtitles

Revisions