title: Draw bounding boxes for entire word instead of each character in an image - MATLAB
body: <p>I have a MATLAB code that draws bounding boxes around each letter.</p>
  
  <p>I would like to draw these boxes around each word, instead of each character.</p>
  
  <p>I had thought of </p>
  
  <ul>
  <li>reading the size of each space between words and based on that, separating each word.</li>
  <li>grouping adjacent rectangles into larger rectangles, which would essentially do the same thing for me. </li>
  </ul>
  
  <p>How would this be done?</p>
  
  <hr>
  
  <p>Here is the image so far:</p>
  
  <p><a href="http://imgur.com/iDF5VD4" rel="nofollow">http://imgur.com/iDF5VD4</a></p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>%CLEAR EVERYTHING
  clear all;
  close all;
  
  %SET FOLDER AND FILE LOCATION
  folder = 'H:\Miscellaneous\Work\Project';
  baseFileName = 'lorem-ipsum.jpg';
  fullFile = fullfile(folder, baseFileName);
  
  %CONVERT TO GRAYSCALE
  normal = imread(fullFile);
  gray = rgb2gray(normal);
  
  %CONVERT TO BINARY IMAGE
  binary = im2bw(gray);
  
  %INVERT IMAGE
  binary = ~binary;
  
  %FILL HOLES
  ifill=imfill(binary,'holes');
  figure,imshow(ifill)
  
  %COUNT LETTER IN TEXT
  [Ilabel num]=bwlabel(ifill);
  disp(num)
  
  %CALCULATE REGION PROPERTIES
  Iprops=regionprops(Ilabel);
  
  %SET BOX PROPERTIES INTO VARIABLE
  Ibox=[Iprops.BoundingBox];
  
  %RESHAPE 1-D ARRAY
  Ibox=reshape(Ibox,[4 num]);
  
  %DRAW BOUNDING BOXES FOR EACH LETTER
  for cnt=1:num
      rectangle('position',Ibox(:,cnt),'edgecolor','r');
  end
  hold off
  </code></pre>