> 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-3-case-study/coin-counting.md).

# Coin counting

ภาพตัวอย่าง

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MII6kB5GuybZQaHjFJV%2F-MIJ17BkjhQGYpr8rZ8z%2F120241279_631603467550504_1016149987895172216_n.jpg?alt=media\&token=63acfdb0-eb29-4f5d-9d89-d7f11973f6e6)

step 1 : เปิด terminal (windows - cmd , mac - terminal)

step 2 : เข้าโฟลเดอร์ที่ติดตั้ง Visual environment และไลบารี่ OpenCV&#x20;

```
$ cd opencv
```

step 3 : เปิด  Visual environment&#x20;

```
## windows
$ <C:path\your\project>\env\Scripts\activate.bat

## ubuntu & mac
$ source venv/bin/activate
```

step 4 : command “code .” in terminal

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MHtyg4y1ulQYF0obJ_h%2F-MHu3yO0ci1O_jsf-s3O%2FScreenshot%20from%202020-09-23%2015-28-03.png?alt=media\&token=db160940-1ac2-4ed8-97d0-651e1b4e6694)

step 5 : create file counter.py

step 6 : ลำดับการทำงานของชุดคำสั่ง

&#x20;    นับเฉพาะเหรียญ หมายความว่า ต้องทำการแยกวัตถุกับภาพพื้นหลังออกจากกัน ก็คือใช้ Threshold แต่เนื่องจาก Threshold ต้องใช้กับภาพไบนารีหรือภาพสีเทาเท่านั้น หลังจากได้ภาพไบนารีที่มีแต่เหรียญได้แล้ว จากนั้นจึงทำการวาดกรอบรอบเหรียญเพื่อทำการนับ หรือก็คือการใช้ Contour&#x20;

* รับภาพ `cv2.imread`
* เปลี่ยนภาพเป็นสีเทา `cv2.cvtColor`
* เบลอภาพเพื่อลด noise `cv2.GaussianBlur`
* threshold `cv2.threshold`
* contour `cv2.findContour & cv2.drawContours`
* แสดงเลขตรงจุดกึ่งกลางของเหรียญ `cv2.moment & cv2.putText`

{% hint style="info" %}
&#x20;    การออกแบบการทำงานสามารถปรับเปลี่ยนทำได้หลายวิธีตามแต่ผู้ออกแบบหรือตามความเหมาะสมของภาพที่จะนำมาใช้งาน
{% endhint %}

step 7 : save file and run "python counter.py" on terminal

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MHtyg4y1ulQYF0obJ_h%2F-MHuPSTEsl9i_TY9ujr0%2FScreenshot%20from%202020-09-23%2017-02-00.png?alt=media\&token=55d1df32-1964-4c40-b1df-1ac3a7cb7628)

**Example:**

```
import cv2
import imutils

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

blur = cv2.GaussianBlur(gray,(3,3),7)

T,thresh = cv2.threshold(blur,20,255,cv2.THRESH_BINARY)
cv2.imshow("threshold",thresh)

cnts = cv2.findContours(thresh,cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
clone = image.copy()

i = 0

for c in cnts:
    # compute the area and the perimeter of the contour
    area = cv2.contourArea(c)
    
    if area > 0:

        i = i+1

        cv2.drawContours(clone, [c], -1, (0, 255, 0), 2)
        
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        
        cv2.putText(clone, "#{}".format(i), (cX - 20, cY), cv2.FONT_HERSHEY_SIMPLEX,
            1.25, (255, 255, 255), 4)
    else:
        pass
    
cv2.imshow("original",image)
cv2.imshow("counter",clone)
cv2.waitKey(0)
```

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MII6kB5GuybZQaHjFJV%2F-MIJ1g3y664027OpA2GZ%2FScreenshot%20from%202020-09-28%2016-27-58.png?alt=media\&token=fd3cadc9-b073-4790-93aa-e909a5d16be7)
