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