ตรวจจับวัตถุสีน้ำเงินด้วย python (Blue object detection with python)

Dr. Pathasu Doungmala
2 min readNov 17, 2021

--

ตรวจจับวัตถุสีน้ำเงินด้วย python (Blue object detection with python)

ในบทความนี้ใช้การแปลงภาพจาก RGB ให้เป็น HSV

ปริภูมิสี HSV เป็นปริภูมิสีที่ประกอบด้วย 3 องค์ประกอบได้แก่ค่า H (Hue) คือค่า โทนสีหรือค่าสีสันซึ่งมีค่าสีที่แตกต่างกันไปตามความถี่ของแสง ค่า S (Saturation) คือค่าความ อิ่มตัวของสี ซึ่งเป็นค่าที่แสดงระดับสีเมื่อเทียบกับค่าโทนสี และค่า V (Value) คือค่าบอกระดับ ความสว่างของภาพ ซึ่งที่ระดับความสว่างต่ำสุดหมายถึงสีดำ ไม่ว่าจะมีค่าโทนสี หรือค่าความอิ่มตัว สีเท่าใด และระดับความสว่างสูงสุดหมายถึงสีขาว ซึ่งเป็นสีที่สว่างที่สุดของค่าโทนสี และค่า ความอิ่มตัวสีโดยภาพปริภูมิสี HSV

โมเดลสี RGB เป็นโมเดลของแสงสี ค่าทั้งสามสีจะเปลี่ยนแปลงตามปริมาณแสง จึงทำให้เกิดความผิดพลาดขึ้น ดังนั้นจึงต้องมีการแปลงโมเดลสี RGB เป็น HSV เพื่อนำมาใช้ในการแบ่งแยกสี โดยแปลงโมเดลสี RGB เป็น HSV

sample code

import cv2

import numpy as np

import matplotlib.pyplot as plt

import math

img = cv2.imread(‘S__8585655_ro.jpg’)

img = cv2.resize(img, (512 , 512))

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_blue = np.array([50,50,50])

upper_blue = np.array([130,255,255])

mask = cv2.inRange(hsv, lower_blue, upper_blue)

# Find contours

contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

print(len(contours))

rect = cv2.minAreaRect(contours[0])

box = cv2.boxPoints(rect)

box = np.int0(box)

print(‘box’,box)

cv2.drawContours(img, [box], 0, (0,255,0), 2)

# but if you really want to use a different rectangle and rotate it, here’s how to do it

# create rectangle

x,y,w,h = cv2.boundingRect(contours[0])

print(‘x,y,w,h’,x,y,w,h)

cv2.rectangle(img,(x,y),(x+w,y+h),(155,155,0),1)

roi=img[y:y+h,x:x+w]

# show

cv2.imshow(“img”, img)

cv2.imshow(“roi”, roi)

cv2.waitKey(0)

https://nextsoftwarethailand.com/product/color-detection-and-object-recognition-machine-learning-image-opencv-python/

color detection python

หมายเหตุท้าย:
หากคุณชอบบทความนี้อย่าลืมคลิก❤ด้านล่างเพื่อแนะนำและถ้าคุณมีคำถามใด ๆ แสดงความคิดเห็นและฉันจะพยายามอย่างดีที่สุดที่จะตอบ คุณสามารถติดตามฉันบน
facebook page (https://www.facebook.com/nextsoftwarehousethailand/)
website : www.nextsoftwarethailand.com
และสามารถส่งอีเมลถึงฉัน

ขาย source code งานด้าน image processing ด้วย python, matlab
เหล้าเตยหอม — ไร่ฟ้าเปลี่ยนสี

--

--

Dr. Pathasu Doungmala
Dr. Pathasu Doungmala

Written by Dr. Pathasu Doungmala

Founder of Next Software — I am working on image processing, pattern recognition and AI to help reduce working in an industry.

No responses yet