python opencv HSV色空間の応用

18494 ワード

HSV色成分範囲は、一般に色空間の画像を有効に処理するのにHSV空間で行われ、その後、基本色に対応するHSV成分に対して厳密な範囲を与える必要があり、以下は実験によって計算された曖昧な範囲(正確な範囲はネット上では与えられていない)である.H:0-180 S:0-255 V:0-255ここで一部の赤を紫色の範囲にまとめる:

グレー


オレンジ



ブルー

hmin
0
0
0
0
156
11
26
35
78
100
125
hmax
180
180
180
10
180
25
34
77
99
124
155
smin
0
0
0
43
43
43
43
43
43
43
smax
255
43
30
255
255
255
255
255
255
255
vmin
0
46
221
46
46
46
46
46
46
46
vmax
46
220
255
255
255
255
255
255
255
255
1、カラーまたはモノクロのカラー画像を判断する
# -*- coding: utf-8 -*-  
import cv2 as cv
import numpy as np

class MatchIdCard:
    def ComputeHistCorrel(image): #     RGB       
        b_hist = cv.calcHist([image], [0], None, [256], [0, 256])
        g_hist = cv.calcHist([image], [1], None, [256], [0, 256])
        r_hist = cv.calcHist([image], [2], None, [256], [0, 256])

        b_2_g = cv.compareHist(b_hist, g_hist, cv.HISTCMP_CORREL)
        g_2_r = cv.compareHist(g_hist, r_hist, cv.HISTCMP_CORREL)
        r_2_b = cv.compareHist(r_hist, b_hist, cv.HISTCMP_CORREL)

        correl_abs = np.abs(b_2_g - g_2_r) + np.abs(g_2_r - r_2_b) + np.abs(r_2_b - b_2_g)
        return correl_abs

    def ComputeCyan(image, kernel_dilate):
        hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
        lower_hsv = np.array([78, 43, 46])
        upper_hsv = np.array([99, 255, 255])
        mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
        dst_dilate = cv.dilate(mask, kernel_dilate)
        contours, _ = cv.findContours(dst_dilate, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        count = 0
        for i, contour in enumerate(contours):
            if cv.contourArea(contour) > 60: count += 1
        return count

    def ComputeRed(image, kernel_dilate):
        hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
        lower_hsv = np.array([0, 43, 46])
        upper_hsv = np.array([10, 255, 255])
        mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
        dst_dilate = cv.dilate(mask, kernel_dilate)
        contours, _ = cv.findContours(dst_dilate, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        count = 0
        for i, contour in enumerate(contours):
            if cv.contourArea(contour) > 60: count += 1
        return count

    def ComputeHSV(image):
        kernel_dilate = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
        count_contour_cyan = MatchIdCard.ComputeCyan(image, kernel_dilate)
        count_contoru_red = MatchIdCard.ComputeRed(image, kernel_dilate)
        return count_contour_cyan + count_contoru_red

    def Compute(img_path):
        src = cv.imread(img_path)
        src = cv.resize(src, (224, 224), interpolation=cv.INTER_AREA)
        flag = False

        count = MatchIdCard.ComputeHSV(src)
        if count > 0:
            flag = True
        return flag


path = r"C:\Users\RoseC\Desktop\picture\20190917192429.jpg"
out = MatchIdCard.Compute(path)
print(out)


2,露光アルゴリズムの計算
# -*- coding: utf-8 -*-  
import cv2 as cv

def cv_exposure(img_path):
    gray = cv.imread(img_path, 0)
    cv.imshow('gray', gray)
    ret, mask_bin = cv.threshold(gray, 250, 255, cv.THRESH_BINARY)
    print(ret)

    cv.imshow('mask_bin', mask_bin)


    cv.waitKey(0)
    cv.destroyAllWindows()

img_path = r"C:\Users\RoseC\Desktop\front_15650064325615836658.jpg"
cv_exposure(img_path)