step4_extract_image_statistics.m 4.1 KB

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