> 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-2-basic-cv/draw-image.md).

# Draw image

&#x20;    หัวข้อนี้จะกล่าวถึงการวาดรูปร่างด้วยการใช้คำสั่ง cv2.line, cv2.rectangle และ cv2.circle จะทำการวาดบนภาพพื้นหลังสีดำ ซึ่งการสร้างภาพพื้นหลังจะต้องใช้ Numpy ที่เป็นไลบรารีประมวลผลตัวเลข

### **Create black background**

```
$ np.zeros(shape, dtype="uint8")
```

**Parameters:**

* shape => กำหนดขนาดของอาร์เรย์
* dtype => ประเภทข้อมูล

{% hint style="info" %}
uint8 คือ ข้อมูลตัวเลขที่มีขนาด 8 บิต (0 ถึง 255)
{% endhint %}

\
**Example:**

```
import cv2
import numpy as np

canvas = np.zeros((300, 300, 3), dtype="uint8")
```

บรรทัดที่ 4 => สร้างอาร์เรย์ที่เป็น 0 ขนาด 300\*300 (สูง\*กว้าง) และมีพื้นที่ 3 ช่องสี (RGB)&#x20;

### &#x20;**Line**

```
$ cv2.line(image, start_point, end_point, color, thickness)
```

**Parameters:**

* image  => ภาพที่ต้องการวาดเส้น
* start\_point => จุดเริ่มต้นของเส้น โดยจะเขียนในลักษณะคู่อันดับ (x,y)&#x20;
* end\_point => จุดสิ้นสุดของเส้น โดยจะเขียนอในลักษณะคู่อันดับ (x,y)&#x20;
* color => สีของเส้น โดยจะเขียนในลักษณะ tuple และเป็นลำดับสี BGR&#x20;

  &#x20;                eg: (255, 0, 0) = สีน้ำเงิน
* thickness => ความหนาของเส้น

\
**Example:**

```
import cv2
import numpy as np

canvas = np.zeros((300, 300, 3), dtype="uint8")

red = (0, 0, 255)
cv2.line(canvas, (300, 0), (0, 300), red, 3)

cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
```

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600687235228_Screenshot+from+2020-09-21+18-20-16.png)

###

### **Circle**

```
$ cv2.circle(image, center_coordinates, radius, color, thickness)
```

**Parameters:**

* image => ภาพที่ต้องการวาดวงกลม
* center\_coordinates => จุดศูนย์กลางของวงกลม โดยจะเขียนอยู่ในลักษณะจุดคู่อันดับ (x,y)
* radius => รัศมี ขนาดของวงกลม
* color => สีของเส้น โดยจะเขียนในลักษณะ tuple และเป็นลำดับสี BGR.&#x20;

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

#### &#x20;**Example**:

```
import cv2
import numpy as np

canvas = np.zeros((300, 300, 3), dtype="uint8")

(centerX, centerY) = (canvas.shape[1] // 2, canvas.shape[0] // 2)
white = (255, 255, 255) 

for r in range(0, 175, 25):        
    
    cv2.circle(canvas, (centerX, centerY), r, white)
    
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
```

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600687296604_Screenshot+from+2020-09-21+18-21-14.png)

###

### **Rectangle**

```
$ cv2.rectangle(image, start_point, end_point, color, thickness)
```

**Parameters:**

* image => ภาพที่ต้องการวาดสี่เหลี่ยม
* start\_point => จุดเริ่มต้น (มุมซ้ายบน) ของสี่เหลี่ยม โดยจะเขียนอยู่ในลักษณะจุดคู่อันดับ (x,y)&#x20;
* end\_point => จุดสิ้นสุด (มุมล่างขวา) ของสี่เหลี่ยม โดยจะเขียนอยู่ในลักษณะจุดคู่อันดับ (x,y)&#x20;
* color => สีของเส้น โดยจะเขียนในลักษณะ tuple และเป็นลำดับสี BGR

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

\
**Example:**

```
import cv2
import numpy as np

canvas = np.zeros((300, 300, 3), dtype="uint8")

blue = (255, 0, 0)
cv2.rectangle(canvas, (0,100), (225, 300), blue, -1)

cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
```

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600687715570_Screenshot+from+2020-09-21+18-28-21.png)

### **Text**

```
$ cv2.putText(image, text, org, font, fontScale, color, thickness)
```

**Parameters:**

* image => ภาพที่ต้องการเขียนตัวอักษร
* text => ข้อความที่ต้องการเขียน
* org => จุดเริ่มต้นเขียนตัวอักษร โดยเขียนอยู่ในลักษณะจุดคู่อันดับ (x,y)&#x20;
* font => ประเภทของตัวอักษร
* fontScale => ขนาดของตัวอักษร
* color => สีของเส้น โดยจะเขียนในลักษณะ tuple และเป็นลำดับสี BGR.&#x20;

  &#x20;               eg: (255, 0, 0) = สีน้ำเงิน
* thickness => ความหนาของเส้นตัวอักษร

\
**Example:**

```
import cv2
import numpy as np

canvas = np.zeros((300, 300, 3), dtype="uint8")

cv2.putText(canvas, "Hello world!!", (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0),1 )

cv2.imshow("Canvas", canvas)
cv2.waitKey(0)

```

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600687952767_Screenshot+from+2020-09-21+18-32-18.png)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
