> 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/image-basics.md).

# Pixel and Color

### Pixel    &#x20;

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

{% hint style="info" %}
*ตัวอย่าง*&#x20;

&#x20;    ภาพมีขนาด 600x450 หมายความว่า ภาพกว้าง 600 และสูง 450 พิกเซล ถ้าเทียบเป็นตารางพิกเซลจะได้ 600 คอลัมน์และ 450 แถว โดยรวมแล้วมี 600  x 450 = 270,000  พิกเซล  &#x20;
{% endhint %}

&#x20;    พิกเซลส่วนใหญ่แสดงเป็นสองลักษณะ คือ ระดับสีเทา (Gray scale) และสี (RGB) ในภาพระดับสีเทาแต่ละพิกเซลมีค่าระหว่าง 0 ถึง 255 โดยที่ค่า 0 จะตรงกับ "ดำ" และ 255  เป็น "สีขาว"&#x20;

![](https://gurus.pyimagesearch.com/wp-content/uploads/2015/03/image_gradient.jpg)

&#x20;    ภาพสี (RGB) ในหนึ่งพิเซลจะประกอบไปด้วย 3 ค่า คือ สีแดง (Red) สีเขียว (Green) และสีน้ำเงิน (Blue) โดยทั้งสามค่าจะมีค่าความเข้มอยู่ในช่วง 0 ถึง 255 เช่นเดียวกับภาพระดีบสีเทา สามารถเขียยนให้อยู่ในรูป tuple ได้ดังนี้ (R,G,B)

&#x20;    ![](https://gurus.pyimagesearch.com/wp-content/uploads/2015/03/color_examples.jpg)\
&#x20;   &#x20;

&#x20;    หากเปรียบเทียบภาพเป็นตาราง จุดพิกัดเริ่มต้น (0,0) ของภาพจะอยู่ที่มุมซ้ายบน และจุดพิกัดสุดท้าย (x,y) ของภาพจะอยู่ที่มุมล่างขวาของภาพ ซึ่งจุดพิกัดสุดท้ายจะมีขนาดเท่ากับขนาดของภาพเสมอ ดังภาพตัวอย่างต่อไปนี้

\
![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600702510314_Screenshot+from+2020-09-21+22-34-53.png)

#### **Example:**

```
import cv2
import imutils 

image = cv2.imread("path/your/image.jpg")
image = imutils.resize(image,width=400)

(h, w) = image.shape[:2]
(b, g, r) = image[0, 0]
print("Pixel at (0, 0) - Red: {r}, Green: {g}, Blue: {b}".format(r=r, g=g, b=b)) 

image[0, 0] = (0, 0, 255)
(b, g, r) = image[0, 0]
print("Pixel at (0, 0) - Red: {r}, Green: {g}, Blue: {b}".format(r=r, g=g, b=b))

cv2.imshow("Original", image)
cv2.waitKey(0)
```

* บรรทัดที่ 1-2 : การเรียกใช้ไลบารี่ที่เกี่ยวข้อง
* บรรทัดที่ 6 : รับตัวแปรขนาดลำดับที่ 0 และลำดับที่ 1 ของภาพ โดยที่ขนาดของภาพจะถูกเก็บในลักษณะอาร์เรย์ ซึ่งจะเรียงจาก ความสูง (h) ความกว้าง (w) และช่องสีของภาพ (3 สี) ตามลำดับ
* บรรทัดที่ 8 รับตัวแปรค่าสีที่ตำแหนงเริ่มต้น (0,0) ของภาพ โดย**ค่าสีของภาพจะถูกเก็บในลักษณะอาร์เรย์ ซึ่งจะเรียงจาก สีน้ำเงิน** **(Blue)** **สีเขียว** **(Green)** **และสีแดง** **(Red)** **ตามลำดับ**

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600703230861_Screenshot+from+2020-09-21+22-46-51.png)

###

### **C**onversion Color&#x20;

&#x20;    การแปลงภาพจากพื้นที่สีหนึ่งไปยังอีกที่หนึ่ง    &#x20;

```
$ cv2.cvtColor(image, flag)
```

**Parameter:**

* image => ภาพที่ต้องการนำมาใช้งาน
* flag =>  กำหนดประเภทของการแปลง

{% hint style="info" %}
flag ที่นำมาใช้

* cv2.COLOR\_BGR2GRAY = แปลงภาพสี BGR เป็นภาพสีเทา
* cv2.COLOR\_BGR2HSV = แปลงภาพสี BGR เป็นภาพสี HSV
  {% endhint %}

\
**Example:**

```
import cv2
import imutils 

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

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow("Gray Scale",gray)
cv2.waitKey(0)

```

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600706905003_Screenshot+from+2020-09-21+23-48-09.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/image-basics.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.
