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

yipeiwu_com6年前Python基础

图像显示和打印面临的一个问题是:图像的亮度和对比度能否充分突出关键部分。这里所指的“关键部分”在 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中的itertools的使用详解

今天了解了下python中内置模块itertools的使用,熟悉下,看能不能以后少写几个for,嘿嘿😁。 1.无穷的迭代器 1.1 count(start,[step...

python 禁止函数修改列表的实现方法

有时候,需要禁止函数修改列表。例如要对裂变进行修改操作,也要保留原来的未打印的设计列表,以供备案。为解决这个问题,可向函数传递列表的副本而不是原件;这样函数所做的任何修改都只影响副本,而...

python处理“&#”开头加数字的html字符方法

python处理“&#”开头加数字的html字符方法

python如何处理“&#”开头加数字的html字符,比如:风水这类数据。 用python抓取数据时,有时会遇到想要数据是以“&#”开头加数字的字符,比如图中...

pytorch 实现在预训练模型的 input上增减通道

如何把imagenet预训练的模型,输入层的通道数随心所欲的修改,从而来适应自己的任务 #增加一个通道 w = layers[0].weight layers[0] = nn.Con...

python中的turtle库函数简单使用教程

python中的turtle库函数简单使用教程

具体内容如下所示: 参考案例: import turtle d=0 for i in range(4): turtle.fd(200) #或者写成turtle.fo...