Opencv | 直方图均衡化

python 复制代码
import cv2 #opencv 读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt #Matplotlib是RGB
%matplotlib inline
python 复制代码
def cv_show(img,name):
    cv2.imshow(name,img)
    cv2.waitKey()
    cv2.destroyAllWindows()

cv2.calcHist(images,channels,mask,histSize,ranges)

  • images: 原图像图像格式为uint8 或 float32。当传入函数时应用中括号\[\]括来例如img

  • channels: 如果灰度图它的值就是0,如果是彩色图像的传入的参数可以是012,它们分别对应着BGR。

  • mask: 掩模图像。统整幅图像的直方图就把它为None。但是如果你想统图像某一分的直方图的你就制作一个掩模图像并使用它。

  • histSize: BIN 的数目。也应用中括号括来

  • ranges: 像素值范围常为0256

cat.jpg

python 复制代码
img = cv2.imread('cat.jpg',0)#0表示灰度图
hist = cv2.calcHist([img],[0],None,[256],[0,256])
hist.shape
python 复制代码
plt.hist(img.ravel(),256);
plt.show()

可视化运行结果:

直方图均衡化:

python 复制代码
equ = cv2.equalizeHist(img)
plt.hist(equ.ravel(),256)
plt.show()
python 复制代码
res = np.hstack((img,equ))
cv_show(res,'res')

运行结果:

自适应直方图均衡化:

python 复制代码
clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))
python 复制代码
res_clahe = clahe.apply(img)
res = np.hstack((img,equ, res_clahe))
cv_show(res,'res')

运行结果:

相关推荐
Joy T1 小时前
Spring Security 从 Acegi 到 AI Agent——JWT、角色鉴权与异步任务安全实战
人工智能·安全·spring·spring security·json web token·securitycontext·多次鉴权
AIHR数智引擎1 小时前
拆掉“关键人依赖”:黄仁勋复职全员会上的四道组织切口
人工智能·经验分享·职场和发展·ai-native·英伟达·aihr
Flynn Liang1 小时前
全栈实战:从零搭建一套可落地的生产级 AI 智能体系统(Redis 版)
人工智能·redis·ai agent
小九九的爸爸1 小时前
为了收第一笔钱,我终于打通OPC支付链路
人工智能·程序员·全栈
kyriewen2 小时前
我写的 12 个 AI Skill,三个月后 8 个在吃空饷
javascript·人工智能·程序员
陆枫Larry2 小时前
MCP 实践(二):让自己开发的 MCP Server 调用真实 HTTP 接口
人工智能
阿里云大数据AI技术2 小时前
从空间圈选到区域决策:EMR Serverless StarRocks(Stella)的 GIS 架构与空间索引实践
人工智能
掘金酱3 小时前
X-Lab 年度大会线上同步活动 | 带上你的 AI 作品,来 X-Lab 年度大会聊聊 AI 新生意!
人工智能
阿里云大数据AI技术3 小时前
Hologres 长记忆服务发布知识库:好记性,也要懂业务
人工智能
AllData公司负责人3 小时前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris