Thread Subject: vectorization help please...

Subject: vectorization help please...

From: ozizus

Date: 18 Jan, 2008 17:51:32

Message: 1 of 3

B is a two dimensional array (image). A is a cell array. Each cell of
A is a 5x5 array (a neigborhood). R and C contains row and column
indices of the centers of the neighborhoods. I want to assign (now
edited) neighborhoods back on to B:

for i=1:numel(R)
   B(R(i)-2:R(i)+2,C(i)-2:C(i)+2)= A{i};
end

can I get rid of 'for' loop? thx in advance.

Subject: vectorization help please...

From: Anh Huy Phan

Date: 18 Jan, 2008 19:35:03

Message: 2 of 3

ozizus <oguz@ce.yildiz.edu.tr> wrote in message <0d01631a-
f719-4396-95fa-4e5dfeec1cb3@u10g2000prn.googlegroups.com>...
> B is a two dimensional array (image). A is a cell array.
Each cell of
> A is a 5x5 array (a neigborhood). R and C contains row and
column
> indices of the centers of the neighborhoods. I want to
assign (now
> edited) neighborhoods back on to B:
>
> for i=1:numel(R)
> B(R(i)-2:R(i)+2,C(i)-2:C(i)+2)= A{i};
> end
>
> can I get rid of 'for' loop? thx in advance.

Here is an example with image Cameraman.tif.
   - Blocksize d = 20
   - Number of blocks: nblks = 4
   -

%% read image and initialize data
im = im2double(imread('cameraman.tif'));
nblks = 4;
% A = rand(d,d,nblks );
d = 15; %
A = squeeze(mat2cell(rand(d,d,nblks),d,d,ones(1,nblks)));
r = [30 20 50 150];
c = [20 50 30 200];

%% generate one reference block d x d

[cc,rr] = meshgrid(1:d);
cc = cc(:);
rr = rr(:);

dataf = cell2mat(A); % convert

%% generate locations of pixels in all blocks

x = rr(:,ones(1,nblks)) + r(ones(d^2,1),:)-(d+1)/2;
y = cc(:,ones(1,nblks)) + c(ones(d^2,1),:)-(d+1)/2;

%% fill data into image
imf = im;
imf(sub2ind(size(imf),x,y)) = dataf;

figure
imshow(imf)

By the way, you can use repmat, bsxfun, ...


HTH

Anh Huy Phan
RIKEN - BSI

Subject: vectorization help please...

From: Anh Huy Phan

Date: 18 Jan, 2008 19:40:26

Message: 3 of 3

ozizus <oguz@ce.yildiz.edu.tr> wrote in message <0d01631a-
f719-4396-95fa-4e5dfeec1cb3@u10g2000prn.googlegroups.com>...
> B is a two dimensional array (image). A is a cell array.
Each cell of
> A is a 5x5 array (a neigborhood). R and C contains row and
column
> indices of the centers of the neighborhoods. I want to
assign (now
> edited) neighborhoods back on to B:
>
> for i=1:numel(R)
> B(R(i)-2:R(i)+2,C(i)-2:C(i)+2)= A{i};
> end
>
> can I get rid of 'for' loop? thx in advance.

Here is an example with image Cameraman.tif.
   - Blocksize d = 20
   - Number of blocks: nblks = 4
   -

%% read image and initialize data
im = im2double(imread('cameraman.tif'));
nblks = 4;
% A = rand(d,d,nblks );
d = 15; %
A = squeeze(mat2cell(rand(d,d,nblks),d,d,ones(1,nblks)));
r = [30 20 50 150];
c = [20 50 30 200];

%% generate one reference block d x d

[cc,rr] = meshgrid(1:d);
cc = cc(:);
rr = rr(:);

dataf = cell2mat(A); % convert

%% generate locations of pixels in all blocks

x = rr(:,ones(1,nblks)) + r(ones(d^2,1),:)-(d+1)/2;
y = cc(:,ones(1,nblks)) + c(ones(d^2,1),:)-(d+1)/2;

%% fill data into image
imf = im;
imf(sub2ind(size(imf),x,y)) = dataf;

figure
imshow(imf)

By the way, you can use repmat, bsxfun, ...


HTH

Anh Huy Phan
RIKEN - BSI

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