step4_extract_image_statistics.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. % Directory setting
  2. frame_directory = 'data/frames/';
  3. mask_directory = 'data/predicts/';
  4. video_names = dir( [frame_directory 'sample*'] );
  5. % Enhancing figure visibility
  6. set(gcf, 'Position', [0 0 800 800], 'Color', [1 1 1] );
  7. % Save result
  8. csv_file_directory = 'data/result_csv/';
  9. if ~isdir(csv_file_directory), mkdir(csv_file_directory); end
  10. % Figure to video
  11. output_video_option = 0;
  12. if output_video_option
  13. output_video_directory = 'data/output/';
  14. if ~isdir(output_video_directory), mkdir(output_video_directory); end
  15. end
  16. % Main loop
  17. for videoIdx = 1:length( video_names )
  18. frame_names = dir( [frame_directory video_names(videoIdx).name '/*.jpg']);
  19. mask_names = dir( [mask_directory video_names(videoIdx).name '/*.jpg']);
  20. %% For one video
  21. xy = nan([2,length( frame_names )]);
  22. bb = nan([4,length( frame_names )]);
  23. for frameIdx = 1:length( frame_names )
  24. %% Single frame loading
  25. frame_name = [ frame_names(frameIdx).folder ...
  26. '/' frame_names(frameIdx).name];
  27. mask_name = [ mask_names(frameIdx).folder ...
  28. '/' mask_names(frameIdx).name];
  29. frame = double(imread( frame_name ))/255; % Scaling to 0-1
  30. mask = double(imread( mask_name ))/255; % Scaling to 0-1
  31. % Manual artifact rejection
  32. mask(:,1:30) = 0;
  33. % Visualization
  34. % Original frame
  35. sp1 = subplot(2,2,1); hold off
  36. imagesc( frame );
  37. set(gca, 'Box', 'off', 'LineWidth', 2, 'XTick', [], 'YTick', [], 'FontSize', 15);
  38. title( sprintf( '(1) Frame #%04d', frameIdx ));
  39. colormap(sp1, 'gray');
  40. % Mask
  41. sp2 = subplot(2,2,2); hold off
  42. imagesc( mask' );
  43. set(gca, 'Box', 'off', 'LineWidth', 2, 'XTick', [], 'YTick', [], 'FontSize', 15);
  44. title( sprintf( '(2) Mask #%04d', frameIdx ));
  45. colormap(sp2, 'gray');
  46. % Overlay
  47. overlay = repmat( frame, [1 1 3] );
  48. overlay(:,:,1) = 0.5*mask';
  49. sp3 = subplot(2,2,3); hold off
  50. imagesc( overlay );
  51. set(gca, 'Box', 'off', 'LineWidth', 2, 'XTick', [], 'YTick', [], 'FontSize', 15);
  52. title( sprintf( '(3) Overlay #%04d', frameIdx ));
  53. %% Extracting image statistics using regionprops
  54. probability_threshold = 0.5; % Probability, anonymous unit (0-1)
  55. mask_binary = mask > probability_threshold;
  56. stats = regionprops( mask_binary', 'BoundingBox', 'Area', 'Centroid', 'Orientation' );
  57. size_threshold = 100; % Pixel
  58. drawing_regionprops = false;
  59. big_enough = find( [stats.Area]>size_threshold );
  60. if ~isempty( big_enough )
  61. stat = stats(big_enough);
  62. xy(:,frameIdx) = stat.Centroid;
  63. bb(:,frameIdx) = stat.BoundingBox;
  64. else % if failed
  65. xy(:,frameIdx) = xy(:,frameIdx-1);
  66. bb(:,frameIdx) = bb(:,frameIdx-1);
  67. end
  68. sp4 = subplot(2,2,4); hold off
  69. imagesc( frame );
  70. set(gca, 'Box', 'off', 'LineWidth', 2, 'XTick', [], 'YTick', [], 'FontSize', 15);
  71. title( sprintf( '(4) Tracking result #%04d', frameIdx ));
  72. colormap(sp4,'gray');
  73. hold on;
  74. % Centroid
  75. plot( xy(1,frameIdx), xy(2,frameIdx), 'g*', 'MarkerSize', 20);
  76. % BoundingBox
  77. rectangle('Position', bb(:,frameIdx),'EdgeColor','g','LineWidth',2 )
  78. % Text annotation
  79. text_contents = sprintf( 'X: [%.1f], Y: [%.1f]', xy(:,frameIdx));
  80. text( xy(1,frameIdx),xy(2,frameIdx) - 50, text_contents, 'Color', 'g',...
  81. 'HorizontalAlignment', 'center' );
  82. %% Etc.
  83. drawnow;
  84. % Writing output video
  85. if output_video_option
  86. if frameIdx==1
  87. try vid_obj.close(); clear vid_obj; end
  88. vid_obj = VideoWriter( [output_video_directory ...
  89. video_names(videoIdx).name '.mp4'], 'MPEG-4');
  90. vid_obj.open();
  91. end
  92. capture_frame = getframe(gcf);
  93. vid_obj.writeVideo(capture_frame.cdata);
  94. if frameIdx==length( frame_names )
  95. vid_obj.close();
  96. end
  97. end
  98. end
  99. % Save coordinate data into *.csv format
  100. csv_filename = [csv_file_directory 'result_' video_names(videoIdx).name '.csv'];
  101. csvwrite( csv_filename, xy );
  102. end