直方图匹配from skimage.exposure import match_histograms

2024-05-11 11:28

本文主要是介绍直方图匹配from skimage.exposure import match_histograms,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

from skimage.exposure import match_histograms

match_histograms 的实现非常简洁有效。直方图匹配或者直方图规定化

import cv2
import numpy as np
from matplotlib import pyplot as pltdef match_histograms(image, reference, *, channel_axis=None):"""Adjust an image so that its cumulative histogram matches that of another.The adjustment is applied separately for each channel.Parameters----------image : ndarrayInput image. Can be gray-scale or in color.reference : ndarrayImage to match histogram of. Must have the same number of channels asimage.channel_axis : int or None, optionalIf None, the image is assumed to be a grayscale (single channel) image.Otherwise, this parameter indicates which axis of the array correspondsto channels.Returns-------matched : ndarrayTransformed input image.Raises------ValueErrorThrown when the number of channels in the input image and the referencediffer.References----------.. [1] http://paulbourke.net/miscellaneous/equalisation/"""print(image.ndim, reference.ndim)if image.ndim != reference.ndim:raise ValueError('Image and reference must have the same number ''of channels.')if channel_axis is not None:if image.shape[-1] != reference.shape[-1]:raise ValueError('Number of channels in the input image and ''reference image must match!')matched = np.empty(image.shape, dtype=image.dtype)for channel in range(image.shape[-1]):matched_channel = _match_cumulative_cdf(image[..., channel],reference[..., channel])matched[..., channel] = matched_channelelse:# _match_cumulative_cdf will always return float64 due to np.interpmatched = _match_cumulative_cdf(image, reference)# if matched.dtype.kind == 'f':#     # output a float32 result when the input is float16 or float32#     out_dtype = utils._supported_float_type(image.dtype)#     matched = matched.astype(out_dtype, copy=False)return matched
def _match_cumulative_cdf(source, template):"""Return modified source array so that the cumulative density function ofits values matches the cumulative density function of the template."""print(source.dtype.kind)if source.dtype.kind == 'u':src_lookup = source.reshape(-1)src_counts = np.bincount(src_lookup)tmpl_counts = np.bincount(template.reshape(-1))print(src_lookup.shape, src_lookup.dtype, src_counts.shape, src_counts.dtype, tmpl_counts.shape, tmpl_counts.dtype)print(tmpl_counts.shape)# omit values where the count was 0tmpl_values = np.nonzero(tmpl_counts)[0]tmpl_counts = tmpl_counts[tmpl_values]print(tmpl_values.shape, tmpl_counts.shape)else:src_values, src_lookup, src_counts = np.unique(source.reshape(-1),return_inverse=True,return_counts=True)tmpl_values, tmpl_counts = np.unique(template.reshape(-1),return_counts=True)# calculate normalized quantiles for each arraysrc_quantiles = np.cumsum(src_counts) / source.sizetmpl_quantiles = np.cumsum(tmpl_counts) / template.size# 0-255的像素值应该变为多少interp_a_values = np.interp(src_quantiles, tmpl_quantiles, tmpl_values)return interp_a_values[src_lookup].reshape(source.shape)if __name__ == "__main__":file1 = r'D:\code\3.jpg'file2 = r'D:\code\1.jpg'img1 = cv2.imread(file1, 0)img2 = cv2.imread(file2, 0)out = match_histograms(img1, img2)plt.figure()#plt.imshow(np.hstack((img1, img2, out))[...,::-1]/255)plt.imshow(np.hstack((img1, img2, out)), 'gray')plt.show()

这篇关于直方图匹配from skimage.exposure import match_histograms的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979400

相关文章

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

python之流程控制语句match-case详解

《python之流程控制语句match-case详解》:本文主要介绍python之流程控制语句match-case使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录match-case 语法详解与实战一、基础值匹配(类似 switch-case)二、数据结构解构匹

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在