/support_programs/basic_image_processing/export_contour.m

http://dirart.googlecode.com/ · Objective C · 80 lines · 69 code · 11 blank · 0 comment · 8 complexity · f41973d0b66cfd7ab1c6ade2f3e3bcf7 MD5 · raw file

  1. function export_contour(filename,ctdim,ctinfo,zsinput)
  2. %
  3. % export_contour(filename,ctdim,ctinfo,zsinput)
  4. %
  5. % ctdim: Dimension of CT images
  6. % ctinfo: Dicom Info of CT images
  7. % zsinput: CT slice z values
  8. %
  9. %{
  10. Copyrighted by:
  11. Deshan Yang, dyang@radonc.wustl.edu
  12. 10/10/2007
  13. Department of radiation oncology
  14. Washington University in Saint Louis
  15. %}
  16. disp('Loading dicominfo');
  17. info = dicominfo(filename);
  18. ps = ctinfo.PixelSpacing;
  19. pp = ctinfo.ImagePositionPatient;
  20. ROI = 1;
  21. surfaces = zeros(ctdim,'uint8');
  22. while 1 % For each structure
  23. roi_item = sprintf('Item_%d',ROI);
  24. if isfield(info.StructureSetROISequence,roi_item)
  25. ROIinfo = getfield(info.StructureSetROISequence,roi_item);
  26. ROIName = ROIinfo.ROIName;
  27. disp(sprintf('Processing contours for %s ...',ROIName));
  28. ROIContour = getfield(info.ROIContourSequence,roi_item);
  29. color = ROIContour.ROIDisplayColor;
  30. ContourSequence = ROIContour.ContourSequence;
  31. c = 0;
  32. mask3d = zeros(ctdim,'uint8');
  33. zs = [];
  34. while 1 % For each contour
  35. c_item = sprintf('Item_%d',c+1);
  36. if isfield(ContourSequence,c_item)
  37. % disp(sprintf('For %s : contour %d',ROIName,c+1));
  38. item = getfield(ContourSequence,c_item);
  39. xyz = reshape(item.ContourData,3,item.NumberOfContourPoints);
  40. x = round((xyz(2,:) - pp(2)) / ps(2) + 0.5); % Converting to pixel unit
  41. y = round((xyz(1,:) - pp(1)) / ps(1) + 0.5);
  42. x = max(x,1); x = min(x,ctdim(2));
  43. y = max(y,1); y = min(y,ctdim(1));
  44. z = xyz(3,:);
  45. idx = find( abs(zsinput - z(1)) < 1);
  46. if isempty(idx)
  47. warning(sprintf('z = %d not found',z(1)));
  48. else
  49. mask3d(:,:,idx) = mask3d(:,:,idx) + uint8(poly2mask(y,x,ctdim(1),ctdim(2)));
  50. for k=1:length(y)
  51. surfaces(x(k),y(k),idx) = ROI;
  52. end
  53. end
  54. else
  55. disp(sprintf('Totally %d 2D contours found for %s',c,ROIName));
  56. %mask3d = flipdim(mask3d,3);
  57. save(sprintf('%s_mask3d.mat',ROIName),'mask3d');
  58. break;
  59. end
  60. c = c+1;
  61. end
  62. else
  63. disp('All finished');
  64. break;
  65. end
  66. ROI = ROI+1;
  67. end
  68. %surfaces = flipdim(surfaces,3);
  69. save surfaces.mat surfaces;