# -*- coding: utf-8 -*-
"""
Created on Fri Jun 11 06:24:33 2021@author: 陨星落云
csdn博客地址: https://blog.csdn.net/qq_28368377"""
import imageio
import cupy as cp
import numpy as np
import matplotlib.pylab as plt
import matplotlib
matplotlib.rcParams['font.family']='SimHei'
import time def compute_histogram(img):# 计算直方图tab = np.zeros(256)for i in np.unique(img):# print(i)tab[i] = np.bincount(img.flatten())[i]return tab.astype(np.uint64).tolist()def hist_matching_cpu(img,target):# 直方图匹配CPU版 img:原图,targettraget:匹配的图像hist0,_ = np.histogram(target.flatten(),256,range=(0,255))cdf_dest = hist0.cumsum()/target.sizeimhist,bins = np.histogram(img.flatten(),len(cdf_dest),density=True)cdf = imhist.cumsum()cdf = cdf/cdf[-1]im2 = np.interp(img.flatten(),bins[:-1],cdf)im3 = np.interp(im2,cdf_dest,bins[:-1])imres = im3.reshape(img.shape[:2])return (imres*255).astype(np.uint8) def hist_matching_gpu(img,target):# 直方图匹配GPU版img,target = cp.array(img),cp.array(target)        hist0,_ = cp.histogram(target.flatten(),256,range=(0,255))cdf_dest = hist0.cumsum()/target.sizeimhist,bins = np.histogram(img.flatten(),len(cdf_dest),density=True)cdf = imhist.cumsum()cdf = cdf/cdf[-1]im2 = cp.interp(img.flatten(),bins[:-1],cdf)im3 = cp.interp(im2,cdf_dest,bins[:-1])imres = cp.asnumpy(im3.reshape(img.shape[:2]))return (imres*255).astype(np.uint8) if __name__ == "__main__":# 读取图像img = imageio.imread("Unequalized.jpg")# target = (np.ones(100).reshape(10,10)*205).astype(np.uint8)target = imageio.imread("gray_lena.jpg")# 直方图匹配CPU版t0 = time.time()img1 = hist_matching_cpu(img,target)print("直方图匹配CPU所需时间:",time.time()-t0)# 直方图匹配GPU版t1 = time.time()img2 = hist_matching_gpu(img,target)print("直方图匹配GPU所需时间:",time.time()-t1)# 显示图像plt.figure(figsize=(20,16))plt.subplot(231)plt.imshow(img,cmap='gray')plt.title('原图')plt.subplot(232)plt.imshow(target,cmap='gray')plt.title('参考图')plt.subplot(233)plt.imshow(img2,cmap='gray')plt.title('结果图')plt.subplot(234)# print(compute_histogram(img))plt.plot(compute_histogram(img))plt.title('原图的直方图')plt.subplot(235)# print(compute_histogram(image_stretch(img)))plt.plot(compute_histogram(target))plt.title('参考的直方图')plt.subplot(236)# print(compute_histogram(image_stretch(img)))plt.plot(compute_histogram(img2))plt.title('结果的直方图')plt.show()

结果:

直方图匹配CPU所需时间: 0.05585050582885742
直方图匹配GPU所需时间: 0.0329129695892334

手动实现直方图匹配(python)相关推荐

  1. OpenCV基础(17)基于OpenCV、scikit-image和Python的直方图匹配

    在本教程中,您将学习如何使用OpenCV和scikit-image进行直方图匹配. 上周我们讨论了直方图均衡化,这是一种基本的图像处理技术,可以提高输入图像的对比度. 但是,如果你想自动匹配两幅图像的 ...

  2. python图像处理:直方图的规定化(直方图匹配)

    写在前面 因为笔者数字图像处理的作业是要求用VB来做规定化的处理,笔者写出来后想看看python有什么库可以实现,毕竟像均衡化之类的操作都可以通过py的cv2库里的函数解决,但是在CSDN上查询的时候 ...

  3. Python实现图像直方图规定化(直方图匹配)-附完整代码

    以下内容需要直方图均衡化.规定化知识 均衡化:https://blog.csdn.net/macunshi/article/details/79815870 规定化:https://blog.csdn ...

  4. 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波9 - 直方图处理 - 直方图匹配(规定化)灰度图像,彩色图像都适用

    直方图匹配(规定化) 连续灰度 s=T(r)=(L−1)∫0rpr(w)dw(3.17)s = T(r) = (L-1) \int_{0}^{r} p_r(w) \text{d} w \tag{3.1 ...

  5. python库skimage 绘制直方图;绘制累计直方图;实现直方图匹配(histogram matching)

    绘制直方图 from skimage import exposure # 绘制彩色图像的c通道的直方图 img_hist, bins = exposure.histogram(img[..., c], ...

  6. 基于OpenCV的直方图匹配

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 如何为图像生成直方图,如何使直方图相等,最后如何将图像直方图修改为 ...

  7. 【OpenCV 例程200篇】48. 图像增强—彩色直方图匹配

    [OpenCV 例程200篇]48. 图像增强-彩色直方图匹配 欢迎关注 『OpenCV 例程200篇』 系列,持续更新中 欢迎关注 『Python小白的OpenCV学习课』 系列,持续更新中 图像直 ...

  8. 【OpenCV 例程200篇】47. 图像增强—直方图匹配

    [OpenCV 例程200篇]47. 图像增强-直方图匹配 欢迎关注 『OpenCV 例程200篇』 系列,持续更新中 欢迎关注 『Python小白的OpenCV学习课』 系列,持续更新中 图像直方图 ...

  9. 数字图像处理:直方图匹配或规定化Histogram Matching (Specification)原理及感悟

    ☞ ░ 前往老猿Python博文目录 https://blog.csdn.net/LaoYuanPython ░ 注意:本文是<数字图像直方图匹配或规定化Histogram Matching ( ...

  10. 在OpenCV下写的直方图匹配(直方图规定化)C++源码!

    直方图匹配的原理就不多作解释了,我曾经还将MATLAB源码改写成过C源码,详情可见我的博文 根据MATLAB的histeq函数改写的运行在OpenCV下的直方图规定化C源码! 本文已转移到 https ...

最新文章

  1. 用 Label 控制 Service 的位置 - 每天5分钟玩转 Docker 容器技术(106)
  2. zz[as3 hack技术]垃圾回收机强制执行
  3. oracle 格式化报表输出,perl的格式化(Format)报表输出实现代码
  4. Linux目录详解,软件应该安装到哪个目录
  5. 入门 | 32 个常用 Python 实现
  6. ES6.X,你必须知道的API和相关技巧
  7. Linux系统安装Apache 2.4.6
  8. 幸福秘诀 男女必须要看哦
  9. java从键盘输入一个数,并将其倒序输出
  10. 关于SSH的分工(网友讨论集合贴)
  11. opencv 凸包讲解和绘制
  12. 编程语言学习之php
  13. 实测:游戏情景中,远控软件实力如何?一篇告诉你ToDesk的强大之处
  14. win7、10系统怎么设置打印机共享(打印机USB接口共享)
  15. 哈佛大学公开课-幸福课-个人笔记
  16. 腾讯CJ放出大招!次世代手游大作曝光,画面堪比3A大作
  17. 安装服务器系统后鼠标键盘没反应,安装Win7系统以后键盘鼠标不能用/失灵没反应的缘由以及解决方法...
  18. Python-身体质量指数BMI
  19. buuctf刷题记录(6)
  20. 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method “javax.servl

热门文章

  1. 【window】重启IIS服务
  2. 使用c#制作赛尔号登录器
  3. 能量时域空间物理_定性半定量物理学——1.如何理解倒空间?
  4. 安装搭配VUE使用的UI框架ElementUI
  5. 前端H5面试题Js: 数组的常用方法有哪些?
  6. Java虚拟机类加载器及双亲委派机制
  7. 如何设置网件gs108e_NETGEAR 美国网件 GS108E 交换机 开箱及单线复用教程
  8. UnityHub 安装失败
  9. 宏脉系统怎么改服务器地址,宏脉系统使用手册大全.doc
  10. 关于CC2541蓝牙开发板的学习笔记