opencv-python绘制图像颜色直方图

#-*- encoding:utf-8 -*-
'''
python 绘制颜色直方图,cv2有hist和calcHist两种方法
'''
import cv2
import numpy as np
from matplotlib import pyplot as plt

def fun1():
    img = cv2.imread('../../data/home.jpg',cv2.IMREAD_GRAYSCALE)
    #bins->图像中分为多少格;range->图像中数字范围
    plt.hist(img.ravel(), bins=256, range=[0, 256]);
    plt.show()

def fun2():
    img = cv2.imread('../../data/home.jpg',cv2.IMREAD_COLOR)
    color = ('b', 'g', 'r')
    for i, col in enumerate(color):
        histr = cv2.calcHist([img], [i], None, [256], [0, 256])
        plt.plot(histr, color=col)
    plt.xlim([0, 256])
    plt.show()

fun2()

opencv-python绘制图像颜色直方图_第1张图片opencv-python绘制图像颜色直方图_第2张图片


你可能感兴趣的:(图像处理)