圖像顯示和打印面臨的一個問題是:圖像的亮度和對比度能否充分突出關(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

