step2_gen_trainset.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. %% (2) Randomly-select partial dataset
  2. % Getting full frame list
  3. frame_directory = 'data/frames/';
  4. video_names = dir( [frame_directory 'sample*' ]);
  5. full_frame_names = {};
  6. counter = 1;
  7. for videoIdx = 1:length( video_names )
  8. frame_names = dir( [frame_directory video_names(videoIdx).name '/*.jpg']);
  9. for frameIdx = 1:length( frame_names )
  10. full_frame_names{ counter, 1 } = [...
  11. frame_names(frameIdx).folder...
  12. '/' frame_names(frameIdx).name];
  13. counter=counter+1;
  14. end
  15. end
  16. % Random select & copy
  17. trainset_directory = 'data/trainset/';
  18. if ~isdir(trainset_directory), mkdir(trainset_directory); end
  19. n_all_image = length(full_frame_names);
  20. n_train_image = 101;
  21. rng(abs(010-6207-8179)) % Fix random seed
  22. target_frame_number = randi( n_all_image, [1 n_train_image] );
  23. for frameIdx = 1:n_train_image
  24. copyfile( full_frame_names{target_frame_number(frameIdx)}, trainset_directory );
  25. end
  26. %% (3) Manual segmentation
  27. mask_directory = trainset_directory;
  28. img_list = dir( [trainset_directory 'frame*.jpg'] );
  29. close all;
  30. for frameIdx = 1:length(img_list)
  31. img_fname = img_list(frameIdx).name;
  32. mask_fname = ['mask' img_list(frameIdx).name(6:end-4) '.jpg'];
  33. frame = imread( [trainset_directory img_fname] );
  34. % Visualization
  35. % open frame; figure(1); clf; imagesc( frame ); drawnow; colormap gray;
  36. % Check existence
  37. if exist([trainset_directory mask_fname])
  38. disp(['Already exist::: ' trainset_directory mask_fname]);
  39. else
  40. figure(2); clf;
  41. gcf_pos = [50 50 1500 600];
  42. set(gcf, 'Position', gcf_pos, 'Color', [1 1 1]);
  43. % Segmentation
  44. subplot(1,2,1);
  45. try % if exist
  46. mask = image_ROI_selection(frame, [ 'Select area' ...
  47. '.. imgIdx=' num2str(sprintf('%04d',frameIdx))]);
  48. catch % in case of no mouse
  49. mask = zeros(size(frame));
  50. end
  51. xlabel(img_fname)
  52. set(gca, 'FontSize', 15, 'Box', 'off', 'LineWidth', 2);
  53. % Show result
  54. subplot(1,2,2);
  55. imagesc( mask ); colormap gray;
  56. title('Result mask (binary)');
  57. drawnow;
  58. imwrite( mask, [mask_directory mask_fname]);
  59. end
  60. end
  61. %open mask
  62. zip( 'data/trainset.zip', 'data/trainset/')
  63. zip( 'data/frames.zip','data/frames/' )
  64. %% Subfunctions
  65. %% Subfunction
  66. function mask = image_ROI_selection(I, textInput)
  67. if nargin == 1, textInput = ''; end
  68. colormap(gray);
  69. imshow(uint8(I), 'DisplayRange', [], 'InitialMagnification',600/max(size(I))*100 );
  70. title( textInput )
  71. %%-- Display instructions to user
  72. %title('click points to make an initial contour, right click to close contour and finish');
  73. disp('click points to make an initial contour,');
  74. disp('right click to close contour and finish');
  75. %%-- begin getting points
  76. [y1 x1 b] = ginput(1);
  77. xi = x1;
  78. yi = y1;
  79. if(b ~= 1) return; end
  80. [ny nx c] = size(I);
  81. mask = zeros(ny,nx);
  82. while(1)
  83. x2 = x1;
  84. y2 = y1;
  85. [y1 x1 b] = ginput(1);
  86. if(b ~= 1)
  87. x1 = xi;
  88. y1 = yi;
  89. end
  90. %%--figure out the length of x & y component
  91. lx = x2-x1;
  92. ly = y2-y1;
  93. %%--figure out line length = pythagorian length + fudge factor
  94. len = ceil((lx^2+ly^2)^(1/2))+1;
  95. %%--make a linearly spaced vector (some values repeated)
  96. x = round(x1:(lx)/(len-1):x2);
  97. y = round(y1:(ly)/(len-1):y2); %make another one for y
  98. %%--if it was a constant level the lines above mess up make a constant line
  99. if(length(x) == 0)
  100. x = round(x1) * ones(1,len);
  101. end
  102. if(length(y) == 0)
  103. y = round(y1) * ones(1,len);
  104. end
  105. this_index2 = sub2ind(size(mask),x, y);
  106. if size(this_index2) == [1 1]
  107. title('Try Again!', 'FontSize', 18)
  108. sprintf Try_Again!
  109. end
  110. try mask(this_index2) = 1; end
  111. idx = find(mask==1);
  112. backup_mask = mask;
  113. %%-- draw the users line in the image (color or grayscale)
  114. if(c-1)
  115. Ir = I(:,:,1); Ig = I(:,:,2); Ib = I(:,:,3);
  116. Ir(idx) = 0;
  117. Ig(idx) = 255;
  118. Ib(idx) = 0;
  119. I(:,:,1) = Ir; I(:,:,2) = Ig; I(:,:,3) = Ib;
  120. else
  121. I(idx) = 255;
  122. end
  123. imshow(uint8(I),'DisplayRange', [], 'InitialMagnification', 600/max(size(I))*100);
  124. title( textInput )
  125. if(b ~= 1) break; end
  126. end
  127. mask = bwfill(mask, 'holes');
  128. % mask = ~mask;
  129. end