1 import matplotlib.pyplot as plt 2 import numpy as np 3 import os 4 import pydicom 5 import cv2 6 7 info = {} 8 # 讀取dicom文件,乳腺癌MRI圖片 9 dcm = pydicom.read_file("D:/1/dicom/datuidwi.dcm") 10 # 通過字典關鍵字來獲取圖像的數據元信息(當然也可以根據TAG號) 11 # 這里獲取幾種常用信息 12 info["PatientID"] = dcm.PatientID # 患者ID 13 info["PatientName"] = dcm.PatientName # 患者姓名 14 #info["PatientBirthData"] = dcm.PatientBirthData # 患者出生日期 15 info["PatientAge"] = dcm.PatientAge # 患者年齡 16 info['PatientSex'] = dcm.PatientSex # 患者性別 17 info['StudyID'] = dcm.StudyID # 檢查ID 18 info['StudyDate'] = dcm.StudyDate # 檢查日期 19 info['StudyTime'] = dcm.StudyTime # 檢查時間 20 info['InstitutionName'] = dcm.InstitutionName # 機構名稱 21 info['Manufacturer'] = dcm.Manufacturer # 設備制造商 22 info['StudyDescription']=dcm.StudyDescription # 檢查項目描述 23 print(info) 24 25 filename = "D:/1/dicom/datuidwi.dcm" 26 jpgname = "D:/1/dicom/test4.jpg" 27 # 讀取dicom文件 28 dcm = pydicom.read_file(filename) 29 # 獲取圖像唯一標識符UID 30 uid = dcm.SOPInstanceUID 31 # 獲取像素矩陣 32 img_arr = dcm.pixel_array 33 # 打印矩陣大小 34 #print(img_arr.shape) 35 # 獲取像素點個數 36 lens = img_arr.shape[0]*img_arr.shape[1] 37 # 獲取像素點的最大值和最小值 38 arr_temp = np.reshape(img_arr,(lens,)) 39 max_val = max(arr_temp) 40 min_val = min(arr_temp) 41 # 圖像歸一化 42 img_arr = (img_arr-min_val)/(max_val-min_val) 43 # 繪制圖像並保存 44 #保存圖片時去掉周圍白邊 45 plt.axis('off') 46 fig = plt.gcf() 47 fig.set_size_inches(7.0/3,7.0/3) #dpi = 300, output = 700*700 pixels 48 plt.gca().xaxis.set_major_locator(plt.NullLocator()) 49 plt.gca().yaxis.set_major_locator(plt.NullLocator()) 50 plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) 51 plt.margins(0,0) 52 plt.imshow(img_arr,cmap=plt.cm.bone) 53 fig.savefig(jpgname, format='jpg', transparent=True, dpi=300, pad_inches = 0) 54 55 #獲取圖像灰度直方圖查看灰度分布 56 img=cv2.imread('D:/1/dicom/test4.jpg') 57 plt.hist(img.ravel(),256,[0,256])#ravel函數功能是將多維數組降為一維數組 58 plt.show() 59 60 img = cv2.imread('D:/1/dicom/test4.jpg', 0) 61 jpgname = 'D:/1/dicom/test58.jpg' 62 # 固定閾值 63 ret, th1 = cv2.threshold(img, 165, 255, cv2.THRESH_BINARY) 64 images = [img, th1] 65 #保存粗分割結果 66 plt.axis('off') 67 fig = plt.gcf() 68 fig.set_size_inches(7.0/3,7.0/3) #dpi = 300, output = 700*700 pixels 69 plt.gca().xaxis.set_major_locator(plt.NullLocator()) 70 plt.gca().yaxis.set_major_locator(plt.NullLocator()) 71 plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) 72 plt.margins(0,0) 73 plt.imshow(images[1], 'gray') 74 fig.savefig(jpgname, format='jpg', transparent=True, dpi=300, pad_inches = 0) 75 #對粗分割結果中非病灶區域填充 76 #截取圖片中的指定區域或在指定區域添加某一圖片 77 def jie_image(src1): 78 src2 = src1[5:125, 280:600]#截取第5行到125行的第280列到600列的區域 79 #cv.imshow("截取", src2) 80 src1[360:480, 280:600] = src2#指定位置填充,大小要一樣才能填充 81 cv2.imshow("合成", src1) 82 src = cv2.imread("D:/1/dicom/test58.jpg", 0) 83 #cv.imshow("原來", src) 84 jie_image(src) 85 cv2.waitKey(0) 86 cv2.destroyAllWindows() 87 cv2.imwrite('D:/1/dicom/test68.jpg', src) 88 89 #填充后得到第二次的粗分割結果,病灶區域存在孔洞,使用孔洞填充方法進行填充 90 ''' 91 圖像說明: 92 圖像為二值化圖像,255白色為目標物,0黑色為背景 93 要填充白色目標物中的黑色空洞 94 ''' 95 imgPath = 'D:/1/dicom/test68.jpg' 96 SavePath = 'D:/1/dicom/test78.jpg' 97 def FillHole(imgPath,SavePath): 98 im_in = cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE); 99 # 復制 im_in 圖像 100 im_floodfill = im_in.copy() 101 # Mask 用於 floodFill,官方要求長寬+2 102 h, w = im_in.shape[:2] 103 mask = np.zeros((h+2, w+2), np.uint8) 104 # floodFill函數中的seedPoint必須是背景 105 isbreak = False 106 for i in range(im_floodfill.shape[0]): 107 for j in range(im_floodfill.shape[1]): 108 if(im_floodfill[i][j]==0): 109 seedPoint=(i,j) 110 isbreak = True 111 break 112 if(isbreak): 113 break 114 # 得到im_floodfill 115 cv2.floodFill(im_floodfill, mask, seedPoint, 255); 116 # 得到im_floodfill的逆im_floodfill_inv 117 im_floodfill_inv = cv2.bitwise_not(im_floodfill) 118 # 把im_in、im_floodfill_inv這兩幅圖像結合起來得到前景 119 im_out = im_in | im_floodfill_inv 120 # 保存結果 121 cv2.imwrite(SavePath, im_out) 122 FillHole(imgPath,SavePath
#以上這種填充結果會把輪廓外圍也填充,分割不准確,因此使用了MATLAB中的imfill函數對分割后病灶區域的結果進行孔洞填充,得到的分割結果如下圖所示
clear all; clc; close all;
img = imread('D:\matlab\bin\test68.jpg');
if ndims(img)==3
img = rgb2gray(img);
end
img_bw = im2bw(img);
img_fill = imfill(img_bw, 'holes');
imwrite(img_fill,'D:\matlab\bin\temp34.jpg');
%figure;
%subplot(1,2,1),imshow(img_bw), title('有空洞的圖像');
%subplot(1,2,2),imshow(img_fill), title('孔洞被填充的圖像');
由於一些原因,不能放原圖,分割結果如下所示
粗分割結果
第一次填充后結果
孔洞填充得到最終分割結果
參考文章:
1.https://www.zhihu.com/tardis/sogou/art/154181400
2.https://zhuanlan.zhihu.com/p/63919290
3.https://www.cnblogs.com/FHC1994/p/9033580.html
4.https://www.jianshu.com/p/293e04f134c3
作者:舟華520
出處:https://www.cnblogs.com/xfzh193/
本文以學習,分享,研究交流為主,歡迎轉載,請標明作者出處!