12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import os
- import numpy as np
- import matplotlib.pyplot as plt
- import pydicom
- import cv2
- from skimage.filters import gaussian
- from skimage.segmentation import active_contour
- from skimage.measure import find_contours
- def get_mask_contour(mask):
- mskray = mask.astype(np.uint8)
- edged_mask = cv2.Canny(mskray, np.min(mskray), np.max(mskray))
- return edged_mask
- folder_pth = r'C:\Users\user\Desktop\knee_seg\LITVYAK_D.I\LITVYAK_D.I\2025-01-22 181006'
- # folder_pth = r'E:\projects\knee_seg\Юшкевич_Оба колена\Yushkevich_L\YUSHKEVICH_A.V. 172\2025-01-31 090519'
- # folder_pth = r'E:\projects\knee_seg\LITVYAK_D.I\2025-01-22 181006'
- # folder_pth = r'E:\projects\knee_seg\data_tamplet\PA0\ST0\SE4'
- mx = []
- list_files = os.listdir(folder_pth)
- pd_fils = []
- for i, fname in enumerate(list_files):
- file_pth = os.path.join(folder_pth, list_files[i])
- file_dcm = pydicom.dcmread(file_pth)
- print(file_dcm['0008', '103e'][0:9])
- if 'Sag PD' in file_dcm['0008', '103e'][0:9]:
- pd_fils.append(file_pth)
- for f in range (2, len(pd_fils) - 2):
- file_dcm = pydicom.dcmread(pd_fils[f])
- im = file_dcm.pixel_array
- counts, bins = np.histogram(im, 100)
- threshold = 0.50 * (np.max(im) - 0.05 * np.max(im))
- segmented_im = np.copy(im)
- segmented_im[segmented_im < threshold] = 0
- contours = find_contours(segmented_im, level=0.5)
- plt.subplot(1, 3, 1)
- plt.imshow(im, cmap='gray')
- plt.title ('Исходное изображение')
- plt.subplot(1, 3, 2)
- plt.imshow(im, 'gray')
- plt.title ('Фильтрация по яркости')
- contours_square = []
- for c in range(len(contours)):
- contours_square.append((contours[c].shape[0]*contours[c].shape[1]))
- contours_thr = 0.3 * np.max (contours_square)
- # plot all
- for cn in range(len(contours)):
- plt.plot(contours[cn][:, 1], contours[cn][:, 0], 'r')
- plt.subplot(1, 3, 3)
- plt.imshow(im, 'gray')
- plt.title ('Фильтрация по размеру объекта')
- for cn in range(len(contours)):
- if contours[cn].shape[0]*contours[cn].shape[1]> contours_thr:
- # contours[cn] = 0
- plt.plot(contours[cn][:, 1], contours[cn][:, 0], 'r')
- plt.show()
|