Path: news.mathworks.com!not-for-mail
From: "jay vaughan" <jvaughan5.nospam@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: vectorize drawing of bounding boxes
Date: Wed, 7 May 2008 17:39:03 +0000 (UTC)
Organization: harvard
Lines: 49
Message-ID: <fvspfn$m20$1@fred.mathworks.com>
Reply-To: "jay vaughan" <jvaughan5.nospam@gmail.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210181943 22592 172.30.248.35 (7 May 2008 17:39:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 7 May 2008 17:39:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1215048
Xref: news.mathworks.com comp.soft-sys.matlab:467211



Hi,

I am trying to optimize the speed of drawing a bunch of
bounding boxes on identified objects. I know to try to avoid
loops, but wasn't sure how to vectorize the code (below).
Any ideas on vectorizing this process or speeding it up in
general? Eventually it will be done many times per second,
so I do need the speed.

A simulation of this below where the first part generates
the coordinates the objects, and the second part is a loop
that creates the boxes.

% FIRST PART, CREATE (RANDOM) BOX COORDINTES
num_boxes= 1000;
box_size= 13;
frame_size = [512 512];

rows = (frame_size(1)-1)*rand(num_boxes,1);
cols = (frame_size(2)-1)*rand(num_boxes,1);
box_coords = round([rows cols]);

% SECOND PART, CREATE BOXES (IF NOT TOO CLOSE TO EDGE)
M = zeros(frame_size);
tic
for k = 1:size(box_coords,1)
    dl = floor(box_size/2);
    dr = ceil(box_size/2);
    R1 = box_coords(k,1)-dl;
    R2 = box_coords(k,1)+dr;
    C1 = box_coords(k,2)-dl;
    C2 = box_coords(k,2)+dl;
    if R1>=1 && C1>=1 && R2<=frame_size(1) && ...
      C2<=frame_size(2)
        M(R1:R2,C1) = 1;
        M(R1:R2,C2) = 1;
        M(R1,C1:C2) = 1;
        M(R2,C1:C2) = 1;
    end
end
toc
% 0.043 seconds on my computer. I was hoping to get <0.01

imagesc(M)
colormap gray
set(gcf,'position',[200 200 frame_size(2) frame_size(1)])
set(gca,'position',[0 0 1 1]);