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

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MXa7d0HAKu46hDe_7IO%2F-MXa8_yotau2IcQZCeN1%2FScreenshot%20from%202021-04-06%2014-39-23.png?alt=media\&token=2bc07991-df72-4ba9-9499-b434244474e0)

###

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

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MXa7d0HAKu46hDe_7IO%2F-MXa9WlZszYMe_2i7BD-%2FScreenshot%20from%202021-04-06%2014-50-25.png?alt=media\&token=812fcd23-d12b-4c02-b436-e57277a5d70e)

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

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MXa7d0HAKu46hDe_7IO%2F-MXaAjMwjpkyU3X-KrlC%2FScreenshot%20from%202021-04-06%2014-54-10.png?alt=media\&token=dada845d-3257-4ccf-8fb7-4a4cfae2d17d)

###

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

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MXa7d0HAKu46hDe_7IO%2F-MXaC0EeBNvxSuMyEJCR%2FScreenshot%20from%202021-04-06%2015-01-58.png?alt=media\&token=5cbf983e-e8f7-4b9e-ae9e-79dbaac73436)

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

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MXa7d0HAKu46hDe_7IO%2F-MXaDjggQqC6rWv0HHis%2FScreenshot%20from%202021-04-06%2015-05-00.png?alt=media\&token=efa06d98-631d-4c13-8f80-d54bbf115c5f)
