< Return to Video

10.6: Pixel Neighbors - Processing Tutorial

  • 0:02 - 0:03
    Ok, so here's another important aspect
  • 0:03 - 0:05
    about image processing that we left out
  • 0:05 - 0:07
    in the previous video.
  • 0:07 - 0:08
    (clears throat)
  • 0:08 - 0:09
    So what did we do?
  • 0:09 - 0:12
    We had, an image of a frog
  • 0:12 - 0:16
    we processed every single pixel and we
  • 0:16 - 0:22
    copied that pixel to our processing display
    window and maybe we manipulated it while
  • 0:22 - 0:25
    copying it, we made it brighter
    we changed it's color, we did all sorts of things.
  • 0:26 - 0:30
    This is, I would say, pixel point processing.
  • 0:30 - 0:33
    What we are doing is there's a one to one
  • 0:33 - 0:35
    relationship, for this pixel
  • 0:36 - 0:38
    change this pixel
  • 0:38 - 0:39
    for the next pixel, change this pixel
  • 0:40 - 0:41
    for the next pixel, change the next pixel.
  • 0:42 - 0:45
    However, most sophisticated image processing
  • 0:45 - 0:47
    algorithms that you might find in
  • 0:47 - 0:48
    not to keep using photoshop as an example
  • 0:48 - 0:51
    photoshop or a ** image processing.
  • 0:51 - 0:54
    actually process goups of pixels
  • 0:54 - 0:55
    for example,
  • 0:55 - 0:58
    let's say this is my frog
  • 0:58 - 0:59
    pixel
  • 1:00 - 1:01
    my frog image
  • 1:01 - 1:03
    and this here is my processing window,
  • 1:04 - 1:06
    and let's say this is the pixel I want to
  • 1:06 - 1:08
    set here in my processing window.
  • 1:08 - 1:11
    What if inorder to set this pixel
    I actually
  • 1:11 - 1:15
    used a whole bunch of neighbouring pixels
  • 1:15 - 1:17
    around that pixel, so that pixel
  • 1:17 - 1:18
    is over here
  • 1:18 - 1:22
    I don't know what am drawing here exactly
    umm but I actually took all of the
  • 1:22 - 1:27
    pixels in five by five matrix around it and
    this is the roo.. the color of this pixel
  • 1:27 - 1:30
    is a function of all of these pixels
    combined.
  • 1:30 - 1:34
    Ahh aah, a classic image processing
  • 1:34 - 1:35
    algorithm that you might
  • 1:35 - 1:36
    have used before is a blur.
  • 1:36 - 1:38
    What if you take a pic.. an image
  • 1:38 - 1:40
    and blur it making it fuzzier,
  • 1:40 - 1:43
    less sharp, less in focus.
  • 1:43 - 1:46
    Well actually if you average all of the pixels
  • 1:46 - 1:47
    of an area
  • 1:47 - 1:49
    to set a particular pixel, that's a
  • 1:49 - 1:51
    blur algorithm.
  • 1:51 - 1:53
    It is making each pixel
  • 1:53 - 1:54
    more like it's neighbours
  • 1:54 - 1:57
    almost ahh bluring it, averaging it,
  • 1:57 - 1:58
    smoothing it between pixels.
  • 1:58 - 2:00
    So this is a actually an algorithm
  • 2:00 - 2:01
    and sharpening it is the opposite
  • 2:01 - 2:03
    taking each pixel and making it more
  • 2:03 - 2:04
    different than it's neighbours.
  • 2:05 - 2:09
    Umm another example
    of an algorithm is finding the edges
  • 2:09 - 2:13
    in an image.
    So if I have a silhouette of a person
  • 2:13 - 2:14
    (chuckles)
  • 2:14 - 2:15
    this looks nothing like a person
  • 2:15 - 2:16
    and I don't know..
  • 2:16 - 2:17
    some weird ghost alien thing
  • 2:17 - 2:20
    and I wanna find the edges of this person
  • 2:20 - 2:22
    well let's say this person was wearing
  • 2:22 - 2:24
    a black shirt,
  • 2:24 - 2:26
    much like the.. my black shirt here
  • 2:26 - 2:27
    how would I find the edges?
  • 2:27 - 2:28
    Well, if this is
  • 2:28 - 2:30
    if you're looking at an image of me
  • 2:30 - 2:31
    the pixels that are very different
  • 2:31 - 2:33
    the white pixels and these black pixels
  • 2:33 - 2:35
    a pixel that's very different
  • 2:35 - 2:37
    from it's neighbours is an edge right?
  • 2:37 - 2:37
    (finger taps his microphone twice)
  • 2:37 - 2:39
    this pixel over here is very similar to
  • 2:39 - 2:40
    all it's neighbours.
  • 2:40 - 2:41
    This pixel right here is quite different
  • 2:41 - 2:43
    than it's neighbours it must be
  • 2:43 - 2:44
    on an edge.
  • 2:44 - 2:45
    So, finding the edges of an image
  • 2:45 - 2:47
    has to do with looking for pixels that
  • 2:47 - 2:48
    are very different from their neighbours.
  • 2:48 - 2:50
    Let's actually see if we can,
  • 2:50 - 2:51
    implement this really really quickly
  • 2:51 - 2:53
    in our frog example.
  • 2:53 - 2:53
    (taps to switch cameras)
  • 2:53 - 2:56
    So if I come over here, to the frog
  • 2:56 - 3:01
    and if I look at this froooooog
  • 3:01 - 3:03
    and what where we doing here?
  • 3:03 - 3:05
    We were ahh kinda doing this threshold
  • 3:05 - 3:07
    thing, that's where we last were.
  • 3:07 - 3:10
    Well, let me say, let's aah
  • 3:10 - 3:11
    take all this out and
  • 3:11 - 3:13
    let's take all this out.
  • 3:13 - 3:17
    Here is a particular pixel location,
  • 3:17 - 3:19
    what if I want to look at this pixel
  • 3:19 - 3:22
    relative to the pixel to it's right
  • 3:22 - 3:24
    or to it's left, or above or below.
  • 3:24 - 3:26
    Well, what will that pixel be this is
  • 3:26 - 3:30
    location1 another location might be
  • 3:30 - 3:35
    (x+1) times y times width
  • 3:35 - 3:37
    and if you look at this what is
  • 3:37 - 3:38
    the difference here?
  • 3:38 - 3:42
    Now am saying, let me look at pixel x + y
  • 3:42 - 3:45
    xy and let me look at pixel x+1, y
  • 3:45 - 3:49
    right? Here ahh if am over here and then
  • 3:49 - 3:50
    am in some image
  • 3:50 - 3:53
    if this is pixel x - oh you can't see that.
  • 3:53 - 3:58
    If this is pixel x, y then this right here
  • 3:58 - 4:00
    right next to it is pixel x+1, y
  • 4:00 - 4:02
    so ofcourse a more sophisticated algorithm
  • 4:02 - 4:06
    migh look at x+1 and x-1 and y+1 and y-1
  • 4:06 - 4:08
    x+2 and y+2 aloot of pixels
  • 4:08 - 4:10
    in the neigboring area but just to
  • 4:10 - 4:12
    start really simply let's look at this pixel
  • 4:12 - 4:14
    and the one next to it.
  • 4:14 - 4:14
    (taps to switch camera)
  • 4:14 - 4:17
    So if I'm looking at this pixel and
  • 4:17 - 4:19
    the one next to it, I can look at both
  • 4:19 - 4:20
    of the brightness of both of those pixels.
  • 4:20 - 4:24
    b1 equals frog.pixels[loc1];
  • 4:24 - 4:29
    b2 equals frog.pixels[loc2];
  • 4:29 - 4:32
    Aaah those cameras are always shutting off
  • 4:32 - 4:33
    am gonna fix that in the middle
  • 4:33 - 4:36
    of this video look I turned black
  • 4:36 - 4:38
    for a second and am back
  • 4:38 - 4:40
    and am just going to fix this one
  • 4:40 - 4:41
    you'll have to bear with me, there's
  • 4:41 - 4:43
    nothing I can do about this right now.
  • 4:43 - 4:45
    ohhh come back (taps twice)
  • 4:45 - 4:48
    Ok, am back I'l edit all that out later
  • 4:48 - 4:52
    ok so now uhh I could get a
  • 4:52 - 4:54
    difference which is maybe am just gonna
  • 4:54 - 4:57
    say the absolute value of (b1-b2)
  • 4:57 - 5:01
    and let's set the pixel of the window
  • 5:01 - 5:04
    equal to that difference
  • 5:04 - 5:08
    noow, oops loc1
  • 5:08 - 5:10
    so loc1 is kind of my core original
  • 5:10 - 5:13
    location so I wanna set that individual
  • 5:13 - 5:15
    pixel but I wanna look at it as a function
  • 5:15 - 5:17
    the value of that pixel as a function of
  • 5:17 - 5:18
    two pixels that are next to each other
  • 5:18 - 5:21
    let's run this and by the way
  • 5:21 - 5:24
    we got this sad, very difficult, really
  • 5:24 - 5:26
    traumatic array out of bounds exception
  • 5:26 - 5:28
    this error is something that will happen
  • 5:28 - 5:30
    to you as soon as you start doing any
  • 5:30 - 5:32
    image processing work. What happened?
  • 5:32 - 5:34
    I tried to look at, investigate,
  • 5:34 - 5:36
    think about access a picel that doesn't
  • 5:36 - 5:39
    exist, because at some point I was at a
  • 5:39 - 5:40
    pixel that was all the way at the edge
  • 5:40 - 5:42
    of my image and I tried to look at
  • 5:42 - 5:43
    pixel one more to the edge
  • 5:43 - 5:45
    which doesn't exist.
  • 5:45 - 5:47
    So there's alot of strategies around this
  • 5:47 - 5:48
    problem for right now am just going to
  • 5:48 - 5:49
    do something which is kind of simple
  • 5:49 - 5:51
    which is say let me just look at all the
  • 5:51 - 5:52
    pixels all the way up to one before the
    edge
  • 5:53 - 5:54
    and that will kind of like deal with this
  • 5:54 - 5:55
    problem but you can think of other things
  • 5:55 - 5:57
    you could have pixels wrap around
  • 5:57 - 5:58
    there's lots of other ways you could
  • 5:58 - 5:59
    approach this problem
  • 5:59 - 6:00
    but now we can see
  • 6:00 - 6:02
    you know this isn't the perfect
  • 6:02 - 6:04
    demonstration but I've kinda really got
  • 6:04 - 6:07
    something here the the frog by the way
  • 6:07 - 6:10
    was had a lot of similar colors so am
  • 6:10 - 6:12
    getting ummm umm
  • 6:12 - 6:14
    actually am not exactly getting
  • 6:14 - 6:16
    what I expected I guess the frog actually
  • 6:16 - 6:18
    didn't have lots of similar colors it's
  • 6:18 - 6:20
    very different cuz we're getting we're
  • 6:20 - 6:22
    getting a very bright color everywhere
  • 6:22 - 6:24
    there. I guess there's a big difference
  • 6:24 - 6:25
    of all the pixels inside the frog
  • 6:25 - 6:26
    and the background is very
  • 6:26 - 6:28
    noisy so there's a couple of things
  • 6:28 - 6:30
    we're noticing here; one - this isn't
  • 6:30 - 6:32
    such a great algorithm am only looking
  • 6:32 - 6:35
    at the pixels to the left coming to the
    right and to the left if I looked at the
  • 6:35 - 6:37
    pixels above and below and diagonally
  • 6:37 - 6:39
    I might be able to uhh determine
  • 6:39 - 6:40
    alot more about the image
  • 6:40 - 6:42
    the other thing is - this particular
  • 6:42 - 6:44
    image has alot of noise in it
  • 6:44 - 6:46
    so I'm am there's so much noise that
  • 6:46 - 6:48
    am not really getting accurate edges
  • 6:48 - 6:50
    maybe blurring the image first and then
  • 6:50 - 6:53
    looking at it looking to the edges might
    help, but in the end am actually
  • 6:53 - 6:54
    visually able to see something here
  • 6:54 - 6:56
    but this wasn't the exact demonstration
  • 6:56 - 6:58
    I was hoping for but uhh
  • 6:58 - 7:00
    to.. look at this, maybe try to
  • 7:00 - 7:02
    oohhh I know what the problem is
  • 7:02 - 7:04
    huuuuuh (sighs deeply)
  • 7:04 - 7:05
    am gonna have to re-record this video
  • 7:05 - 7:08
    but what did I just do here?
    I did the thing that I did originally
  • 7:08 - 7:12
    which is just I looked at the arbitrary
    difference between the giant integer value
  • 7:12 - 7:15
    you're probably all just watching this
    like yelling at the screen like you're
  • 7:15 - 7:17
    doing it wrong, what I wanted to do was
  • 7:17 - 7:18
    look at the brightness of each one of
  • 7:18 - 7:20
    this pixels just forgot like I wanna
  • 7:20 - 7:22
    compare the brightness of each one of
  • 7:22 - 7:24
    these pixels. This is gonna give us
  • 7:24 - 7:27
    something much much more umm
  • 7:27 - 7:30
    oh yeah and it's very hard to see here
  • 7:30 - 7:32
    but actually this really worked.
  • 7:32 - 7:34
    I'm really getting the edges I'm really
  • 7:34 - 7:35
    getting the edges cause the edges of
  • 7:35 - 7:38
    the frog that's where the pixels are most
  • 7:38 - 7:39
    different and infact if I want
  • 7:39 - 7:43
    to be a bit more thoughtful about this
  • 7:43 - 7:45
    I might just come up with some threshold
  • 7:45 - 7:48
    like if the pixel values are at all
  • 7:48 - 7:50
    different then,
  • 7:50 - 7:56
    draw black otherwise draw white
  • 7:56 - 7:58
    and now if I run this, we can see
  • 7:58 - 7:59
    aah I'm really starting to see the
  • 7:59 - 8:01
    contours of this frog. I think this could
  • 8:01 - 8:03
    be a better demonstration. So here
  • 8:03 - 8:05
    we're seeing just a really basic
  • 8:05 - 8:07
    algorithm for looking at the comparison
  • 8:07 - 8:10
    between two pixels and finding the edges
  • 8:10 - 8:13
    now again this was a very simplistic
  • 8:13 - 8:15
    approach most of the time what you wanna
  • 8:15 - 8:18
    do wooah haha all that work and this
  • 8:18 - 8:20
    camera is not even on umm most of
  • 8:20 - 8:21
    the time what you wanna do is look
  • 8:21 - 8:24
    at a whole selection of pixels and in fact
  • 8:24 - 8:25
    this is something that you might not
  • 8:25 - 8:27
    be aware of this is right there bring up
  • 8:27 - 8:29
    photoshop for a second if I go to
  • 8:29 - 8:31
    photoshop and I've got my frog loaded
  • 8:31 - 8:35
    in here and I go to filter ahh ahh ah
  • 8:35 - 8:39
    other custom what I dont..
  • 8:40 - 8:41
    what your actually seeing here
  • 8:41 - 8:43
    is an and this is hard for you to see
  • 8:43 - 8:46
    is this are the weights of a pixel
  • 8:46 - 8:48
    If I wanna make a pixel on the center
  • 8:48 - 8:50
    much more important than the ones in
  • 8:50 - 8:52
    the area, that's a sharpening.
  • 8:52 - 8:55
    I want the pixel to be more different
  • 8:55 - 8:56
    than it's neighbours I actually ahh
  • 8:56 - 9:00
    repr.. ahh oops I created very quickly a
  • 9:00 - 9:02
    blur and you can see here
  • 9:02 - 9:05
    this this would be a blur, what if
  • 9:05 - 9:07
    I take all the pixels in a huge neighbour
  • 9:07 - 9:09
    space, add them all together
  • 9:09 - 9:11
    divide them by 25 as an average
  • 9:11 - 9:13
    and you can see here that this
  • 9:13 - 9:15
    umm frog is now blurred am gonna uncheck
  • 9:15 - 9:18
    the preview let me kind of zoom in here
  • 9:18 - 9:22
    umm you can see ahh over here
  • 9:22 - 9:24
    look over here, that frog is blurred
  • 9:24 - 9:26
    that frog is not blurred so this
  • 9:26 - 9:28
    particular algorithm is blurring that
  • 9:28 - 9:31
    image by adding all the pixels together
  • 9:31 - 9:33
    and infact umm you know am not
  • 9:33 - 9:35
    gonna go through the code for this
  • 9:35 - 9:39
    in detail but we can look at this
  • 9:39 - 9:42
    right here so if I look at this particular
  • 9:42 - 9:46
    example this this particular example you can
  • 9:46 - 9:49
    see here if I zoom in to it that the
  • 9:49 - 9:52
    pixels inside the rectangle are being
  • 9:52 - 9:54
    blurred and the pixels outside are not
  • 9:54 - 9:57
    you can see kinda all the arm above me as
  • 9:57 - 9:58
    I move the rectangle over am kinda
  • 9:58 - 10:00
    blurring out that pixel those pixels
  • 10:00 - 10:03
    if I go back to the code, and I put in
  • 10:03 - 10:08
    these weights this is actually
  • 10:08 - 10:10
    sharpening, again I want the pixel
  • 10:10 - 10:12
    on the center to substract the pixels
  • 10:12 - 10:14
    around it to be more different than
  • 10:14 - 10:16
    the pixels around it and if I run this
  • 10:16 - 10:19
    this is a bit more obvious, we can see
  • 10:19 - 10:21
    that this is me sharpening that image
  • 10:21 - 10:23
    now there's alot of noise is happening
  • 10:23 - 10:25
    here am very zoomed in but you can
  • 10:25 - 10:27
    see how this is changing the
  • 10:27 - 10:29
    quality of the pixels so comparing the
  • 10:29 - 10:31
    pixels and its neighbours this idea of
  • 10:31 - 10:34
    looking at a pixel and looking at the
  • 10:34 - 10:35
    ones that are right of it or above or
  • 10:35 - 10:37
    below is an important powerful
  • 10:37 - 10:39
    technique in image processing
  • 10:39 - 10:41
    and we can just you know this video
  • 10:41 - 10:42
    can go on for hours and hours and
  • 10:42 - 10:44
    look at every image processing
  • 10:44 - 10:45
    algorithm that photoshop uses
  • 10:45 - 10:47
    that anyone uses and we can see this
  • 10:47 - 10:49
    technique in alot of them. I guess
  • 10:49 - 10:51
    what am saying here is see what you can
  • 10:51 - 10:52
    do with this idea.
  • 10:52 - 10:57
    What if you umm what if you ahh umm try
  • 10:57 - 11:00
    to recreate this edge detection algorithm
  • 11:00 - 11:01
    but actually use more than just the two
  • 11:01 - 11:03
    pixels that are to the right and left
  • 11:03 - 11:04
    what if you use the pixels above or down
  • 11:04 - 11:06
    what if you actually were able to
  • 11:06 - 11:08
    interactly select whether you're looking
  • 11:08 - 11:10
    at the comparison of vertical pixels
  • 11:10 - 11:11
    or horizontal pixels there's lots of
  • 11:11 - 11:13
    possibilities you might explore and try
  • 11:13 - 11:15
    here and actually something that I
  • 11:15 - 11:17
    didn't do which I think will be
  • 11:17 - 11:18
    really interesting to see which I
  • 11:18 - 11:19
    would like to maybe add to another
  • 11:19 - 11:20
    video at some point and I.. think of it
  • 11:20 - 11:22
    as what happens
  • 11:23 - 11:25
    when we're processing the pixels
  • 11:25 - 11:28
    this pixel goes here this pixel goes here
  • 11:28 - 11:30
    what if I start to move the pixel around?
  • 11:30 - 11:33
    what if I take, what if every time I look
  • 11:33 - 11:35
    at a pixel I actually offset put it in
  • 11:35 - 11:37
    some like random location near where
  • 11:37 - 11:39
    it originally was this will actually give
  • 11:39 - 11:41
    me almost llike a watery effect because
  • 11:41 - 11:42
    the pixels start to like look like
  • 11:42 - 11:45
    they're dispersing or not. This this is an
  • 11:45 - 11:47
    idea that I'd love to demonstrate
  • 11:47 - 11:49
    so I think this video was already quite
  • 11:49 - 11:50
    loong so am not gonna do it here umm
  • 11:50 - 11:52
    but you can think of this as an excercise
  • 11:52 - 11:54
    and I'll try to add one in at some point
  • 11:54 - 11:55
    too that does this.
  • 11:55 - 11:56
    So here are the things that are missing
  • 11:56 - 11:59
    umm am gonna upload these so if you're
  • 11:59 - 12:00
    watching these in the first pass
  • 12:00 - 12:02
    this is where it's gonna stop
  • 12:02 - 12:04
    but what I wanna get to in the next set
  • 12:04 - 12:05
    of videos, number one I wanna look at
  • 12:05 - 12:07
    moving the pixels around which will
  • 12:07 - 12:09
    also be relevant to the idea of what
  • 12:09 - 12:12
    happens if we just sort of like forget
  • 12:12 - 12:14
    about this way of doing things entirely
  • 12:14 - 12:16
    it's like this one to one pixel idea
  • 12:16 - 12:18
    what if I just have a system where am
  • 12:18 - 12:21
    drawing you know patterns and shapes
  • 12:21 - 12:23
    to the screen maybe I draw grid of
  • 12:23 - 12:24
    rectangles or I draw random circles
  • 12:24 - 12:27
    everywhere or I draw swirls swirly spirals
  • 12:27 - 12:29
    all over the screen what if just use an
  • 12:29 - 12:32
    image essentially a database of color
  • 12:32 - 12:33
    so what if I could like look up colors
  • 12:33 - 12:35
    doing form a drawing algorithm
  • 12:35 - 12:37
    create a painting for an image
  • 12:37 - 12:38
    that's one thing I'm gonna look at
  • 12:38 - 12:41
    and once we've done that, the next piece
  • 12:41 - 12:42
    that I wanna look at is what happens
  • 12:42 - 12:44
    when this images aren't static
  • 12:44 - 12:46
    what if I'm getting images from a camera
  • 12:46 - 12:49
    that are changing live real time images or
  • 12:49 - 12:51
    images from a movie file.
  • 12:51 - 12:53
    So those are the I guess there's like
  • 12:53 - 12:55
    three more three/four more videos that
  • 12:55 - 12:56
    need to come to kinda finish off
  • 12:56 - 12:57
    all of these concepts.
  • 12:57 - 13:00
    Ok so send me your feedback, umm I'll
  • 13:00 - 13:03
    be happy to add more things, delete
  • 13:03 - 13:06
    things, redo things or just go away not
  • 13:06 - 13:08
    come back. (laughs) Ok so have good..
  • 13:08 - 13:10
    enjoy your morning, evening, night.
  • 13:10 - 13:13
    I don't know when you're watching this
    and you're just like eating some cereal
  • 13:13 - 13:16
    at home that's... something
    you might be doing and
  • 13:16 - 13:18
    enjoy that cereal (laughs) talk to you
    later.
Title:
10.6: Pixel Neighbors - Processing Tutorial
Description:

Book: Learning Processing A Beginner's Guide to Programming, Images,Animation, and Interaction

Chapter: 15

Official book website: http://learningprocessing.com/

Twitter: https://twitter.com/shiffman

This video covers how to compare pixels to its neighbors for the purpose of image processing.

This video needs links to source code examples!
This video needs links to other things mentioned!
Please write in the comments what is missing and what would be helpful!

more » « less
Video Language:
English
Duration:
13:20

English subtitles

Revisions