# Image Processing IV
## Icebreaker 🧊
**Two Truths and a Lie 🕵️♂️🔍🤫**
We will put you into breakout rooms.
Each participant will prepare three statements about themselves: two truths and a made-up story. You will share the three statements with your group as if they're all true!
After each participant shares their statements, the rest of the group will have to guess which statement is the lie. After the guessing round, the participant will reveal the lie.
Continue with the next participant until everyone has had a turn.
## Objectives🎯
1. Understanding edge detection and its applications.
2. Refine our function to finally uncover the license plate.
## Refining our function
Okay, so now we have this idea that we can use formulas on an image to change the values of its pixels in relation to its surrounding ones. In order to be able to read the license plate, we will have to clearly see the outline of the license characters. We want to clearly see the edges! We will continue using our [colab](https://colab.research.google.com/drive/1j2buZW4NOP_mBrlVY1V2rraThfutt1_d) from yesterday.
Imagine your favorite comic book. Recall how the bold lines around characters make everything stand out. We sort of want to do the same...

Let's take a look at this image. We can very clearly see the outline of the character.
**What would the pixel values around an edge look like? Think about our image above, think about the numeric pixel value our background would have compared to the character's outline.**
<details>
<summary> <B>Think, then click</B> </summary>
The pixel values around the edge would show a significant difference or a sudden change in the pixel values.
</details>
<br>
So in order to find the edges in an image, we need to find where there are sharp changes in the pixel values.Imagine each pixel's value represents how much change is there. A big change means a big value, and a small change means a small value.
Can you think of why making pixel values represent change might be useful?
Thinking back to our image above, with Greg's outline, the change is big because the colors go from his outline color (black) to (white) colors. But for the background itself, where the colors stay similar(white), the change is much smaller.
**What higher level math concept does this sound like?**
<details>
<summary> <B>Think, then click</B> </summary>
Derivatives! We use derivatives to measure the rates of change in pixel values.
</details>
<br>
Okay, so let’s find the derivatives across this image. We’re just going to give you the formula for this and take a look at the result.
Here’s how we could code this:
```python=
import math
# A B C
# D E F
# G H I
def edge_detect(img):
result = np.zeros(img.shape)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
A = safe_get(i-1, j-1, img)
B = safe_get(i-1, j, img)
C = safe_get(i-1, j+1, img)
D = safe_get(i, j-1, img)
E = safe_get(i, j, img) * 9
F = safe_get(i, j+1, img)
G = safe_get(i+1, j-1, img)
H = safe_get(i+1, j, img)
I = safe_get(i+1, j+1, img)
# FORMULA
xenergy = A + 2*D + G - C - 2*F - I
yenergy = A + 2*B + C - G - 2*H - I
result[i,j] = math.sqrt(pow(xenergy, 2) + pow(yenergy, 2))
return result
```
And here’s the result of running that:

To make the license plate more clear, we should increase the sensitivity of our edge detector.
**What can we change in our code to to detect even the tiniest changes in the image?**
<details>
<summary> <B>Think, then click</B> </summary>
We can multiply the final value by a number to make smaller edges more visible. Here’s a case where I multiplied by 4:
</details>
<br>

Okay, let’s look at the sharpened and edge detected license plates a little more closely:


## Edge Detection
We just used an image processing technique called edge detection.

When you spot edges, you're finding those important lines that separate different parts of a picture.
**Brainstorm 🧠: What are other real-world situations in which edge detection is relevant. Write down your ideas in our jamboard.**
<details>
<summary> <B>Think, then click</B> </summary>
Some other real world applications
* Medical Imaging
* Self-driving cars
* Sharpening
* Optical Character Recognition (OCR): In processing documents,text is separated from the background, making it easier for computers to recognize and digitize text.
</details>
<br>
**Physical Keys & Edge Detection**
Physical keys might seem unrelated, but they're an example of how edge detection ties into security. Imagine you have a real key in your hand. What makes that key unique? It's not just the shape; it's the tiny grooves on the key's "teeth." These grooves create a pattern, and each key's pattern is unique. **Can you think of ways we might need to be cautious about using edge detection in certain situations?**
<details>
<summary> <B>Think, then click</B> </summary>
Imagine someone takes a super sharp picture of your key's teeth. This picture is like an image, and edge detection can be used to spot the edges of those grooves. Once those edges are detected, someone could potentially create a copy of your key using the pattern they found in the image.
</details>
<br>