drawBoxesToImg.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. function imWithBoxes = drawBoxesToImg(im, boxes, partcolor, i_linewidth)
  2. % showboxes(im, boxes)
  3. % Draw boxes on top of image.
  4. if nargin < 4
  5. i_linewidth = 1;
  6. end
  7. if nargin < 3,
  8. partcolor{1} = [255,0,0];
  9. partcolor(2:20) = num2cell(repmat( [0,0,255], [19,1]),2);
  10. end
  11. if ( nargout == 0)
  12. %imagesc(im); axis image; axis off;
  13. imshow(im); hold on;
  14. if ~isempty(boxes)
  15. numparts = floor(size(boxes, 2)/4);
  16. for i = 1:numparts
  17. x1 = boxes(:,1+(i-1)*4);
  18. y1 = boxes(:,2+(i-1)*4);
  19. x2 = boxes(:,3+(i-1)*4);
  20. y2 = boxes(:,4+(i-1)*4);
  21. line([x1 x1 x2 x2 x1]',[y1 y2 y2 y1 y1]','Color',partcolor{i},'linewidth',i_linewidth);
  22. end
  23. end
  24. drawnow;
  25. hold off;
  26. else
  27. if ( ndims(im) == 2 )
  28. imWithBoxes = repmat( im, [1,1,3] );
  29. else
  30. imWithBoxes = im;
  31. end
  32. hold on;
  33. i_offset = i_linewidth/2;
  34. %imagesc(im); axis image; axis off;
  35. if ~isempty(boxes)
  36. numparts = floor(size(boxes, 2)/4);
  37. for i = 1:numparts
  38. %[dist-to-left dist-to-top dist-to-left+width dist-to-top+height]
  39. x1 = boxes(:,1+(i-1)*4);
  40. y1 = boxes(:,2+(i-1)*4);
  41. x2 = boxes(:,3+(i-1)*4);
  42. y2 = boxes(:,4+(i-1)*4);
  43. [ height, width, ~ ] = size ( imWithBoxes );
  44. i_minLeft = max ( 1 , x1-i_offset );
  45. i_maxRight = min ( width, x2+i_offset);
  46. i_minBottom = max ( 1 , y1-i_offset );
  47. i_maxTop = min ( height, y2+i_offset);
  48. %
  49. imWithBoxes( i_minBottom:y2+i_offset, i_minLeft:x1+i_offset, 1) = partcolor{i}(1);
  50. imWithBoxes( i_minBottom:y2+i_offset, i_minLeft:x1+i_offset, 2) = partcolor{i}(2);
  51. imWithBoxes( i_minBottom:y2+i_offset, i_minLeft:x1+i_offset, 3) = partcolor{i}(3);
  52. %
  53. imWithBoxes( i_minBottom:i_maxTop, x2-i_offset:i_maxRight, 1) = partcolor{i}(1);
  54. imWithBoxes( i_minBottom:i_maxTop, x2-i_offset:i_maxRight, 2) = partcolor{i}(2);
  55. imWithBoxes( i_minBottom:i_maxTop, x2-i_offset:i_maxRight, 3) = partcolor{i}(3);
  56. %
  57. imWithBoxes( i_minBottom:y1+i_offset, x1:x2, 1) = partcolor{i}(1);
  58. imWithBoxes( i_minBottom:y1+i_offset, x1:x2, 2) = partcolor{i}(2);
  59. imWithBoxes( i_minBottom:y1+i_offset, x1:x2, 3) = partcolor{i}(3);
  60. %
  61. imWithBoxes( y2-i_offset:i_maxTop, x1:x2, 1) = partcolor{i}(1);
  62. imWithBoxes( y2-i_offset:i_maxTop, x1:x2, 2) = partcolor{i}(2);
  63. imWithBoxes( y2-i_offset:i_maxTop, x1:x2, 3) = partcolor{i}(3);
  64. end
  65. end
  66. end