Thread Subject: pixel list from bounding box?

Subject: pixel list from bounding box?

From: DS

Date: 24 Jul, 2008 12:59:05

Message: 1 of 5

Can anyone suggest a better way to do the following? I
want a pixel list (x,y), not linear indices, of a region in
an image specified by a bounding box ... Below is a for-
loop sort of brute force approach, but I get the impression
this isn't a good way to do this in MatLab. Any advice?


function [xlist ylist] = pixel_list(box)
% Return list of pixel coordinates for a specified bounding
box.
%
% BOX is a vector which specifies a region [ul_corner
width], where
% ul_corner is in the form [x y] and specifies the upper
left corner of
% the rectangular region. width is in the form [x_width
y_width] and
% specifies the width of the region along each dimension.
%
% Example:
% box = [1 1 2 3]; % define 2x3 pixel area
% [x y] = pixel_list([1 1 2 3])
%
% ans: x=[1 1 2 2 3 3] y=[1 2 3 1 2 3]
%
% box = [50 50 2 3]; % define 2x3 pixel area
% [x y] = pixel_list([1 1 2 3])
%
% ans: x=[50 50 51 51 52 52] y=[50 51 52 50
51 52]
%
%
% See also REGIONPROPS

%preallocate xlist
xlist = zeros(box(3)*box(4),1);
%outer loop
for m=1:box(4)
    %inner loop
    for n=1:box(3)
        box(1)+1-m;
    end
end

%preallocate ylist
ylist = zeros(box(3)*box(4),1);
%outer loop
for m=1:box(4)
    %inner loop
    for n=1:box(3)
        box(2)+1-m;
    end
end

Subject: pixel list from bounding box?

From: Ian Clarkson

Date: 24 Jul, 2008 13:24:02

Message: 2 of 5

"DS " <null@null.com> wrote in message
<g69uap$ivq$1@fred.mathworks.com>...
> Can anyone suggest a better way to do the following? I
> want a pixel list (x,y), not linear indices, of a region in
> an image specified by a bounding box ... Below is a for-
> loop sort of brute force approach, but I get the impression
> this isn't a good way to do this in MatLab. Any advice?
>
>
> function [xlist ylist] = pixel_list(box)
> % Return list of pixel coordinates for a specified bounding
> box.
> %
> % BOX is a vector which specifies a region [ul_corner
> width], where
> % ul_corner is in the form [x y] and specifies the upper
> left corner of
> % the rectangular region. width is in the form [x_width
> y_width] and
> % specifies the width of the region along each dimension.
> %
> % Example:
> % box = [1 1 2 3]; % define 2x3 pixel area
> % [x y] = pixel_list([1 1 2 3])
> %
> % ans: x=[1 1 2 2 3 3] y=[1 2 3 1 2 3]
> %
> % box = [50 50 2 3]; % define 2x3 pixel area
> % [x y] = pixel_list([1 1 2 3])
> %
> % ans: x=[50 50 51 51 52 52] y=[50 51 52 50
> 51 52]
> %
> %
> % See also REGIONPROPS
>
> %preallocate xlist
> xlist = zeros(box(3)*box(4),1);
> %outer loop
> for m=1:box(4)
> %inner loop
> for n=1:box(3)
> box(1)+1-m;
> end
> end
>
> %preallocate ylist
> ylist = zeros(box(3)*box(4),1);
> %outer loop
> for m=1:box(4)
> %inner loop
> for n=1:box(3)
> box(2)+1-m;
> end
> end

i'm not sure if your example code is correct: the "answer"
that you seek seems to imply that the coordinate (3,3)
exists, but isn't that impossible given your box dimensions?
try the following:

box = [1 1 2 3];
boxex = zeros(box(4),box(3));
[ylist xlist] = ind2sub(size(boxex),1:numel(boxex));

you could be even more clever about it by just creating the
matrices yourself using repmat() and the like, but it may
not be as illustrative.

>> xlist

xlist =

     1 1 1 2 2 2

>> ylist

ylist =

     1 2 3 1 2 3

hope this helps.

Subject: pixel list from bounding box?

From: Adam

Date: 24 Jul, 2008 13:25:04

Message: 3 of 5

"DS " <null@null.com> wrote in message
<g69uap$ivq$1@fred.mathworks.com>...
> Can anyone suggest a better way to do the following? I
> want a pixel list (x,y), not linear indices, of a region in
> an image specified by a bounding box ... Below is a for-
> loop sort of brute force approach, but I get the impression
> this isn't a good way to do this in MatLab. Any advice?
>
>
> function [xlist ylist] = pixel_list(box)
> % Return list of pixel coordinates for a specified bounding
> box.
> %
> % BOX is a vector which specifies a region [ul_corner
> width], where
> % ul_corner is in the form [x y] and specifies the upper
> left corner of
> % the rectangular region. width is in the form [x_width
> y_width] and
> % specifies the width of the region along each dimension.
> %
> % Example:
> % box = [1 1 2 3]; % define 2x3 pixel area
> % [x y] = pixel_list([1 1 2 3])
> %
> % ans: x=[1 1 2 2 3 3] y=[1 2 3 1 2 3]
> %
> % box = [50 50 2 3]; % define 2x3 pixel area
> % [x y] = pixel_list([1 1 2 3])
> %
> % ans: x=[50 50 51 51 52 52] y=[50 51 52 50
> 51 52]
> %
> %
> % See also REGIONPROPS
>
> %preallocate xlist
> xlist = zeros(box(3)*box(4),1);
> %outer loop
> for m=1:box(4)
> %inner loop
> for n=1:box(3)
> box(1)+1-m;
> end
> end
>
> %preallocate ylist
> ylist = zeros(box(3)*box(4),1);
> %outer loop
> for m=1:box(4)
> %inner loop
> for n=1:box(3)
> box(2)+1-m;
> end
> end

You're not assigning anything in your loops

[xlist ylist] = meshgrid(box(1):box(1)+box(3),
box(2):box(2)+box(4));

~Adam

Subject: pixel list from bounding box?

From: Adam

Date: 24 Jul, 2008 13:34:02

Message: 4 of 5

"Adam " <not.real@email.com> wrote in message
<g69vrg$cnj$1@fred.mathworks.com>...

> [xlist ylist] = meshgrid(box(1):box(1)+box(3),
> box(2):box(2)+box(4));
>
> ~Adam
 
meh, feeling OCD

[xlist ylist] =
meshgrid(box(1):box(1)+box(3)-1,box(2):box(2)+box(4)-1);

xlist = xlist(:).';
ylist = ylist(:).';

Adam

Subject: pixel list from bounding box?

From: DS

Date: 24 Jul, 2008 16:34:02

Message: 5 of 5

Ian and Adam - thanks for the help, that's just what I was
looking for.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com