JES Image Manipulation - 7 - Edge Detection

WilliamFiset · Intermediate ·⚡ Algorithms & Data Structures ·12y ago
Skills: CV Basics85%
SourceCode: https://dl.dropboxusercontent.com/u/84011326/Youtube%20files/JES/6_edge.py Images: https://dl.dropboxusercontent.com/u/84011326/Youtube%20files/JES/images.zip =============================================================================== Developer tools I used in the creation/testing of the content in these videos: 1) Sublime text, my favorite lightweight code editor (https://www.sublimetext.com). NOTE: I'm often asked about the color scheme I use, find it here: https://github.com/williamfiset/dotfiles/tree/master/sublime 2) Kite, a free AI-powered coding assistant that provides smart code completions while typing: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=williamfiset&utm_content=description-only ===============================================================================

What You'll Learn

The video demonstrates edge detection in image processing using JES Image Manipulation, implementing techniques such as comparing luminance between adjacent pixels and using a precision variable to determine edges. Tools like get pixel function, xrange, and Lambda functions are utilized to develop the edge detection algorithm.

Full Transcript

hello everyone welcome back for an image manipulation video and in today's tutorial we're going to check out how to do Edge detection you might be saying computers can't detect edges on pictures that's nuts well I'm telling you it's possible and the way it's done is basically you check the difference in uh luminance between AJ recent pixels so first let's save this file and um right here in video 5 let's call it uh edge. py so essentially what we're going to do is we're going to have to develop another technique since we need to grab the adjacent pixels before we were using a function call it get uh pixels like this which took a picture object and would return a list of you know all the pixels in our image like that but what if you have a pixel but you want to get the one that's to its right well you need to use a function called get pixel get pixel like that this also takes a picture object because it needs to know which picture you're accessing the pixel in and what X and Y coordinates that pixel is within this image um of course you can't access pixels that are outside the image or you'll have an error raised so with that in mind let's proceed okay so what we want to do is we want to make a function called X Ed or whatever you want to call it um and this going to have a picture and it's also going to have a variable called pre uh cision and what Precision is is going to be a variable that measures um how precise we want our Edge dection to be um I find that a good number is say maybe like 15 um and you'll see why later on okay so we want to as usual make a copy of our picture so just picture equals a duplicate picture and we're going to pass picture variable and for this uh image manipulation we're also going to want two variables called width and height now we can access um our width and height through our picture do width and a picture. height but um I would recommend that you use um they'll give the same result but it's preferable if you use Getters just because they're built-in functions um so get with of our picture and get height our pictures okay this is actually pretty self-explanatory Returns the width and the height of our image and now that's all good now if you remember I said get pixel got um took as arguments picture and X and Y well we need to Loop through all of our picture and all of its X and Y coordinates so to do that we are going to do uh we're going to use two for loops and this often confuses people but it's really really simple so for y in the range of height and for X in the range of width and this will just Loop through all our y's and all our x's and produce all the X and Y coordinates that we need now notice that instead of using range I used X range um the reason being is uh we don't need to return a list all we need to do is just have our X and our Y and X range is actually faster than range so that's why I use it just don't confuse yourself they're almost identical except xrange returns and xrange object but it'll work nevertheless all right um so how to proceed is is for Edge detection or how we're going to do it we're just going to get the pixel that is below and to the right of our current pixel now this causes a problem when we're trying to access uh a pixel that is on the far right side or on the very um very like um bottom of our image because those pixels don't exist that's why I'm going to say height minus one and with minus one so that we don't access pixels that don't exist now if you were Photoshop you would probably want to access pixels that are above below left and right but we're just going to access the ones that are to right and below us just because that is easier and it does essentially the same job and we're not photoshoop okay so our current pixel I'm just going to call it PX is going to equal um our current pixel is within our picture at coordinate XY and our pixel to the right which I'm just going to call right just going to be our pixel same as this one so we could just copy this the only difference is if it's to the right it's going to be x + 1 and if it is below is going to be the same thing as our original except there's going to be a change in the y coordinate it's going to be y + 1 remember our y coordinate system is flipped so y + one is actually down and not up so don't confuse yourself there okay let me scroll down next what we want to do is actually get the luminance of each of these pixels so that we can compare them later on and we could create another function um to get our luminance remember it's our red plus our green plus our blue and uh all that divid by three but instead I'm just going to make an anonymous function with Lambda so luminance is going to equal Lambda pixel and all that divided by three so going to get the red of our pixel plus get blue of our pixel plus get green of our pixel if you guys can see that hold on like that so essentially all this does is we're saying okay um we're going to pass in a pixel as a parameter for our function and it's going to get it's red it's blue and it's green it's going to divide by three and this value is what it's going to return okay um if you're confused about the whole Lambda thing you can just create your own function that will add the red green and blue divide by three and return that value it's not a big deal so um the luminance of our right pixel so this is a say luminance it stands for right so the luminance of our right pixel is going to be equal to luminance that's this up here and remember it takes a pixel object so our right pixel was called right so luminance of right and the luminance of our uh below pixel is going to be equal to all this except it's going to take the below pixel and the luminance of our um original pixel is going to be all this except it's going to be PX right there okay cool so we have the luminance of our right pixel our below pixel and our original pixel now this is where the actual algorithmic part comes into play so the um for a pixel to be an edge it has to be the difference between the right pixel and the below pixel uh has to be greater than uh our Precision whichever which said was 15 up here or whatever we specify okay so if the difference meaning the absolute value between uh difference of our left pixel and our original pixel is greater than our pre uh cision I think that's how I spelled it yeah okay uh is greater than our precision and this is very important and uh the difference between our below pixel and our original pixel well the luminance of these is also greater than our Precision then we know that this pixel um is an edge and the way we set a pixel to be an edge is essentially we're going to return a black and white image with all the black black pixels being an edge and all the white pixels just not an edge so ignore them so set the color of our current pixel remember that's just PX which we got up here to Black and if it's not an edge then we can do this except White awesome and all we have to do now is return oh one more two more okay return that image so picture you might be wondering why do we return picture well remember um when we got those pixels uh we got them inside the picture so whenever we change the color of the this pixel it knows that it's associated with this picture okay so let's pull up our command line and let's test this function and I'll go over it one more time because we've seen a lot of new stuff okay so our picture um we want to make a picture object and we want to get a picture object through the finder so right here images and I have this sweet little image right here guys check it out this actually the background on my iPod if you want a copy of it I don't blame you check out this cute sheep okay so when we run our Edge dection The Edge um you can think of as if you were an artist what is the main thing what is the most important thing in this image well obviously it's the Sheep so the edge should be able to detect the Sheep the legs and the ear maybe the tail stuff like that okay I'm just bring that down and let's just call it Edge uh sheep equals Edge and remember Edge takes uh two parameters a picture this that's our picture right here and uh Precision well I'm just going to leave it 15 for now so run that and hope we don't have any bugs okay so we're going to show the picture Edge sheep and check it out that is our sheep and you can see it Precision of 15 got all we needed it got um like bit the eye the the ears tail and legs so that's really good now let's see what happens if we increase the Precision so if we increase the Precision and say maybe 30 oops oops 30 let's see what that will give us so if we look in our code we can see that uh the difference in Precision has to be greater so what will that do so as you can see an increase in Precision we don't have any more of those black dots that we had we just have the main portion of our image we only have the real Edge now so as you increase the Precision you're losing those pixels that you don't really want um in your image but you're also losing detail on your your your true Edge so it's kind of a tradeoff um I always prefer having a lower Precision just so that it gets more detail but it's all up to you and now let's go over the code because I'm sure it's confusing a lot of you guys okay so from the start um we make a um duplicate of our picture so doesn't um tamper with the original next we get the width and the height of our image and we do that because we need to Loop through um the range of the height and the range of our X and of our width I'm sorry and that's what we do right here we Loop through all the Y's in our in our height minus one because we don't want to grab um a pixel at the bottom very bottom of our image um because if we did that that pixel would grab a pixel that's not in the image same thing for the width next that now that we have an X in our Y coordinates we can actually go and fetch our original pixel which is this one I make comment the original pixel and these pixels are to the right and Below of this original pixels because of the shifts in X and Y that we put here next this um function uh luminance um gets the uh average of our red green and blue values and divides it by three so it gets the luminance again uh don't be too confused by this just make your own function if it confuses you it's not a big deal um next we get the luminance using our luminance um function of our pixels up here pixel right below and then we get the difference between our right pixel and our original pixel and the difference between our below pixel and our uh original pixel and if both of those differences are greater than whatever Precision we identified has to be both of them because of this and condition right here then we can set that pixel to be black meaning it's an edge yes we found an edge all right that's good but if not if it's not an edge then we set it to White so that's why most of our sheep image was white is because it didn't find an edge so guys that is all for today if you have any questions just leave them in the comments below and thanks again for watching I really appreciate it
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 7 of 60

1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

This video teaches edge detection in image processing using JES Image Manipulation, covering techniques such as comparing luminance between adjacent pixels and using a precision variable to determine edges. It provides a comprehensive understanding of how to implement edge detection algorithms in image processing. By following the steps outlined in the video, viewers can develop their own edge detection algorithms and apply them to various image processing tasks.

Key Takeaways
  1. Create a function called X Ed to implement edge detection
  2. Make a copy of the picture and pass it to the function
  3. Loop through all pixels in the image using two for loops
  4. Get the pixel below and to the right of each current pixel
  5. Use xrange instead of range for faster iteration
  6. Calculate luminance of a pixel using the luminance function
  7. Compare luminance of adjacent pixels to detect edges
💡 The edge detection algorithm uses the luminance function to compare pixels and determine if they are part of an edge, and increasing the precision of edge detection can result in losing detail on the true edge.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →