step2_gen_trainset.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. %% (1) Select partial dataset randomly
  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 = 100;
  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. %% (2) 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. %% Subfunction
  65. function mask = image_ROI_selection(I, textInput)
  66. if nargin == 1, textInput = ''; end
  67. colormap(gray);
  68. imshow(uint8(I), 'DisplayRange', [], 'InitialMagnification',600/max(size(I))*100 );
  69. title( textInput )
  70. %%-- Display instructions to user
  71. %title('click points to make an initial contour, right click to close contour and finish');
  72. disp('click points to make an initial contour,');
  73. disp('right click to close contour and finish');
  74. %%-- begin getting points
  75. [y1 x1 b] = ginput(1);
  76. xi = x1;
  77. yi = y1;
  78. if(b ~= 1) return; end
  79. [ny nx c] = size(I);
  80. mask = zeros(ny,nx);
  81. while(1)
  82. x2 = x1;
  83. y2 = y1;
  84. [y1 x1 b] = ginput(1);
  85. if(b ~= 1)
  86. x1 = xi;
  87. y1 = yi;
  88. end
  89. %%--figure out the length of x & y component
  90. lx = x2-x1;
  91. ly = y2-y1;
  92. %%--figure out line length = pythagorian length + fudge factor
  93. len = ceil((lx^2+ly^2)^(1/2))+1;
  94. %%--make a linearly spaced vector (some values repeated)
  95. x = round(x1:(lx)/(len-1):x2);
  96. y = round(y1:(ly)/(len-1):y2); %make another one for y
  97. %%--if it was a constant level the lines above mess up make a constant line
  98. if(length(x) == 0)
  99. x = round(x1) * ones(1,len);
  100. end
  101. if(length(y) == 0)
  102. y = round(y1) * ones(1,len);
  103. end
  104. this_index2 = sub2ind(size(mask),x, y);
  105. if size(this_index2) == [1 1]
  106. title('Try Again!', 'FontSize', 18)
  107. sprintf Try_Again!
  108. end
  109. try mask(this_index2) = 1; end
  110. idx = find(mask==1);
  111. backup_mask = mask;
  112. %%-- draw the users line in the image (color or grayscale)
  113. if(c-1)
  114. Ir = I(:,:,1); Ig = I(:,:,2); Ib = I(:,:,3);
  115. Ir(idx) = 0;
  116. Ig(idx) = 255;
  117. Ib(idx) = 0;
  118. I(:,:,1) = Ir; I(:,:,2) = Ig; I(:,:,3) = Ib;
  119. else
  120. I(idx) = 255;
  121. end
  122. imshow(uint8(I),'DisplayRange', [], 'InitialMagnification', 600/max(size(I))*100);
  123. title( textInput )
  124. if(b ~= 1) break; end
  125. end
  126. mask = bwfill(mask, 'holes');
  127. % mask = ~mask;
  128. end