step4_extract_image_statistics.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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
  103. %% Sample visualization
  104. csv_files = dir( [ csv_file_directory '*.csv'] );
  105. for fileIdx = 1:length(csv_files)
  106. %% 1. Trace
  107. % Get coordinate
  108. xy = csvread( [csv_file_directory csv_files( fileIdx ).name] )/448;
  109. % Calc speed
  110. speed = [0; smooth( sum(abs(diff(xy,2)),1), 5 )];
  111. speed = speed/max(speed);
  112. outliers = find( speed > (nanmean(speed)+nanstd(speed)*3) );
  113. xy(outliers) = nan;
  114. % Plot
  115. subplot( 2,4, fileIdx ); hold off
  116. plt = plot( xy(1,:), xy(2,:), 'k.');
  117. % Enhance visibility
  118. set(gca, 'XDir', 'normal', 'YDir', 'reverse');
  119. set(gca, 'XTick', [], 'YTick', [], 'LineWidth', 2, 'Box', 'on', 'FontSize', 15 );
  120. axis([0 1 0 1]);
  121. title( sprintf( 'Trace (File %1d)', fileIdx ) );
  122. %% 2. Histogram
  123. % Get coordinate
  124. edges = linspace( 0, 1, 100 );
  125. [n,xedge,yedge] = hb_histcount2( xy(1,:), xy(2,:), edges, edges );
  126. try,n=imgaussfilt(n,1);end
  127. % Plot
  128. subplot( 2,4, 4+fileIdx ); hold off
  129. imagesc( xedge, yedge, log(n)' );
  130. % Enhance visibility
  131. set(gca, 'XDir', 'normal', 'YDir', 'reverse');
  132. set(gca, 'XTick', [], 'YTick', [], 'LineWidth', 2, 'Box', 'on', 'FontSize', 15 );
  133. title( sprintf( 'Occupancy (File %1d)', fileIdx ) );
  134. axis([0 1 0 1]);
  135. colormap hot;
  136. end
  137. % Subfunction
  138. function [n,xedge,yedge,idx] = hb_histcount2(X, Y, xedge, yedge)
  139. nbin = [length(xedge)-1, length(yedge)-1];
  140. for binIdx_x = 1:nbin(1)
  141. xIdx = (X>xedge(binIdx_x)) .* (X<=xedge(binIdx_x+1));
  142. for binIdx_y = 1:nbin(2)
  143. yIdx = (Y>xedge(binIdx_y)) .* (Y<=xedge(binIdx_y+1));
  144. jointIdx = find( xIdx .* yIdx );
  145. n( binIdx_x, binIdx_y ) = length(jointIdx);
  146. idx{binIdx_x, binIdx_y} = jointIdx;
  147. end
  148. end
  149. end