python 實現(xiàn)12bit灰度圖像映射到8bit顯示的方法
圖像顯示和打印面臨的一個問題是:圖像的亮度和對比度能否充分突出關(guān)鍵部分。這里所指的“關(guān)鍵部分”在 CT 里的例子有軟組織、骨頭、腦組織、肺、腹部等等。
技術(shù)問題
1、顯示器往往只有 8-bit, 而數(shù)據(jù)有 12- 至 16-bits。
2、如果將數(shù)據(jù)的 min 和 max 間 (dynamic range) 的之間轉(zhuǎn)換到 8-bit 0-255 去,過程是個有損轉(zhuǎn)換, 而且出來的圖像往往突出的是些噪音。
算法分析
12-bit 到 8-bit 直接轉(zhuǎn)換:
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實現(xiàn)
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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python調(diào)用Java可執(zhí)行jar包問題
這篇文章主要介紹了Python調(diào)用Java可執(zhí)行jar包問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
python可視化plotly?圖例(legend)設(shè)置
這篇文章主要介紹了python可視化plotly?圖例(legend)設(shè)置,主要介紹了關(guān)于python?的legend圖例,參數(shù)使用說明,具有很好的參考價值,希望對大家有所幫助,需要的朋友可以參考下賣你具體內(nèi)容2022-02-02
Python使用sqlite3第三方庫讀寫SQLite數(shù)據(jù)庫的方法步驟
數(shù)據(jù)庫非常重要,程序的數(shù)據(jù)增刪改查需要數(shù)據(jù)庫支持,python處理數(shù)據(jù)庫非常簡單,而且不同類型的數(shù)據(jù)庫處理邏輯方式大同小異,下面這篇文章主要給大家介紹了關(guān)于Python使用sqlite3第三方庫讀寫SQLite數(shù)據(jù)庫的方法步驟,需要的朋友可以參考下2022-07-07
Python按照某列內(nèi)容對兩個DataFrame進行合并操作方法
這篇文章主要給大家介紹了關(guān)于Python按照某列內(nèi)容對兩個DataFrame進行合并操作的相關(guān)資料,文中通過代碼示例介紹的非常詳細,對大家學(xué)習(xí)或者使用Python具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08

