Help us understand the problem. What is going on with this article?

Python + OpenCVによる色情報の取得

画像の特定範囲の色平均情報(RGB、HSV)の取得方法

test.py
import cv2
import os

# 対象画像読み込み
img = cv2.imread("testImg.png",cv2.IMREAD_COLOR)

# 対象範囲を切り出し
boxFromX = 20 #対象範囲開始位置 X座標
boxFromY = 40 #対象範囲開始位置 Y座標
boxToX = 80 #対象範囲終了位置 X座標
boxToY = 60 #対象範囲終了位置 Y座標
# y:y+h, x:x+w の順で設定
imgBox = img[boxFromY: boxToY, boxFromX: boxToX]

# RGB平均値を出力
# flattenで一次元化しmeanで平均を取得 
b = imgBox.T[0].flatten().mean()
g = imgBox.T[1].flatten().mean()
r = imgBox.T[2].flatten().mean()

# RGB平均値を取得
print("B: %.2f" % (b))
print("G: %.2f" % (g))
print("R: %.2f" % (r))

# BGRからHSVに変換
imgBoxHsv = cv2.cvtColor(imgBox,cv2.COLOR_RGB2HSV)

# HSV平均値を取得
# flattenで一次元化しmeanで平均を取得 
h = imgBoxHsv.T[0].flatten().mean()
s = imgBoxHsv.T[1].flatten().mean()
v = imgBoxHsv.T[2].flatten().mean()

# HSV平均値を出力
# uHeは[0,179], Saturationは[0,255],Valueは[0,255]
print("Hue: %.2f" % (h))
print("Salute: %.2f" % (s))
print("Value: %.2f" % (v))

上記を実行すると、色情報が以下のように出力される

B: 87.23
G: 82.33
R: 188.25
Hue: 121.54
Salute: 150.21
Value: 188.25
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away