dicomrt_sortct.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. function [change,error_handle]=dicomrt_sortct(filelist,xlocation,ylocation,zlocation,filename)
  2. % dicomrt_sortct(filelist,xlocation,ylocation,zlocation,filename)
  3. %
  4. % Sort CT data set specified in filename.
  5. %
  6. % See also dicomrt_loaddose, dicomrt_loadct, dicomrt_loadctlist
  7. %
  8. % Copyright (C) 2002 Emiliano Spezi (emiliano.spezi@physics.org)
  9. % Set parameters
  10. counter=0;
  11. error_handle=0;
  12. match=0;
  13. % sort ct slices with respect to their z location
  14. [zlocation_sorted,sorted_index]=sort(zlocation);
  15. % Progress bar
  16. h = waitbar(0,['Sorting progress:']);
  17. set(h,'Name','dicomrt_sortct: sorting CT images');
  18. diff_location=dicomrt_mmdigit(zlocation_sorted-zlocation,6);
  19. if find(diff_location) % ct slices are not sorted
  20. disp('Message: slices are not sorted!');
  21. change=1;
  22. % sort filelist accordingly
  23. filelist_sorted=' ';
  24. for i=1:length(sorted_index)
  25. counter=counter+1;
  26. filelist_sorted=char(filelist_sorted,filelist(sorted_index(i),:));
  27. xlocation_sorted(i)=xlocation(sorted_index(i));
  28. ylocation_sorted(i)=ylocation(sorted_index(i));
  29. waitbar(i/length(sorted_index),h);
  30. end
  31. xlocation_sorted=xlocation_sorted';
  32. ylocation_sorted=ylocation_sorted';
  33. filelist_sorted(1,:)=[];
  34. disp('Message: ct slices are now sorted!');
  35. % finds duplicates
  36. duplicate_index=0;
  37. [duplicate]=diff(zlocation_sorted);
  38. [duplicate_index]=find(duplicate==0);
  39. if length(duplicate_index)~=0
  40. warning('dicomrt_sortct: Images with the same zlocation were found. You can exit now or proceed to delete duplicates.');
  41. leaveoption=input('Exit now ? (Y/N) [N]','s');
  42. if leaveoption == 'Y' | leaveoption == 'y';
  43. disp('OK. Check your file list and try again. ');
  44. error_handle=1;return;
  45. else
  46. duplicates_name=' ';
  47. filelist_sorted_scrub=' ';
  48. for i=1:length(duplicate_index)
  49. duplicates_name=char(duplicates_name,filelist_sorted(duplicate_index(i),:));
  50. filelist_sorted(duplicate_index(i),1:10)='duplicate-'; % mark duplicates
  51. xlocation_sorted(duplicate_index(i))=pi;
  52. ylocation_sorted(duplicate_index(i))=pi;
  53. end
  54. for i=1:length(filelist_sorted)
  55. if isequal(filelist_sorted(i,1:10),'duplicate-')~=1
  56. filelist_sorted_scrub=char(filelist_sorted_scrub,filelist_sorted(i,:));
  57. else
  58. end
  59. end % at this point duplicates are deleted in new filelist
  60. filelist_sorted_scrub(1,:)=[];
  61. disp('The following duplicates has been deleted from file list:');
  62. duplicates_name(1,:)=[];
  63. duplicates_name
  64. %
  65. % now sorting xlocation and ylocation accordingly to what done before
  66. % (prepare for scout images identification)
  67. %
  68. for i=1:length(xlocation_sorted)
  69. if xlocation_sorted(i)~=pi
  70. xlocation_sorted_scrub=xlocation_sorted(i);
  71. end
  72. end
  73. for i=1:length(ylocation_sorted)
  74. if ylocation_sorted(i)~=pi
  75. ylocation_sorted_scrub=ylocation_sorted(i);
  76. end
  77. end
  78. end
  79. else
  80. filelist_sorted_scrub=filelist_sorted;
  81. end
  82. % Export sorted/scrubed filelist
  83. newfilename=[filename,'.sort.txt'];
  84. newfile=fopen(newfilename,'w');
  85. for i=1:size(filelist_sorted_scrub,1)
  86. fprintf(newfile,'%s',filelist_sorted_scrub(i,:)); fprintf(newfile,'\n');
  87. end
  88. fclose(newfile);
  89. disp(['A new file list has been written by dicomrt_sortct with name: ',newfilename]);
  90. disp('This file will be used to import ct data instead');
  91. else % ct slices are already sorted
  92. change=0;
  93. end
  94. % Close progress bar
  95. close(h);