python 实现12bit灰度图像映射到8bit显示的方法


Posted in Python onJuly 08, 2019

图像显示和打印面临的一个问题是:图像的亮度和对比度能否充分突出关键部分。这里所指的“关键部分”在 CT 里的例子有软组织、骨头、脑组织、肺、腹部等等。

技术问题

1、显示器往往只有 8-bit, 而数据有 12- 至 16-bits。
2、如果将数据的 min 和 max 间 (dynamic range) 的之间转换到 8-bit 0-255 去,过程是个有损转换, 而且出来的图像往往突出的是些噪音。

算法分析

12-bit 到 8-bit 直接转换:

computeMinMax(pixel_val,  min,  max);  //  先算图像的最大和最小值 
for  (i  =  0;  i  <  nNumPixels;  i++) 
  disp_pixel_val[i]  =  (pixel_val[i]  -  min)*255.0/(double)(max-min);

这个算法必须有,对不少种类的图像是很有效的:如 8-bit 图像,MRI, ECT, CR 等等。

python实现

def matrix2uint8(matrix):
  ''' 
matrix must be a numpy array NXN
Returns uint8 version
  '''
  m_min= np.min(matrix)
  m_max= np.max(matrix)
  matrix = matrix-m_min
  return(np.array(np.rint( (matrix-m_min)/float(m_max-m_min) * 255.0),dtype=np.uint8))
  #np.rint, Round elements of the array to the nearest integer.
def preprocess(img, crop=True, resize=True, dsize=(224, 224)):
  if img.dtype == np.uint8:
    img = img / 255.0

  if crop:
    short_edge = min(img.shape[:2])
    yy = int((img.shape[0] - short_edge) / 2)
    xx = int((img.shape[1] - short_edge) / 2)
    crop_img = img[yy: yy + short_edge, xx: xx + short_edge]
  else:
    crop_img = img

  if resize:
    norm_img = imresize(crop_img, dsize, preserve_range=True)
  else:
    norm_img = crop_img

  return (norm_img).astype(np.float32)
def deprocess(img):
  return np.clip(img * 255, 0, 255).astype(np.uint8)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中的实例方法、静态方法、类方法、类变量和实例变量浅析
Apr 26 Python
Python编程之多态用法实例详解
May 19 Python
详解Python 协程的详细用法使用和例子
Jun 15 Python
将tensorflow的ckpt模型存储为npy的实例
Jul 09 Python
Python检测数据类型的方法总结
May 20 Python
Python PO设计模式的具体使用
Aug 16 Python
python3.7 openpyxl 删除指定一列或者一行的代码
Oct 08 Python
python sklearn常用分类算法模型的调用
Oct 16 Python
如何用OpenCV -python3实现视频物体追踪
Dec 04 Python
matlab 计算灰度图像的一阶矩,二阶矩,三阶矩实例
Apr 22 Python
使用Python-OpenCV消除图像中孤立的小区域操作
Jul 05 Python
利用Python实现学生信息管理系统的完整实例
Dec 30 Python
Python+pyplot绘制带文本标注的柱状图方法
Jul 08 #Python
python如何实现异步调用函数执行
Jul 08 #Python
django中forms组件的使用与注意
Jul 08 #Python
python celery分布式任务队列的使用详解
Jul 08 #Python
Python利用matplotlib做图中图及次坐标轴的实例
Jul 08 #Python
Python数据类型之列表和元组的方法实例详解
Jul 08 #Python
pandas对dataFrame中某一个列的数据进行处理的方法
Jul 08 #Python
You might like
phpExcel中文帮助手册之常用功能指南
2014/08/18 PHP
基于PHP实现数据分页显示功能
2016/05/26 PHP
详解cookie验证的php应用的一种SSO解决办法
2017/10/20 PHP
PHP使用mongoclient简单操作mongodb数据库示例
2019/02/08 PHP
准确获得页面、窗口高度及宽度的JS
2006/11/26 Javascript
Javascript实现滑块滑动改变值的实现代码
2013/04/12 Javascript
使用 Node.js 做 Function Test实现方法
2013/10/25 Javascript
JSP中使用JavaScript动态插入删除输入框实现代码
2014/06/13 Javascript
拥Bootstrap入怀——导航栏篇
2016/05/30 Javascript
JS button按钮实现submit按钮提交效果
2016/11/01 Javascript
基于vue-router 多级路由redirect 重定向的问题
2018/09/03 Javascript
web页面和微信小程序页面实现瀑布流效果
2018/09/26 Javascript
jquery操作select常见方法大全【7种情况】
2019/05/28 jQuery
基于Vue和Element-Ui搭建项目的方法
2019/09/06 Javascript
js实现简单的无缝轮播效果
2020/09/05 Javascript
javascript中layim之查找好友查找群组
2021/02/06 Javascript
详解Python中的type()方法的使用
2015/05/21 Python
使用Python脚本生成随机IP的简单方法
2015/07/30 Python
python 查找字符串是否存在实例详解
2017/01/20 Python
利用python如何处理百万条数据(适用java新手)
2018/06/06 Python
Python坐标线性插值应用实现
2019/11/13 Python
python 消除 futureWarning问题的解决
2019/12/25 Python
python爬虫开发之selenium模块详细使用方法与实例全解
2020/03/09 Python
深入浅析Python 命令行模块 Click
2020/03/11 Python
Python爬虫爬取博客实现可视化过程解析
2020/06/29 Python
Python matplotlib读取excel数据并用for循环画多个子图subplot操作
2020/07/14 Python
Linux面试经常问的文件系统操作命令
2016/10/04 面试题
招商经理岗位职责
2013/11/16 职场文书
十八大报告观后感
2014/01/28 职场文书
小组口号大全
2014/06/09 职场文书
婚宴致辞
2015/07/28 职场文书
mysql升级到5.7时,wordpress导数据报错1067的问题
2021/05/27 MySQL
Redis入门教程详解
2021/08/30 Redis
经典《舰娘》游改全新动画预告 预定11月开播
2022/04/01 日漫
Python实现文字pdf转换图片pdf效果
2022/04/03 Python
Win11如何默认打开软件界面最大化?Win11默认打开软件界面最大化的方法
2022/07/15 数码科技