python用直方图规定化实现图像风格转换
以下内容需要直方图均衡化、规定化知识
均衡化:https://blog.csdn.net/macunshi/article/details/79815870
规定化:https://blog.csdn.net/macunshi/article/details/79819263
直方图均衡化应用:
图像直方图均衡化能拉伸灰度图,让像素值均匀分布在0,255之间,使图像看起来不会太亮或太暗,常用于图像增强;
直方图规定化应用:
举个例子,当我们需要对多张图像进行拼接时,我们希望这些图片的亮度、饱和度保持一致,事实上就是让它们的直方图分布一致,这时就需要直方图规定化。
直方图规定化与均衡化的思想一致,事实上就是找到各个灰度级别的映射关系。具体实现的过程中一般会选一个参考图像记为A,找到A的直方图与目标图像的直方图的映射关系,从而找到目标图像的像素以A为“参考”时的映射关系。
具体实现可参考文中链接(看完茅塞顿开)
基于python利用直方图规定化统一图像风格
参考图像

原始图像(第一行)/处理后的图像(第二行)
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
源码:
import os
import cv2
import numpy as np def get_map(Hist):
# 计算概率分布Pr
sum_Hist = sum(Hist)
Pr = Hist/sum_Hist
# 计算累计概率Sk
Sk = []
temp_sum = 0
for n in Pr:
temp_sum = temp_sum + n
Sk.append(temp_sum)
Sk = np.array(Sk)
# 计算映射关系img_map
img_map = []
for m in range(256):
temp_map = int(255*Sk[m] + 0.5)
img_map.append(temp_map)
img_map = np.array(img_map)
return img_map def get_off_map(map_): # 计算反向映射,寻找最小期望
map_2 = list(map_)
off_map = []
temp_pre = 0 # 如果循环开始就找不到映射时,默认映射为0
for n in range(256):
try:
temp1 = map_2.index(n)
temp_pre = temp1
except BaseException:
temp1 = temp_pre # 找不到映射关系时,近似取向前最近的有效映射值
off_map.append(temp1)
off_map = np.array(off_map)
return off_map def get_infer_map(infer_img):
infer_Hist_b = cv2.calcHist([infer_img], [0], None, [256], [0,255])
infer_Hist_g = cv2.calcHist([infer_img], [1], None, [256], [0,255])
infer_Hist_r = cv2.calcHist([infer_img], [2], None, [256], [0,255])
infer_b_map = get_map(infer_Hist_b)
infer_g_map = get_map(infer_Hist_g)
infer_r_map = get_map(infer_Hist_r)
infer_b_off_map = get_off_map(infer_b_map)
infer_g_off_map = get_off_map(infer_g_map)
infer_r_off_map = get_off_map(infer_r_map)
return [infer_b_off_map, infer_g_off_map, infer_r_off_map] def get_finalmap(org_map, infer_off_map): # 计算原始图像到最终输出图像的映射关系
org_map = list(org_map)
infer_off_map = list(infer_off_map)
final_map = []
for n in range(256):
temp1 = org_map[n]
temp2 = infer_off_map[temp1]
final_map.append(temp2)
final_map = np.array(final_map)
return final_map def get_newimg(img_org, org2infer_maps):
w, h, _ = img_org.shape
b, g ,r =cv2.split(img_org)
for i in range(w):
for j in range(h):
temp1 = b[i,j]
b[i,j] = org2infer_maps[0][temp1]
for i in range(w):
for j in range(h):
temp1 = g[i,j]
g[i,j] = org2infer_maps[1][temp1]
for i in range(w):
for j in range(h):
temp1 = r[i,j]
r[i,j] = org2infer_maps[2][temp1]
newimg = cv2.merge([b,g,r])
return newimg def get_new_img(img_org, infer_map):
org_Hist_b = cv2.calcHist([img_org], [0], None, [256], [0,255])
org_Hist_g = cv2.calcHist([img_org], [1], None, [256], [0,255])
org_Hist_r = cv2.calcHist([img_org], [2], None, [256], [0,255])
org_b_map = get_map(org_Hist_b)
org_g_map = get_map(org_Hist_g)
org_r_map = get_map(org_Hist_r)
org2infer_map_b = get_finalmap(org_b_map, infer_map[0])
org2infer_map_g = get_finalmap(org_g_map, infer_map[1])
org2infer_map_r = get_finalmap(org_r_map, infer_map[2])
return get_newimg(img_org, [org2infer_map_b, org2infer_map_g, org2infer_map_r]) if __name__ == "__main__":
dstroot = './imgs'
infer_img_path = './abc.png'
infer_img = cv2.imread(infer_img_path)
outroot = './out1'
infer_map = get_infer_map(infer_img) # 计算参考映射关系
dstlist = os.listdir(dstroot)
for n in dstlist:
img_path = os.path.join(dstroot, n)
print(img_path)
img_org = cv2.imread(img_path)
new_img = get_new_img(img_org, infer_map) # 根据映射关系获得新的图像
new_path = os.path.join(outroot, n)
cv2.imwrite(new_path, new_img)
python用直方图规定化实现图像风格转换的更多相关文章
- Ubuntu16.04+GTX1080配置TensorFlow并实现图像风格转换
1. TensorFlow TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,表达了高层次的机器学习计算,大幅简化了第一代系统,并且具备更好的灵活性和可延展性. Te ...
- Python之数据规整化:清理、转换、合并、重塑
Python之数据规整化:清理.转换.合并.重塑 1. 合并数据集 pandas.merge可根据一个或者多个不同DataFrame中的行连接起来. pandas.concat可以沿着一条轴将多个对象 ...
- A Neural Algorithm of Artistic Style 图像风格转换 - keras简化版实现
前言 深度学习是最近比较热的词语.说到深度学习的应用,第一个想到的就是Prisma App的图像风格转换.既然感兴趣就直接开始干,读了论文,一知半解:看了别人的源码,才算大概了解的具体的实现,也惊叹别 ...
- 「新手必看」Python+Opencv实现摄像头调用RGB图像并转换成HSV模型
在ROS机器人的应用开发中,调用摄像头进行机器视觉处理是比较常见的方法,现在把利用opencv和python语言实现摄像头调用并转换成HSV模型的方法分享出来,希望能对学习ROS机器人的新手们一点帮助 ...
- 【神经网络与深度学习】neural-style、chainer-fast-neuralstyle图像风格转换使用
neural-style 官方地址:这个是使用torch7实现的;torch7安装比较麻烦.我这里使用的是大神使用TensorFlow实现的https://github.com/anishathaly ...
- fast neural style transfer图像风格迁移基于tensorflow实现
引自:深度学习实践:使用Tensorflow实现快速风格迁移 一.风格迁移简介 风格迁移(Style Transfer)是深度学习众多应用中非常有趣的一种,如图,我们可以使用这种方法把一张图片的风格“ ...
- 神经风格转换Neural Style Transfer a review
原文:http://mp.weixin.qq.com/s/t_jknoYuyAM9fu6CI8OdNw 作者:Yongcheng Jing 等 机器之心编译 风格迁移是近来人工智能领域内的一个热门研究 ...
- [python-opencv]图像二值化【图像阈值】
图像二值化[图像阈值]简介: 如果灰度图像的像素值大于阈值,则为其分配一个值(可以是白色255),否则为其分配另一个值(可以是黑色0) 图像二值化就是将灰度图像上的像素值设置为0或255,也就是将整个 ...
- Win8Metro(C#)数字图像处理--2.34直方图规定化
原文:Win8Metro(C#)数字图像处理--2.34直方图规定化 [函数名称] WriteableBitmap HistogramSpecificateProcess(WriteableBi ...
随机推荐
- Java实现 LeetCode 773 滑动谜题(BFS)
773. 滑动谜题 在一个 2 x 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示, 以及一块空缺用 0 来表示. 一次移动定义为选择 0 与一个相邻的数字(上下左右)进行交换. 最终 ...
- Java实现 LeetCode 640 求解方程(计算器的加减法计算)
640. 求解方程 求解一个给定的方程,将x以字符串"x=#value"的形式返回.该方程仅包含'+',' - '操作,变量 x 和其对应系数. 如果方程没有解,请返回" ...
- Java中构造方法的详细介绍
构造方法是一个特殊的方法 它会在实例化对象时自动调用 构造方法的定义 必须同时满足下面的三个条件 方法名与类名相同 方法名前面没有返回值类型的声明 在方法中不能使用return语句返回值 class ...
- Shell中傻傻分不清楚的TOP3
Shell中傻傻分不清楚的TOP3 发布文章 近来小姐姐又犯憨憨错误,问组内小伙伴export命令不会持久化环境变量吗?反正我是问出口了..然后小伙伴就甩给了我一个<The Linux Comm ...
- HDFS ha 格式化报错:a shared edits dir must not be specified if HA is not enabled.
错误内容: Formatting using clusterid: CID-19921335-620f-4e72-a056-899702613a6b2019-01-12 07:28:46,986 IN ...
- [xDebug]Xdebug和Sublime调试PHP代码
安装xdebug 省略... 配置sublime 要调试某一个项目,首先得把这个项目在sublime下保存成一个project sublime->project->save project ...
- abp-CMS模块-广告
无论是开发app还是网站,可能都需要一个广告功能,比如我们常见的在首页有个轮播广告,里面会轮播显示多个图片.还有比如一个新闻门户网站 很常见的 banner横幅广告,还有js特效广告等.本篇说说在ab ...
- @atcoder - AGC024F@ Simple Subsequence Problem
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定由若干长度 <= N 的 01 字符串组成的集合 S. ...
- TensorFlow从0到1之TensorFlow多层感知机实现MINIST分类(22)
TensorFlow 支持自动求导,可以使用 TensorFlow 优化器来计算和使用梯度.它使用梯度自动更新用变量定义的张量.本节将使用 TensorFlow 优化器来训练网络. 前面章节中,我们定 ...
- python第三方库大全
环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具.官网 pyenv:简单的 Python 版本管理工具.官网 Vex:可以在虚拟环境中执行命令.官网 v ...







