> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/artificial-intelligence-ai/basic-image-processing/computer-vision-for-python/lab-2-basic-cv/canny-edge-detection.md).

# Canny edge detection

&#x20;    Canny edge detection เป็นการหาเส้นรอบวัตถุที่อยู่ในภาพ โดยจะทำการแปลงภาพสีเทาเป็นภาพไบนารีของเส้นรอบวัตถุและมีอัลกอริทึมหลายขั้นตอนในการหาเส้นรอบวัตถุ เนื่องจากขอบภาพเกิดจากความแตกต่างของความเข้มแสงจากจุดหนึ่งไปยังอีกจุดหนึ่ง หากความต่างนี้มีค่ามาก จะทำให้เห็นขอบภาพได้อย่างชัดเจน แต่ถ้าความต่างนี้มีค่าน้อย จะทำให้เห็นขอบภาพได้ไม่ชัดเจน&#x20;

```
$ cv2.Canny(image, threshold1, threshold2, apertureSize, L2gradient)
```

\
**Parameters:**

* image => ภาพสีเทาที่จะนำมาใช้หาเส้นขอบวัตถุ
* threshold1 => เกณฑ์ค่าพิกเซลขั้นต่ำสำหรับขั้นตอน hysteresis
* threshold2 => เกณฑ์ค่าพิกเซลขั้นสูงสำหรับขั้นตอน hysteresis

Optional parameters are:

* apertureSize => kernel สำหรับตัวกรอง Sobel ค่าเริ่มต้นคือ (3 x 3) ใช้สำหรับค้นหาการไล่ระดับสีของภาพ ฟิลเตอร์ใช้สำหรับปรับภาพให้เรียบและคมขึ้น
* L2gradient => เป็นตัวแปรประเภทบูลีนและค่าเริ่มต้นคือ False

\
**Example:**

```
import cv2
import imutils

image = cv2.imread("/path/your/image.jpeg")
image = imutils.resize(image,width=400)
cv2.imshow("Original", image)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0) 
cv2.imshow("Blurred", blurred) 

wide = cv2.Canny(blurred, 10, 200)
cv2.imshow("Wide Edge Map", wide)

mid = cv2.Canny(blurred, 30, 150)
cv2.imshow("Mid Edge Map", mid)

tight = cv2.Canny(blurred, 240, 250) 
cv2.imshow("Tight Edge Map", tight)
cv2.waitKey(0)
```

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600707852156_Screenshot+from+2020-09-22+00-03-51.png)
