MATLAB: Saving the image with the bounding box

Image Acquisition Toolboximage processing rectangle bounding box

I'm saving my mask1 in a folder to visualize the image processing but i want to add to my image (when im using imwrite) my rectangle (BoundingBox) in the same image. I create my bounding box with :
s=find([numberOfblobs.Area]>500 & [numberOfblobs.Area]<3500);
Here's my code:
% function [ image ] = BatchLAB()
clear all;
close all;
%Delete images in output folder
delete('C:\Documents and Settings\sidussault\Bureau\Projet intervention\Image test 3\00020_07_230_000D_D1_(0)_20110503_d\Test\traiter\*.jpg');
% Go get all my Jpgs in my folder
files=dir('C:\Documents and Settings\sidussault\Bureau\Projet intervention\Image test 3\00020_07_230_000D_D1_(0)_20110503_d\Test\*.jpg');
addpath('C:\Documents and Settings\sidussault\Bureau\Projet intervention\Image test 3\00020_07_230_000D_D1_(0)_20110503_d\Test');
addpath('C:\Documents and Settings\sidussault\Bureau\Projet intervention\Image test 3\00020_07_230_000D_D1_(0)_20110503_d\Test\traiter');
%Batch processing loop
for k=1:numel(files)
cform = makecform('srgb2lab');
img=imread(files(k).name);
imgLAB=applycform(img,cform);
img1=imresize(imgLAB,[800 800]);
imgBW=im2bw(img1);
imgBW1 = bwmorph(imgBW,'shrink');
imgBW2 = bwmorph(imgBW1,'majority');
imgBW3 = bwmorph(imgBW2,'thin');
mask = imopen(imgBW3, strel('Disk',1));
mask1 = imfill(mask,[200 200],6);
[L Ne]= bwlabel(mask1);
numberOfblobs=regionprops(L,'all');
for n=1:size(numberOfblobs,1)
rectangle('Position',numberOfblobs(n).BoundingBox,'EdgeColor','r','LineWidth',2)
end
s=find([numberOfblobs.Area]>500 & [numberOfblobs.Area]<3500);
for n=1:size(s,2)
rectangle('Position',numberOfblobs(s(n)).BoundingBox,'EdgeColor','g','LineWidth',2)
end
output_name=['C:\Documents and Settings\sidussault\Bureau\Projet intervention\Image test 3\00020_07_230_000D_D1_(0)_20110503_d\Test\traiter\' files(1).name];
imwrite(mask1, ['C:\Documents and Settings\sidussault\Bureau\Projet intervention\Image test 3\00020_07_230_000D_D1_(0)_20110503_d\Test\traiter\' files(k).name]);
end

Best Answer

The rectangle function (and others like plot, scatter, etc) is useful inside of Matlab to visualize certain aspects of your data. However, you are somewhat limited in saving those plots.
A much more flexible approach is to create a second image using the bounding box coordinates, and then save that using imwrite just as you are doing with your mask image. I thought there were several examples of functions to do this in the File Exchange, but it looks like Brett has the only one...but a very flexible one at that: http://www.mathworks.com/matlabcentral/fileexchange/38721-embed-text-and-graphics-in-an-image
But, a web search for "matlab lineburning" will turn up a few more directed alternatives.