金子邦彦研究室人工知能OpenCV 4 の Python プログラムOpenCV でカラーヒストグラム(Python を使用)

OpenCV でカラーヒストグラム(Python を使用)

【サイト内の OpenCV 関連ページ】

【OpenCV の公式情報】

カラーヒストグラム

https://github.com/opencv/opencv/blob/master/samples/python/color_histogram.py で 公開されているプログラムを書き換えて使用

OpenCV による動画表示を行う.

import numpy as np
import cv2 as cv

# built-in modules
import sys

hsv_map = np.zeros((180, 256, 3), np.uint8)
h, s = np.indices(hsv_map.shape[:2])
hsv_map[:,:,0] = h
hsv_map[:,:,1] = s
hsv_map[:,:,2] = 255
hsv_map = cv.cvtColor(hsv_map, cv.COLOR_HSV2BGR)
cv.imshow('hsv_map', hsv_map)
cv.namedWindow('hist', 0)
hist_scale = 10
def set_scale(val):
    global hist_scale
    hist_scale = val

cv.createTrackbar('scale', 'hist', hist_scale, 32, set_scale)
v = cv.VideoCapture(0)
while(v.isOpened()):
    r, bgr = v.read()
    if ( r == False ):
        break
    cv.imshow('camera', bgr)
    small = cv.pyrDown(bgr)
    hsv = cv.cvtColor(small, cv.COLOR_BGR2HSV)
    dark = hsv[...,2] < 32
    hsv[dark] = 0
    h = cv.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
    h = np.clip(h*0.005*hist_scale, 0, 1)
    vis = hsv_map*h[:,:,np.newaxis] / 255.0
    cv.imshow('hist', vis)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

v.release()
cv.destroyAllWindows()

[image]