python双重直方图_Python 2.x中两幅图像的直方图匹配?
我以前写过一个答案here解释如何在图像直方图上进行分段线性插值,以实现高光/中音/阴影的特定比率。
两幅图像之间histogram matching的基本原理相同。基本上,计算源图像和模板图像的累积直方图,然后进行线性插值,以找到模板图像中与源图像中唯一像素值的分位数最接近的唯一像素值:import numpy as np
def hist_match(source, template):
"""
Adjust the pixel values of a grayscale image such that its histogram
matches that of a target image
Arguments:
-----------
source: np.ndarray
Image to transform; the histogram is computed over the flattened
array
template: np.ndarray
Template image; can have different dimensions to source
Returns:
-----------
matched: np.ndarray
The transformed output image
"""
oldshape = source.shape
source = source.ravel()
template = template.ravel()
# get the set of unique pixel values and their corresponding indices and
# counts
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True,
return_counts=True)
t_values, t_counts = np.unique(template, return_counts=True)
# take the cumsum of the counts and normalize by the number of pixels to
# get the empirical cumulative distribution functions for the source and
# template images (maps pixel value --> quantile)
s_quantiles = np.cumsum(s_counts).astype(np.float64)
s_quantiles /= s_quantiles[-1]
t_quantiles = np.cumsum(t_counts).astype(np.float64)
t_quantiles /= t_quantiles[-1]
# interpolate linearly to find the pixel values in the template image
# that correspond most closely to the quantiles in the source image
interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)
return interp_t_values[bin_idx].reshape(oldshape)
例如:from matplotlib import pyplot as plt
from scipy.misc import lena, ascent
source = lena()
template = ascent()
matched = hist_match(source, template)
def ecdf(x):
"""convenience function for computing the empirical CDF"""
vals, counts = np.unique(x, return_counts=True)
ecdf = np.cumsum(counts).astype(np.float64)
ecdf /= ecdf[-1]
return vals, ecdf
x1, y1 = ecdf(source.ravel())
x2, y2 = ecdf(template.ravel())
x3, y3 = ecdf(matched.ravel())
fig = plt.figure()
gs = plt.GridSpec(2, 3)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1], sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(gs[0, 2], sharex=ax1, sharey=ax1)
ax4 = fig.add_subplot(gs[1, :])
for aa in (ax1, ax2, ax3):
aa.set_axis_off()
ax1.imshow(source, cmap=plt.cm.gray)
ax1.set_title('Source')
ax2.imshow(template, cmap=plt.cm.gray)
ax2.set_title('template')
ax3.imshow(matched, cmap=plt.cm.gray)
ax3.set_title('Matched')
ax4.plot(x1, y1 * 100, '-r', lw=3, label='Source')
ax4.plot(x2, y2 * 100, '-k', lw=3, label='Template')
ax4.plot(x3, y3 * 100, '--r', lw=3, label='Matched')
ax4.set_xlim(x1[0], x1[-1])
ax4.set_xlabel('Pixel value')
ax4.set_ylabel('Cumulative %')
ax4.legend(loc=5)
对于一对RGB图像,可以将此函数分别应用于每个通道。根据您试图达到的效果,您可能希望首先将图像转换为不同的颜色空间。例如,如果要匹配亮度,而不是色调或饱和度,可以转换为HSV space,然后仅在V通道上进行匹配。
python双重直方图_Python 2.x中两幅图像的直方图匹配?相关推荐
- 【python图像处理】两幅图像的合成一幅图像(blending two images)
将两幅图像合成一幅图像,是图像处理中常用的一种操作,python图像处理库PIL中提供了多种种将两幅图像合成一幅图像的接口. 下面我们通过不同的方式,将两图合并成一幅图像. 1.使用Image.ble ...
- 计算两幅图像的PSNR和SSIM以及python代码实现
欢迎关注博主的公众号:happyGirl的异想世界.有更多干货还有技术讨论群哦~ psnr是"Peak Signal to Noise Ratio"的缩写,即峰值信噪比,是一种评价 ...
- python对比两张图片的不同并圈起来,比较两幅图像/图片,并标记差异
问题1: 这篇文章展示了比较两张图片的方法.最简单的方法可能是:from PIL import Image from PIL import ImageChops im1 = Image.open(&q ...
- 【python】两幅图像融合成一幅图像
完成的目标 可以看以下的过程,比如有两张图像A和B,希望变为AB融合的图像: 代码实现[代码里面可以实现为图像添加文本描述,如果是中文,可以参考我的博文] # -*- coding:utf-8 -*- ...
- OpenCV之core 模块. 核心功能(1)Mat - 基本图像容器 OpenCV如何扫描图像、利用查找表和计时 矩阵的掩码操作 使用OpenCV对两幅图像求和(求混合(blending))
Mat - 基本图像容器 目的 从真实世界中获取数字图像有很多方法,比如数码相机.扫描仪.CT或者磁共振成像.无论哪种方法,我们(人类)看到的是图像,而让数字设备来"看"的时候,则 ...
- 如何计算两幅图像的相似度,计算两张图片相似度
怎么对比两张图片的相似度 1.首先打开微信,选择底部"发现".如图所示.2.然后在点击进入"小程序".如图所示.3.然后输入"腾讯AI体验中心&quo ...
- ITK:两幅图像之差的绝对值
ITK:两幅图像之差的绝对值 内容提要 C++实现代码 内容提要 计算两个图像中相应像素之差的绝对值. C++实现代码 #include "itkImage.h" #include ...
- 交换两幅图像的幅度谱和相位谱,并重构图像
转载自:http://blog.sina.com.cn/s/blog_909778ea0100y8ju.html 博主:科技狸的博客: 来源:新浪博客: 交换两幅图像的幅度谱和相位谱,并重构图像!!! ...
- 相机计算坐标公式_相机位姿估计3:根据两幅图像的位姿估计结果求某点的世界坐标...
关键词:相机位姿估计,单目尺寸测量,环境探知 用途:基于相机的环境测量,SLAM,单目尺寸测量 文章类型:原理说明.Demo展示 @Author:VShawn @Date:2016-11-28 @La ...
最新文章
- 用GDB调试程序(五)
- matlab最大化函数,求助,最大化一个函数
- [Job Interview] C/C plus plus Programming
- 计算机义务维修队,我院捷诚义务维修服务队电脑维修服务活动
- Mac下关闭Sublime Text 3的更新检查
- python的交互式解释器_python3.4.1解释器python交互式图形编程实例(三)
- layoutSubviews调用
- google protobuf windows下环境配置
- RPM是RedHat Package Manager(RedHat软件包管理工具)
- 河南网通帐号在线转换工具
- maven加载本地lib下的jar包
- 基于JSP和SQL的CD销售管理系统
- Microsoft JET Database Engine 错误 '80004005' 操作必须使用一个可更新的查询。问题解决办法
- 生物信息学在疾病基础研究中的应用
- iredmail mysql_iRedmail配置手册
- Go 语言入门系列:指针的基本应用
- ES6,ES7,ES8,ES9,ES10新特性一览
- FPS游戏外挂屡禁不止,如何破局?
- python 一球从100m高度落下,一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?Python实例...
- Machine.config 文件中节点machineKey的强随机生成