# 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)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/artificial-intelligence-ai/basic-image-processing/computer-vision-for-python/lab-2-basic-cv/canny-edge-detection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
