read_data.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import os
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import pydicom
  5. import cv2
  6. from skimage.filters import gaussian
  7. from skimage.segmentation import active_contour
  8. from skimage.measure import find_contours
  9. def get_mask_contour(mask):
  10. mskray = mask.astype(np.uint8)
  11. edged_mask = cv2.Canny(mskray, np.min(mskray), np.max(mskray))
  12. return edged_mask
  13. folder_pth = r'C:\Users\user\Desktop\knee_seg\LITVYAK_D.I\LITVYAK_D.I\2025-01-22 181006'
  14. # folder_pth = r'E:\projects\knee_seg\Юшкевич_Оба колена\Yushkevich_L\YUSHKEVICH_A.V. 172\2025-01-31 090519'
  15. # folder_pth = r'E:\projects\knee_seg\LITVYAK_D.I\2025-01-22 181006'
  16. # folder_pth = r'E:\projects\knee_seg\data_tamplet\PA0\ST0\SE4'
  17. mx = []
  18. list_files = os.listdir(folder_pth)
  19. pd_fils = []
  20. for i, fname in enumerate(list_files):
  21. file_pth = os.path.join(folder_pth, list_files[i])
  22. file_dcm = pydicom.dcmread(file_pth)
  23. print(file_dcm['0008', '103e'][0:9])
  24. if 'Sag PD' in file_dcm['0008', '103e'][0:9]:
  25. pd_fils.append(file_pth)
  26. for f in range (2, len(pd_fils) - 2):
  27. file_dcm = pydicom.dcmread(pd_fils[f])
  28. im = file_dcm.pixel_array
  29. counts, bins = np.histogram(im, 100)
  30. threshold = 0.50 * (np.max(im) - 0.05 * np.max(im))
  31. segmented_im = np.copy(im)
  32. segmented_im[segmented_im < threshold] = 0
  33. contours = find_contours(segmented_im, level=0.5)
  34. plt.subplot(1, 3, 1)
  35. plt.imshow(im, cmap='gray')
  36. plt.title ('Исходное изображение')
  37. plt.subplot(1, 3, 2)
  38. plt.imshow(im, 'gray')
  39. plt.title ('Фильтрация по яркости')
  40. contours_square = []
  41. for c in range(len(contours)):
  42. contours_square.append((contours[c].shape[0]*contours[c].shape[1]))
  43. contours_thr = 0.3 * np.max (contours_square)
  44. # plot all
  45. for cn in range(len(contours)):
  46. plt.plot(contours[cn][:, 1], contours[cn][:, 0], 'r')
  47. plt.subplot(1, 3, 3)
  48. plt.imshow(im, 'gray')
  49. plt.title ('Фильтрация по размеру объекта')
  50. for cn in range(len(contours)):
  51. if contours[cn].shape[0]*contours[cn].shape[1]> contours_thr:
  52. # contours[cn] = 0
  53. plt.plot(contours[cn][:, 1], contours[cn][:, 0], 'r')
  54. plt.show()