python查看bit_python 实现12bit灰度图像映射到8bit显示的方法
圖像顯示和打印面臨的一個問題是:圖像的亮度和對比度能否充分突出關鍵部分。這里所指的“關鍵部分”在 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 實現12bit灰度圖像映射到8bit顯示的方法
本文地址: http://www.cppcns.com/jiaoben/python/264879.html
總結
以上是生活随笔為你收集整理的python查看bit_python 实现12bit灰度图像映射到8bit显示的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Office2016 Excel 快捷键
- 下一篇: 2019.11.4 英语学习