# Coin counting

ภาพตัวอย่าง

![](/files/-MIJ17BkjhQGYpr8rZ8z)

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

![](/files/-MHu3yO0ci1O_jsf-s3O)

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

![](/files/-MHuPSTEsl9i_TY9ujr0)

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

![](/files/-MIJ1g3y664027OpA2GZ)


---

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