step1_parse_video.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. parse_video();
  2. %%
  3. function parse_video(resize_shape, crop_flag)
  4. %
  5. % Parsing video (*.mp4/*.avi) file(s) into image (*.jpg) files
  6. %
  7. % Written by Hio-Been Han, hiobeen.han@kaist.ac.kr
  8. %
  9. % 2020-09-30
  10. %
  11. if nargin < 1, resize_shape = [448 448]; end
  12. if nargin < 2, crop_flag = false; end
  13. %% (1) Get file info & set save directory
  14. [file_list, vid_directory] = uigetfile('*.mp4','MultiSelect','on');
  15. if ischar(file_list), file_list = {file_list}; end
  16. img_directory = [ 'data/frames/'];
  17. if ~isdir(img_directory), mkdir(img_directory); end
  18. convert_resolution = sprintf( '%dx%d', resize_shape(1),resize_shape(2));
  19. save_format = '.jpg';
  20. %% (2) Parse video into JPEG files
  21. for fileIdx = 1:length(file_list)
  22. % Set save directory
  23. fname = file_list{fileIdx};
  24. uniqname = fname(1:end-4);
  25. fileformat = fname(end-3:end);
  26. vidname = [vid_directory uniqname];
  27. save_directory = [img_directory uniqname '/'];
  28. if ~isdir(save_directory), mkdir(save_directory); end
  29. % Read frames
  30. v = VideoReader( [ vidname fileformat ]);
  31. nFrames = v.Duration * v.FrameRate;
  32. for frameIdx = 1:nFrames
  33. if v.hasFrame
  34. frame = v.readFrame();
  35. % Set ROI
  36. if frameIdx == 1
  37. if crop_flag
  38. mask = image_ROI_selection(frame, ['Select ROI to save' ...
  39. ' [Occlusion => Right Click]']);
  40. else
  41. mask = logical(ones([size(frame,1), size(frame,2)]));
  42. end
  43. idx_x = find( nanmean(mask,2) );
  44. idx_y = find( nanmean(mask,1) );
  45. original_resolution = sprintf( '%dx%d', size(frame,1), size(frame,2));
  46. end
  47. % Save frame
  48. % frame_valid = rgb2gray( frame(idx_x,idx_y,:) );
  49. frame_valid = rgb2gray( frame(:,60:end-60,:) );
  50. imgname_write = [num2str(sprintf('frame-%06d',frameIdx))...
  51. '-' uniqname '-from' original_resolution '-to' convert_resolution save_format];
  52. imwrite( imresize( frame_valid, resize_shape ), [ save_directory imgname_write ] )
  53. end
  54. end
  55. end
  56. end
  57. %% Subfunc
  58. function mask = image_ROI_selection(I, textInput)
  59. if nargin == 1, textInput = ''; end
  60. figure(1), set(gcf, 'Color', [1 1 1]);
  61. colormap(gray);
  62. imshow(uint8(I), 'DisplayRange', [], 'InitialMagnification',600/max(size(I))*100 );
  63. title( textInput )
  64. %%-- Display instructions to user
  65. %title('click points to make an initial contour, right click to close contour and finish');
  66. disp('click points to make an initial contour,');
  67. disp('right click to close contour and finish');
  68. %%-- begin getting points
  69. [y1 x1 b] = ginput(1);
  70. xi = x1;
  71. yi = y1;
  72. if(b ~= 1) return; end
  73. [ny nx c] = size(I);
  74. mask = zeros(ny,nx);
  75. while(1)
  76. x2 = x1; y2 = y1;
  77. [y1 x1 b] = ginput(1);
  78. if(b ~= 1), x1 = xi; y1 = yi; end
  79. lx = x2-x1; ly = y2-y1;
  80. len = ceil((lx^2+ly^2)^(1/2))+1;
  81. x = round(x1:(lx)/(len-1):x2); y = round(y1:(ly)/(len-1):y2); %make another one for y
  82. if(length(x) == 0), x = round(x1) * ones(1,len); end
  83. if(length(y) == 0)
  84. y = round(y1) * ones(1,len);
  85. end
  86. this_index2 = sub2ind(size(mask),x, y);
  87. if size(this_index2) == [1 1]
  88. title('Try Again!', 'FontSize', 18)
  89. sprintf Try_Again!
  90. end
  91. try mask(this_index2) = 1; end
  92. idx = find(mask==1);
  93. backup_mask = mask;
  94. %%-- draw the users line in the image (color or grayscale)
  95. if(c-1)
  96. Ir = I(:,:,1); Ig = I(:,:,2); Ib = I(:,:,3);
  97. Ir(idx) = 0;
  98. Ig(idx) = 255;
  99. Ib(idx) = 0;
  100. I(:,:,1) = Ir; I(:,:,2) = Ig; I(:,:,3) = Ib;
  101. else
  102. I(idx) = 255;
  103. end
  104. imshow(uint8(I),'DisplayRange', [], 'InitialMagnification', 600/max(size(I))*100);
  105. title( textInput )
  106. if(b ~= 1) break; end
  107. end
  108. mask = bwfill(mask, 'holes');
  109. % mask = ~mask;
  110. end