python直方图教程_OpenCV Python直方图教程13,OpenCVPython
发布日期:2021-06-24 12:43:28 浏览次数:2 分类:技术文章

本文共 2547 字,大约阅读时间需要 8 分钟。

1、显示直方图

用到了matlab画图模块matplotlib

还没安装的话:

pip install matplotlib

用到了函数

477c64e1947f9ea90b8effab65f53722.png

典型代码如下

#引入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)

f15a2f99506e6f0dedccb4e6df1cc648.png

2、直方图均衡化

57edbf56097e15bc08695bddee64436a.png

#引入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)

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

eaaac2d376858f30fee6451122e57059.png

3.局部直方图均衡化

7ba1d9f8374118ce499ceae91102a3d8.png

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

#引入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)

94f703dac9cfe1f66896bf636827ad7e.png

转载地址:https://blog.csdn.net/weixin_32820131/article/details/114452701 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:weixin java tools_weixin-java-tools(微信开发工具包)
下一篇:java 虚拟方法_Java中的虚拟方法virtual method是什么含义

发表评论

最新留言

很好
[***.229.124.182]2024年04月14日 08时34分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章