Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3D matrix operation speed up

Subject: 3D matrix operation speed up

From: francesco santi

Date: 12 Feb, 2009 16:03:01

Message: 1 of 12

Hello, I have the following problem.
I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component.
I have a 1x3 vector which contains the mean value of the R G B components.
I've done it with a for loop but it's very slow for big images (i.e. matrix) and I'm pretty sure there's a faster way to do it.
I've tried also to put all the 3 RGB components in a cell, so I have in position r,c a vector of 3 color components. Then I am using a for loop and cell2mat to do the same operation as above. But again I need to replace the for loop for speed.
Any help would be appreciated.
Thanks in advance,
Francesco

Subject: 3D matrix operation speed up

From: nor ki

Date: 12 Feb, 2009 16:11:01

Message: 2 of 12

"francesco santi" <fpsanti@gmail.com> wrote in message <gn1h7l$qs0$1@fred.mathworks.com>...
> Hello, I have the following problem.
> I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component.
> I have a 1x3 vector which contains the mean value of the R G B components.
> I've done it with a for loop but it's very slow for big images (i.e. matrix) and I'm pretty sure there's a faster way to do it.
> I've tried also to put all the 3 RGB components in a cell, so I have in position r,c a vector of 3 color components. Then I am using a for loop and cell2mat to do the same operation as above. But again I need to replace the for loop for speed.
> Any help would be appreciated.
> Thanks in advance,
> Francesco

Hello Francesco,

is this the loop you used?

for n = 1:3
    im(:,:,n) = im(:,:,n)-vec(n);
end

then maybe you can accelerate it with imsubtract if you have the image processing toolbox
cheers
kinor

Subject: 3D matrix operation speed up

From: us

Date: 12 Feb, 2009 16:15:05

Message: 3 of 12

"francesco santi"
> I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component...

one of the many solutions

% the data
     m=ceil(10*rand(2,4,3));
     v=[10,100,1000];
% the engine
     r=arrayfun(@(x) m(:,:,x)-v(x),1:3,'uni',false);
     r=cat(3,r{:});
% the result
     disp(r);
%{
(:,:,1) =
    -7 -2 -9 -2
    -3 -2 -1 -1
(:,:,2) =
   -94 -92 -96 -97
   -97 -98 -90 -96
(:,:,3) =
  -991 -997 -990 -994
  -996 -994 -993 -992
%}

us

Subject: 3D matrix operation speed up

From: us

Date: 12 Feb, 2009 16:28:02

Message: 4 of 12

"francesco santi"
> I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component...

one of the many solutions

% the data
     m=ceil(10*rand(2,4,3));
     v=[10,100,1000];
% the engine
     r=arrayfun(@(x) m(:,:,x)-v(x),1:3,'uni',false);
     r=cat(3,r{:});
% the result
     disp(r);
%{
(:,:,1) =
    -7 -2 -9 -2
    -3 -2 -1 -1
(:,:,2) =
   -94 -92 -96 -97
   -97 -98 -90 -96
(:,:,3) =
  -991 -997 -990 -994
  -996 -994 -993 -992
%}

us

Subject: 3D matrix operation speed up

From: francesco santi

Date: 12 Feb, 2009 17:28:02

Message: 5 of 12

> is this the loop you used?
>
> for n = 1:3
> im(:,:,n) = im(:,:,n)-vec(n);
> end
>
> then maybe you can accelerate it with imsubtract if you have the image processing toolbox

% this generates the cell which contains the image
HSV = hsv2rgb(im);
ImageMatrix = cell(HSV_rows, HSV_cols);
for r=1:HSV_rows
    for c=1:HSV_cols
        ImageMatrix{r,c} = [HSV(r,c,1) HSV(r,c,2) HSV(r,c,3)];
    end
end
%this is the loop to compute the subtraction between a pixel and its mean
for r=1:HSV_rows
    for c=1:HSV_cols
        fl = double(cell2mat(ImageMatrix(r,c))); %pixel color components 1x3
        temp(r,c) = (fl - Ml); %Ml=mean vector 1x3
     end
end

Subject: 3D matrix operation speed up

From: francesco santi

Date: 12 Feb, 2009 19:36:01

Message: 6 of 12

Thanks us for the detailed and quick reply.
Could you please have a look to the code that I have posted and formulate another "ad hoc" solution with cell?
Maybe with similar size and variables name to make it easier to understand.
Thank you again for your time and help.

Subject: 3D matrix operation speed up

From: francesco santi

Date: 16 Feb, 2009 22:16:01

Message: 7 of 12

Thanks "us" for the detailed and quick reply.
Could you please have a look to the code that I have posted and formulate another "ad hoc" solution with cell?
Maybe with similar size and variables name to make it easier to understand.
Sorry but Matlab help is quite short and not very clear, for me, about this function.
Thank you again for your time and help.

Subject: 3D matrix operation speed up

From: nor ki

Date: 17 Feb, 2009 09:32:01

Message: 8 of 12

"francesco santi" <fpsanti@gmail.com> wrote in message <gn1m72$r6k$1@fred.mathworks.com>...
> > is this the loop you used?
> >
> > for n = 1:3
> > im(:,:,n) = im(:,:,n)-vec(n);
> > end
> >
> > then maybe you can accelerate it with imsubtract if you have the image processing toolbox
>
> % this generates the cell which contains the image
> HSV = hsv2rgb(im);
> ImageMatrix = cell(HSV_rows, HSV_cols);
> for r=1:HSV_rows
> for c=1:HSV_cols
> ImageMatrix{r,c} = [HSV(r,c,1) HSV(r,c,2) HSV(r,c,3)];
> end
> end
> %this is the loop to compute the subtraction between a pixel and its mean
> for r=1:HSV_rows
> for c=1:HSV_cols
> fl = double(cell2mat(ImageMatrix(r,c))); %pixel color components 1x3
> temp(r,c) = (fl - Ml); %Ml=mean vector 1x3
> end
> end

Hi Francesco,
work on whole matrices, for example with the solution of 'us' or with the loop i proposed. Changing names should be your business. The matlab help is really nice indeed, have fun.

kinor

Subject: 3D matrix operation speed up

From: francesco santi

Date: 17 Feb, 2009 10:15:03

Message: 9 of 12

Thanks again for the answer.
Sorry, but I didn't want someone to do my job. :)
I am quite new to Matlab and for me the Matlab help seems not so easy/clear.
I tried the arrayfun solution and it works, but not as i would like to.
It was my fault because I couldn't explain the problem very well!
I'll try again.

I have a MxNx3 matrix which is an image.
I have to compute the following formula for each pixel and store the result in a MxN matrix.
Singleton(row,col) = (px(row,col)-m(class)) * invS(class) * (px(row,col)-m(class))';

px=RGB_matrix(row,col,:):
px=reshape(px,1,3); %I have the 3 RGB color components of one pixel in one row
m=[mean(R) mean(G) mean(B)]; %the mean vector contains the mean value of the color components
invS %is the inverse of the covariance matrix

The mean vector and inverse matrix change depending on the class which the pixel belongs to. So I also wrote a for loop to compute all the m and invS belonging to each class.

I hope the problem is more clear now.
I have to compute that formula over all the pixels trying to avoid for loops. More over I have to store the result in a new MxN matrix in the same position of the original pixel.
Now I am using 3 for loops and for big images it is very slow, of course.

Subject: 3D matrix operation speed up

From: francesco santi

Date: 17 Feb, 2009 10:20:05

Message: 10 of 12

this is the detailed function that i am using.
I hope it help you to understand better my problem.

function Singleton = singleton(classes, Parameters)
global HSV_rows HSV_cols HSV
%HSV is the image matrix (RGB->HSV color space)

Singleton = cell(1,classes);
for l=1:classes
    temp = zeros(HSV_rows, HSV_cols);
    detSl = Parameters(l).DetermS; %determinant
    temp2 = -log(1/(sqrt(power(2*pi,3)*detSl)));
    Ml = Parameters(l).Med; %mean value
    invSl = Parameters(l).InverS; %inverse covariance matrix
    for r=1:HSV_rows
        for c=1:HSV_cols
            fl = reshape(HSV(r,c,:),1,3); %color components of pixel in r,c
            temp(r,c) = (fl - Ml) * invSl * (fl - Ml)';
        end
    end
    Singleton{l} = temp2+temp*.5; %storing the result here
end

Subject: 3D matrix operation speed up

From: Jos

Date: 17 Feb, 2009 10:32:02

Message: 11 of 12

"francesco santi" <fpsanti@gmail.com> wrote in message <gne2n7$5f2$1@fred.mathworks.com>...
> Thanks again for the answer.
> Sorry, but I didn't want someone to do my job. :)
> I am quite new to Matlab and for me the Matlab help seems not so easy/clear.
> I tried the arrayfun solution and it works, but not as i would like to.
> It was my fault because I couldn't explain the problem very well!
> I'll try again.
>
> I have a MxNx3 matrix which is an image.
> I have to compute the following formula for each pixel and store the result in a MxN matrix.
> Singleton(row,col) = (px(row,col)-m(class)) * invS(class) * (px(row,col)-m(class))';
>
> px=RGB_matrix(row,col,:):
> px=reshape(px,1,3); %I have the 3 RGB color components of one pixel in one row
> m=[mean(R) mean(G) mean(B)]; %the mean vector contains the mean value of the color components
> invS %is the inverse of the covariance matrix
>
> The mean vector and inverse matrix change depending on the class which the pixel belongs to. So I also wrote a for loop to compute all the m and invS belonging to each class.
>
> I hope the problem is more clear now.
> I have to compute that formula over all the pixels trying to avoid for loops. More over I have to store the result in a new MxN matrix in the same position of the original pixel.
> Now I am using 3 for loops and for big images it is very slow, of course.

Did you look at the PROFILER to see the bottlenecks of your code?

help profile

Jos

Subject: 3D matrix operation speed up

From: francesco santi

Date: 17 Feb, 2009 12:23:01

Message: 12 of 12

yes, i am using the profiler and the most used and time consuming function is that "singleton"

Tags for this Thread

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.

Contact us