直方图匹配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

相关文章

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

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

关于Gateway路由匹配规则解读

《关于Gateway路由匹配规则解读》本文详细介绍了SpringCloudGateway的路由匹配规则,包括基本概念、常用属性、实际应用以及注意事项,路由匹配规则决定了请求如何被转发到目标服务,是Ga... 目录Gateway路由匹配规则一、基本概念二、常用属性三、实际应用四、注意事项总结Gateway路由

一文带你理解Python中import机制与importlib的妙用

《一文带你理解Python中import机制与importlib的妙用》在Python编程的世界里,import语句是开发者最常用的工具之一,它就像一把钥匙,打开了通往各种功能和库的大门,下面就跟随小... 目录一、python import机制概述1.1 import语句的基本用法1.2 模块缓存机制1.

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

hdu 3065 AC自动机 匹配串编号以及出现次数

题意: 仍旧是天朝语题。 Input 第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。 接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。 在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。

二分最大匹配总结

HDU 2444  黑白染色 ,二分图判定 const int maxn = 208 ;vector<int> g[maxn] ;int n ;bool vis[maxn] ;int match[maxn] ;;int color[maxn] ;int setcolor(int u , int c){color[u] = c ;for(vector<int>::iter

POJ 3057 最大二分匹配+bfs + 二分

SampleInput35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSampleOutput321impossible

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

OmniGlue论文详解(特征匹配)

OmniGlue论文详解(特征匹配) 摘要1. 引言2. 相关工作2.1. 广义局部特征匹配2.2. 稀疏可学习匹配2.3. 半稠密可学习匹配2.4. 与其他图像表示匹配 3. OmniGlue3.1. 模型概述3.2. OmniGlue 细节3.2.1. 特征提取3.2.2. 利用DINOv2构建图形。3.2.3. 信息传播与新的指导3.2.4. 匹配层和损失函数3.2.5. 与Super

二分图的最大匹配——《啊哈!算法》

二分图 如果一个图的所有顶点可以被分为X和Y两个集合,并且所有边的两个顶点恰好一个属于X,另外一个属于Y,即每个集合内的顶点没有边相连,那么此图就是二分图。 二分图在任务调度、工作安排等方面有较多的应用。 判断二分图:首先将任意一个顶点着红色,然后将其相邻的顶点着蓝色,如果按照这样的着色方法可以将全部顶点着色的话,并且相邻的顶点着色不同,那么该图就是二分图。 java