selectButterflies.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. function selectButterflies ( s_img, s_dest )
  2. % function selectButterflies ( s_img, s_dest )
  3. %
  4. % BRIEF
  5. % Select bounding boxes for images, save those guys to a specified
  6. % folder, and keep track of everything with a small cache.
  7. %
  8. % INPUT
  9. % s_img -- string specifying name of image to be annotated
  10. % s_dest -- where to save cropped images to
  11. %
  12. % OUTPUT
  13. % none
  14. %
  15. s_cacheDest = './cacheButterflies.mat';
  16. if ( exist ( s_cacheDest ) )
  17. load ( s_cacheDest, 'cacheButterflies', 'i_counter' );
  18. end
  19. im = imread ( s_img );
  20. figImg = figure;
  21. figCrop = figure;
  22. b_continueSelection = true;
  23. i_numBoxesPerClick = 1;
  24. imWithBoxes = im;
  25. %
  26. i_linewidth = 3;
  27. partcolor{1} = [255,0,0];
  28. if ( ~exist ( 'cacheButterflies' ) )
  29. cacheButterflies = struct( 'name', 'rectangle' ) ;
  30. else
  31. idxSameImg = strcmp ( {cacheButterflies.name}, s_img);
  32. bBox = {cacheButterflies.rectangle};
  33. bBoxSameImg = cell2mat(bBox(idxSameImg)');
  34. imWithBoxes = drawBoxesToImg(imWithBoxes, bBoxSameImg, partcolor, i_linewidth);
  35. end
  36. if ( ~exist ( 'i_counter') )
  37. i_counter = 1 ;
  38. end
  39. s_prompt = sprintf('Press enter for save+continue, r for retry, and X for abort\n');
  40. while b_continueSelection
  41. figure ( figImg );
  42. imshow ( imWithBoxes );
  43. [xmin, ymin, xmax, ymax] = clickBoundingBoxes2D ( i_numBoxesPerClick );
  44. xmin = int32(round(xmin));
  45. ymin = int32(round(ymin));
  46. xmax = int32(round(xmax));
  47. ymax = int32(round(ymax));
  48. imTmp = imcrop(im, [xmin, ymin, xmax-xmin, ymax-ymin]);
  49. figure ( figCrop );
  50. imshow ( imTmp );
  51. result = input(s_prompt, 's');
  52. while ( ~isempty(result) && (strcmp(result,'r')==0) && (strcmp(result,'X')==0) )
  53. disp(sprintf('Retry input!\n'))
  54. result = input(s_prompt, 's');
  55. end
  56. if ( strcmp(result,'X') )
  57. return;
  58. end
  59. if ( isempty(result) )
  60. % write current proposal
  61. s_destFig = sprintf('%s_%03d.png',s_dest, i_counter);
  62. imwrite ( imTmp, s_destFig );
  63. % save results to cache
  64. cacheButterflies( i_counter ).name = s_img;
  65. cacheButterflies( i_counter ).rectangle = [xmin,ymin,xmax,ymax];
  66. i_counter = i_counter +1;
  67. save ( s_cacheDest, 'cacheButterflies', 'i_counter' );
  68. % plot current result
  69. newproposal = [xmin,ymin,xmax,ymax];
  70. imWithBoxes = drawBoxesToImg(imWithBoxes, newproposal, partcolor, i_linewidth);
  71. end
  72. end
  73. end