From: DS on
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
From: Ian Clarkson on
"DS " <null(a)null.com> wrote in message
<g69uap$ivq$1(a)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.
From: Adam on
"DS " <null(a)null.com> wrote in message
<g69uap$ivq$1(a)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

From: Adam on
"Adam " <not.real(a)email.com> wrote in message
<g69vrg$cnj$1(a)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
From: DS on
Ian and Adam - thanks for the help, that's just what I was
looking for.

 | 
Pages: 1
Prev: matlabpool error
Next: 64-bit mex problems