opencv学习笔记---直方图笔记 compareHIST

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


def create_rgb_hist(image):
    h, w, c = image.shape
    rgHist = np.zeros([16*16*16, 1], np.float32)//必须是float型
    print(rgHist)
    hsize = 256/16
    for row in range (0, h, 1):
        for col in range (0, w, 1):
            b = image[row, col, 0]
            g = image[row, col, 1]
            r = image[row, col, 2]
            index = np.int(b/hsize)*16*16 + np.int(g/hsize)*16 + np.int(r/hsize)
            rgHist[np.int(index), 0] = rgHist[np.int(index), 0] + 1
    return rgHist


def hist_compare(image1, image2):
    hist1 = create_rgb_hist(image1)
    hist2 = create_rgb_hist(image2)
    match1 = cv.compareHist(hist1, hist2, cv.HISTCMP_BHATTACHARYYA)
    match2 = cv.compareHist(hist1, hist2, cv.HISTCMP_CORREL)
    match3 = cv.compareHist(hist1, hist2, cv.HISTCMP_CHISQR)
    print("巴氏距离:%s,相关性:%s,卡方:%s" % (match1, match2, match3))

def hist_image(image):
    color = ("blue", "green", "red")
    for i, color in enumerate(color):
        hist = cv.calcHist([image], [i], None, [256], [0, 256])
    print(hist)


image1 = cv.imread("D://work//demo.jpg")
image2 = cv.imread("D://work//img.jpg")
#hist_image(image1)
hist_compare(image1, image2)

猜你喜欢

转载自blog.csdn.net/qq_41603898/article/details/82049972
今日推荐