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:
for faster spped and shorter time in image processing

Subject: for faster spped and shorter time in image processing

From: ravi

Date: 9 Aug, 2010 15:42:04

Message: 1 of 5

Hi guys, I have an application,i.e a rather large algorithm consisting of a numerous amount of looping and significant amount of data processing ,specifically image pixels of about 640x480.

The processing time is a bit slow. Should I code some of the mathematics in c++ and use MEX or something like that? or are there any other options?

would like some advice

ravi

Subject: for faster spped and shorter time in image processing

From: Andy

Date: 9 Aug, 2010 15:51:04

Message: 2 of 5

"ravi " <ravi_071@hotmail.com> wrote in message <i3p7kc$jfd$1@fred.mathworks.com>...
> Hi guys, I have an application,i.e a rather large algorithm consisting of a numerous amount of looping and significant amount of data processing ,specifically image pixels of about 640x480.
>
> The processing time is a bit slow. Should I code some of the mathematics in c++ and use MEX or something like that? or are there any other options?
>
> would like some advice
>
> ravi

Have you used the profiler to see where the bottleneck in your code is? How slow is it? Have you made sure to do basic things like vectorize where possible, preallocate arrays that are calculated in loops, eliminate unnecessary function-calling overhead, etc.? Do you have specific code that you can show us that is not performing as fast as you'd like it to?

Subject: for faster spped and shorter time in image processing

From: ravi

Date: 9 Aug, 2010 15:59:05

Message: 3 of 5

Andy,
I will look over these these you suggested and then post what I have found.

thank you
ravi

Subject: for faster speed and shorter time in image processing

From: ravi

Date: 10 Aug, 2010 03:13:04

Message: 4 of 5

hi guys,

I am in some difficulties with respect to processing time.

I ran my code using Profiler and below displays a snippet from Profiler where the 'bottleneck' neck is:

 ====================================================
 1319.21 345073 574 Mi= [Mi1 Mi2 Mi3; Mi4 Mi5 Mi6; Mi7 Mi8 Mi9] ;
 464.50 345073 575 B_matrix = [ BA BB BC];
====================================================

As you can see times are 1319.21 and 464.50 seconds.Below the page is my code with the test array.

This issue with the times happens when I use an image array of 640 x 480.
However, it is an insignificant issue when I use an test array of 12 x 12 that I used to test the mathematics of my application. I was in shock at the ridiculous time when I finished my application and decided to test the real 640x480 image .

I couldnt post the 640x480 matrix withe the code below because of its shear size so I put the 12x12 array.

I'm asking for some help to optimize the time drastically.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;

IM = [105,109,104,108,107,101,107,110,105,105,106,102;106,100,104,106,100,105,106,103,105,102,108,106;...
       107,103,108,109,105,108,109,100,108,108,105,107;103,106,106,103,205,209,210,200,200,201,106,103;...
       105,105,110,100,103,202,210,202,205,208,108,105;105,102,108,102,100,208,203,208,205,210,109,105;....
       100,108,110,101,105,209,207,203,209,207,108,105;102,108,106,101,107,207,210,210,201,207,108,105;....
       109,107,103,101,100,104,107,107,101,105,104,104;108,104,110,103,107,109,101,101,107,106,100,107;....
       101,108,101,109,100,107,101,101,104,106,108,104;107,109,100,106,108,103,104,106,101,106,108, 210];


   ext = [ 3 10 6 11;
                   8 7 9 11;
                   3 5 4 10;
                   4 10 8 11;
                   5 5 8 6;
                   8 6 9 10]

 
       [ m n ] = size(ext) ;
       
              len = m


     GO= [ 0 0 0 0 0 0 0 0 0 0 0 0;
         0 7.2111 17.2627 8.9443 10.7703 21.2603 3.1623 15.8114 1.4142 8.9443 7.2111 0;
         0 11.4018 16.5529 134.6180 327.6126 417.4326 409.8902 393.2150 386.3315 306.7409 132.9662 0;
         0 12.6491 5.8310 188.8597 323.3110 311.4643 398.8483 402.8399 396.3231 415.8654 317.5154 0;
         0 16.4924 13.0384 130.2075 466.9047 341.5845 11.4018 22.8035 30.8869 384.8792 409.3531 0;
         0 21.0238 12.6491 28.2843 422.1706 415.2036 6.3246 3.1623 16.1245 390.0051 415.0012 0;
         0 27.8927 21.0238 17.2627 428.1682 410.1756 17.4929 7.6158 11.4018 391.1547 411.0304 0;
         0 12.0000 30.8869 13.4164 342.8615 445.4773 401.0112 400.6045 414.0193 424.8670 326.4506 0;
         0 6.3246 20.8806 11.3137 152.1184 324.5705 425.0012 421.2671 398.0050 318.2012 154.3826 0;
         0 8.2462 8.6023 17.2047 17.0294 4.0000 24.2074 17.4929 13.0384 11.4018 9.0554 0;
         0 5.0990 12.1655 4.2426 1.4142 11.3137 17.7200 11.4018 16.5529 8.2462 156.0833 0;
         0 0 0 0 0 0 0 0 0 0 0 0];
        
        
      
        L = [ 0 0 0 0 0 0 0 0 0 0 0 0;
           0 0 0 0 0 0 0 0 0 0 0 0;
           0 0 0 0 0 0 0 0 0 0 1 0;
           0 0 0 0 0 0 0 0 0 0 1 0;
           0 0 0 0 0 0 0 0 0 1 1 0;
           0 0 0 0 0 0 0 0 0 1 1 0;
           0 0 0 0 0 0 0 0 0 0 0 0;
           0 0 0 0 0 0 2 2 2 2 0 0;
           0 0 0 0 0 0 2 2 2 2 2 0;
           0 0 0 0 0 0 0 0 0 0 0 0;
           0 0 0 0 0 0 0 0 0 0 0 0;
           0 0 0 0 0 0 0 0 0 0 0 0]
       
       
      L2 =[ 0 0 0 0 0 0 0 0 0 0 0 0;
             0 0 0 0 0 0 0 0 0 0 0 0;
             0 0 0 0 3 3 3 3 3 3 0 0;
             0 0 0 0 0 0 3 3 3 0 4 0;
             0 0 0 0 0 5 0 0 0 4 4 0;
             0 0 0 0 5 5 0 0 0 4 4 0;
             0 0 0 0 5 5 0 0 0 4 4 0;
             0 0 0 0 5 0 6 6 6 0 4 0;
             0 0 0 0 0 6 6 6 6 6 0 0;
             0 0 0 0 0 0 0 0 0 0 0 0;
             0 0 0 0 0 0 0 0 0 0 0 0;
             0 0 0 0 0 0 0 0 0 0 0 0]
   
       
       

  A=zeros(len,1);
  B=zeros(len,1);
  C=zeros(len,1);
  D=zeros(len,1);
  E=zeros(len,1);
  F=zeros(len,1);
  G=zeros(len,1);
 H=zeros(len,1);
 I=zeros(len,1);


 BA=zeros(len,1);
 BB = zeros(len,1);
 BC=zeros(len,1);
 
    for k = 1:len

for indx=ext(k,1):ext(k,3)
    for jndx=ext(k,2):ext(k,4)



      if (L(indx,jndx) ==k || L2(indx,jndx) ==k)
        

          
% M(1,1) = M(1,1) + G(i,j) * i * i
% M(1,2) = M(1,2) + G(i,j) * i * j
% M(1,3) = M(1,3) + G(i,j) * i
% M(2,1) = M(1,2)
% M(2,2) = M(2,2) + G(i,j) * j * j
% M(2,3) = M(2,3) + G(i,j) * j
% M(3,1) = M(1,3)
% M(3,2) = M(2,3)
% M(3,3) = M(3,3) + G(i,j)
          
          
        A(k)=A(k) + GO(indx,jndx)*indx*indx;
        B(k)=B(k) + GO(indx,jndx)*indx*jndx;
        C(k)=C(k) + GO(indx,jndx)*indx;
        D(k)=B(k);
        E(k)=E(k) + GO(indx,jndx)*jndx*jndx;
        F(k)=F(k) + GO(indx,jndx)*jndx;
        G(k)=C(k);
        H(k)=F(k);
        I(k)=I(k) + GO(indx,jndx);
        
        BA(k) = BA(k) + GO(indx,jndx) * IM(indx,jndx) *indx
        BB(k) = BB(k) + GO(indx,jndx) * IM(indx,jndx) * jndx
        BC(k) = BC(k) + GO(indx,jndx) * IM(indx,jndx)
        
        
        a(k)=A(k)
        b(k)=B(k)
        c(k)=C(k)
        d(k)=E(k)
        e(k)= F(k)
        h(k)= I(k)
        
        KK(k) =-a(k) *d(k) *h(k) +a(k) *e(k) ^2+b(k) ^2*h(k) -2*b(k) *c(k) *e(k) +c(k) ^2*d(k)


       avg(k) = BC(k)/I(k)
        
  Mi1(k) = (-d(k) * h(k) +e(k) ^ 2) / KK(k)
  Mi2(k) = (b(k) *h(k) - c(k) * e(k)) / KK(k)
  Mi3(k) = (-b(k) * e(k) + c(k) * d(k)) / KK(k)
  Mi4(k) = Mi2(k)
  Mi5(k) = (- a(k) * h(k) + c(k) ^ 2) / KK(k)
  Mi6(k) = ( a(k)* e(k) -b(k) * c(k)) / KK(k)
  Mi7(k) = Mi3(k)
  Mi8(k) = Mi6(k)
  Mi9(k) = (- a(k) * d(k) + b(k) ^ 2) / KK(k)

  Mi= [Mi1 Mi2 Mi3; Mi4 Mi5 Mi6; Mi7 Mi8 Mi9]
       B_matrix = [ BA BB BC]
        
      end
    end
end
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
    
thanks
ravi

Subject: for faster speed and shorter time in image processing

From: Steven_Lord

Date: 10 Aug, 2010 13:34:41

Message: 5 of 5



"ravi " <ravi_071@hotmail.com> wrote in message
news:i3qg40$nr8$1@fred.mathworks.com...
> hi guys,
>
> I am in some difficulties with respect to processing time.
>
> I ran my code using Profiler and below displays a snippet from Profiler
> where the 'bottleneck' neck is:
>
> ====================================================
> 1319.21 345073 574 Mi= [Mi1 Mi2 Mi3; Mi4 Mi5 Mi6;
> Mi7 Mi8 Mi9] ; 464.50 345073 575 B_matrix = [ BA
> BB BC]; ====================================================
>
> As you can see times are 1319.21 and 464.50 seconds.Below the page is my
> code with the test array.
>
> This issue with the times happens when I use an image array of 640 x 480.
> However, it is an insignificant issue when I use an test array of 12 x 12
> that I used to test the mathematics of my application. I was in shock at
> the ridiculous time when I finished my application and decided to test the
> real 640x480 image .
>
> I couldnt post the 640x480 matrix withe the code below because of its
> shear size so I put the 12x12 array.
>
> I'm asking for some help to optimize the time drastically.
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> clc;
> clear all;

Converting this script file into a function file (and removing the previous
two commands) may offer better performance.

*snip*

> Mi1(k) = (-d(k) * h(k) +e(k) ^ 2) / KK(k)
> Mi2(k) = (b(k) *h(k) - c(k) * e(k)) / KK(k)
> Mi3(k) = (-b(k) * e(k) + c(k) * d(k)) / KK(k)
> Mi4(k) = Mi2(k)
> Mi5(k) = (- a(k) * h(k) + c(k) ^ 2) / KK(k)
> Mi6(k) = ( a(k)* e(k) -b(k) * c(k)) / KK(k)
> Mi7(k) = Mi3(k)
> Mi8(k) = Mi6(k)
> Mi9(k) = (- a(k) * d(k) + b(k) ^ 2) / KK(k)

You don't preallocate these matrices, so they're growing inside the loop.
Preallocate them to the size that they will be when you code is finished and
fill them in rather than growing them by one element each iteration through
the loop.

> Mi= [Mi1 Mi2 Mi3; Mi4 Mi5 Mi6; Mi7 Mi8 Mi9] B_matrix = [ BA BB BC]

Looking at your code, it looks like you don't use these inside the loop, so
why do you construct them inside the loop? Pull the concatenation of the
Mi*'s to AFTER the loop and perform that concatenation ONCE. The same holds
for B_matrix.

*snip*

Applying these three techniques should speed up your code quite a bit. If
you can only do one, pulling the concatenation out of the loop is probably
the biggest bang-for-buck.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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