OpenCV__Python直方图_教程13

1、显示直方图

用到了matlab画图模块matplotlib

还没安装的话:

pip install matplotlib

用到了函数

典型代码如下

#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#引入matplotlib模块
from matplotlib import pyplot as plt


#绘制直方图
def image_hist(img):
    color = ('blue','green','red')
    for i,color in enumerate(color):
        hist = cv.calcHist([img],[i],None,[256],[0,256])
        plt.plot(hist,color=color)
        plt.xlim([0,256])
    plt.show() 



def img_test():
    img = cv.imread('E:/chenopencvblogimg/lena.jpg')
    #判断是否读取成功
    if img is None:
        print("Could not read the image,may be path error")
        return
    cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
    cv.imshow("origin Pic",img)

    image_hist(img) 

    #让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
    cv.waitKey(0)
    #销毁窗口
    cv.destroyAllWindows()

if __name__ == '__main__':
    sys.exit(img_test() or 0)

2、直方图均衡化 

#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#引入matplotlib模块
from matplotlib import pyplot as plt


#直方图均衡化,用在灰度图上
def euqal_hist(img):
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    dst = cv.equalizeHist(gray)
    return dst


def img_test():
    img = cv.imread('E:/chenopencvblogimg/equalhist.jpg')
    #判断是否读取成功
    if img is None:
        print("Could not read the image,may be path error")
        return
    cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
    cv.imshow("origin Pic",img)

    img_show = euqal_hist(img) 
    cv.namedWindow("equal_hist",cv.WINDOW_NORMAL)
    cv.imshow("equal_hist",img_show)
    #让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
    cv.waitKey(0)
    #销毁窗口
    cv.destroyAllWindows()

if __name__ == '__main__':
    sys.exit(img_test() or 0)

这幅测试图的效果是夸张的好 

 

3.局部直方图均衡化

 

注意:参数可以自己选择和设置,可以不用默认!!! 

#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#引入matplotlib模块
from matplotlib import pyplot as plt


#直方图均衡化,用在灰度图上
def euqal_hist(img):
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    dst = cv.equalizeHist(gray)
    return dst


#局部直方图均衡化,用在灰度图上
def clahe_hist(img):
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    clahe = cv.createCLAHE()
    #clahe = cv.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))
    dst = clahe.apply(gray)
    return dst


def img_test():
    img = cv.imread('E:/chenopencvblogimg/equalhist.jpg')
    #判断是否读取成功
    if img is None:
        print("Could not read the image,may be path error")
        return
    cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
    cv.imshow("origin Pic",img)

    img_show = euqal_hist(img) 
    cv.namedWindow("equal_hist",cv.WINDOW_NORMAL)
    cv.imshow("equal_hist",img_show)
    img_show = clahe_hist(img) 
    cv.namedWindow("clahe_hist",cv.WINDOW_NORMAL)
    cv.imshow("clahe_hist",img_show)


    #让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
    cv.waitKey(0)
    #销毁窗口
    cv.destroyAllWindows()

if __name__ == '__main__':
    sys.exit(img_test() or 0)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值