这是我的示例代码。我尝试将自适应阈值应用于分割骨骼。加载存储为 mat 文件的 Dicom 文件数据并提取 ct 图像。然后取一片 NumPy.ndarray 类型的 CT 数据,然后将其转换为 CV_8UC1 类型以应用阈值。
param = yaml.load(open("parameters.yaml"))
filenames = os.listdir(param['path_to_data'])
targetfolder = []
for i in range(0,len(filenames)):
data = read_mat(os.path.join(param['path_to_data'],filenames[i]))
ct_image = data['ct_image']
mid_slice = np.round(np.array(data['ct_image'].shape)/2).astype(int)
ct_slice = ct_image[:,:,mid_slice[2]]
u8 = ct_slice.astype(np.uint8)
cv2.imshow('im',u8)
thresh1 = cv2.adaptiveThreshold(u8, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 701, 2)
thresh2 = cv2.adaptiveThreshold(u8,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
titles = ['Original Image', 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [u8, thresh1,thresh2,thresh3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()