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 ...
随机推荐
- jchdl - 门和开关层(GSL)
https://mp.weixin.qq.com/s/dcBfMLOuaFtrk6i149vIVQ 第一部分 静态建模:拓扑模型 GSL层拓扑建模相对简单,由线和节点组成: 线连接各个节点: ...
- webpack+vue2.0项目 (一) vue-cli脚手架
很早以前就开始看vue2.0和webpack,但总是留不下深刻的印象,一直缺少一个可以贯通的项目,而且工作也没有时间,最近辞职在家,从网上找了个项目,写了大概八天,踩了无数的坑啊!! 下载的项目包括, ...
- Java实现 蓝桥杯VIP 算法训练 瓷砖铺放
[题目描述]: 有一长度为N(1< =N< =10)的地板,给定两种不同瓷砖:一种长度为1,另一种长度为2,数目不限.要将这个长度为N的地板铺满,一共有多少种不同的铺法? 例如,长度为4的 ...
- java代码(8) ---guava字符串工具
guava字符串工具 一.Joiner 根据指定的分隔符把字符串连接在一起,MapJoiner执行相同的操作,但是针对Map的key和value 分析源码可知:该类构造方法被private修饰,无法直 ...
- Cookie默认不设置path时,哪些请求会携带cookie数据
默认不设置path的时候,只会在请求和servlet同路径的情况下才会携带cookie中存储的数据,包含同级目录和下级目录 例如: 在http://localhost:8080/day01/test/ ...
- 构建自己的jar包上传至Mvaen中央仓库和版本更新
构建自己的jar包上传至Mvaen中央仓库和版本更新 一直羡慕别人制造轮子,开源项目,供别人使用:我也想这样,可以自己才疏学浅,本次就将自己写小工具上传到Maven的中央仓库. 一步一步详细教程演示如 ...
- Unable to open debugger port (127.0.0.1:55119): java.net.SocketException "Socket closed"
1.端口问题 排查端口,lsof -i:8080 修改端口等 2.权限问题 端口排查无解的话,查看idea Event Log(View->Tool Window->Event Log) ...
- while or if
多线程 wait && notifyAll 模式实现时,如果 锁中有判断,对共享对象有curd 操作时,有可能出现异常 即,判断 条件 这个时候关键字有 if 改为while 即可 ...
- 循环使用 v-for 指令。
循环语句 循环使用 v-for 指令. v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名. v-for 可以绑定数据到 ...
- @bzoj - 5104@ Fib数列
目录 @description@ @solution@ @accepted code@ @details@ @description@ Fib数列为1,1,2,3,5,8... 求在Mod10^9+9 ...







