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人工智能tensorflow函數tf.get_variable使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數tf.get_variable使用方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
Python通過paramiko遠程下載Linux服務器上的文件實例
今天小編就為大家分享一篇Python通過paramiko遠程下載Linux服務器上的文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

