# Contour

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

### **Find Contours**

```
$ cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
```

**Parameters:**

* image => ภาพไบนารีหรือภาพระดับสีเทา
* mode => CV\_RETR\_LIST เป็นโหมดพื้นฐานที่ใช้ในการดึงรูปร่างทั้งหมดของวัตถุ
* method => CV\_CHAIN\_APPROX\_SIMPLE เป็นเทคนิคที่ใช้ในการบีบอัดจุดซ้ำซ้อน&#x20;

ยกตัวอย่างเช่น การวาด contour บนสี่เหลี่ยม จุดที่ต่อเนื่องกันของสี่เหลี่ยมจะถูกบีบอัดให้เหลือเพียง 4 จุดที่จำเป็น

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600711605314_Screenshot+from+2020-09-22+01-06-33.png)

**Draw contour**

```
$ cv2.drawContours(image, cnts, contourIdx, color, thickness)
```

**Parameters:**

* image => ภาพผลลัพธ์ที่ต้องการวาดเส้นกรอบ contour
* cnts => รูปทรงอินพุตทั้งหมด แต่ละเส้นจะถูกจัดเก็บเป็นเวกเตอร์จุด
* contourIdx => ระบุรูปร่างที่จะวาด หากเป็นลบจะมีการวาดรูปทรงทั้งหมด
* color => สีของเส้น contour โดยเขียนในลักษณะ tuple และเป็นลำดับสี BGR

  &#x20;               eg: (255, 0, 0) = สีน้ำเงิน
* thickness => ความหนาของเส้น ถ้าเป็น -1 จะเป็นกรอบทึบ

\
**Example:**

```
import numpy as np
import cv2
import imutils

image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
cv2.imshow("Original", image) 

cnts = cv2.findContours(gray.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

clone = image.copy()
cv2.drawContours(clone, cnts, -1, (0, 255, 0), 2)

cv2.imshow("All Contours", clone)
cv2.waitKey(0)
```

![](/files/-MXa8_yotau2IcQZCeN1)

###

### Centroid

&#x20;    เป็นการหาจุดศูนย์กลางของวัตถุ โดยอาศัยการคำนวณทางคณิตศาสตร์

```
import numpy as np
import cv2
import imutils

image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
 
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)

cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

clone = image.copy()
 
for c in cnts:

	M = cv2.moments(c)
	cX = int(M["m10"] / M["m00"])
	cY = int(M["m01"] / M["m00"])
 
	cv2.circle(clone, (cX, cY), 10, (0, 255, 0), -1)
 
cv2.imshow("All Contours", clone)
cv2.waitKey(0)
```

![](/files/-MXa9WlZszYMe_2i7BD-)

### Bounding Boxes

```
import numpy as np
import cv2
import imutils

image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
 
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)

cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

clone = image.copy()
 
for c in cnts:
	(x, y, w, h) = cv2.boundingRect(c)
	cv2.rectangle(clone,(x,y),(x+w,y+h),(0,255,0),2)
 
cv2.imshow("All Contours", clone)
cv2.waitKey(0)
```

![](/files/-MXaAjMwjpkyU3X-KrlC)

###

### Rotated Bounding Boxes

```
import numpy as np
import cv2
import imutils

image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
 
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)

cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

clone = image.copy()
 
for c in cnts:
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(clone,[box],0,(0,0,255),2)
 
cv2.imshow("All Contours", clone)
cv2.waitKey(0)
```

![](/files/-MXaC0EeBNvxSuMyEJCR)

### Minimum Enclosing Circles

```
import numpy as np
import cv2
import imutils

image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
 
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)

cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

clone = image.copy()
 
for c in cnts:
    (x,y),radius = cv2.minEnclosingCircle(c)
    center = (int(x),int(y))
    radius = int(radius)
    cv2.circle(clone,center,radius,(0,255,0),2)
 
cv2.imshow("All Contours", clone)
cv2.waitKey(0)
```

![](/files/-MXaDjggQqC6rWv0HHis)


---

# 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/contour.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.
