From: astro mmi on 27 Apr 2010 20:40 Hi everyone, This is how I am trying to inplement the dct2 function using the equation for dct. I am not able to understand why the program runs into an infinite loop when i execute the code. Pls let me know where the error is and how I can enhance the speed of the program... Thanx in advance i=imread('p_6_1.jpg'); figure,imshow(i) title('Original image') %To evaluate Discrete Cosine Transform siz=size(i); a=zeros(1,siz(1)); b=zeros(1,siz(1)); c=zeros(siz(1),siz(2)); for x=1:siz(1) for y=1:siz(2) for u=1:siz(1) for v=1:siz(2) if(u==1) a(u)=sqrt(1/siz(1)); else a(u)=sqrt(2/siz(1)); end b=a; c(u,v)=a(u)*b(v)*sum(sum(i(x,y)*cos(((2*x+1)*pi*u)/(2*siz(1)))*cos(((2*y+1)*pi*v)/(2*siz(1))))); end end end end
From: astro mmi on 27 Apr 2010 20:54 Also I see that when I reduce the size of the image, it works and i am getting the dct.........which implies my for loops are running too long...........pls let me know how this efficiency can be enhanced and how my code can be made to run faster....... Thanx in advance "astro mmi" <pyarsa_madhu(a)yahoo.co.in> wrote in message <hr805o$gmh$1(a)fred.mathworks.com>... > Hi everyone, > This is how I am trying to inplement the dct2 function using the equation for dct. I am not able to understand why the program runs into an infinite loop when i execute the code. Pls let me know where the error is and how I can enhance the speed of the program... > Thanx in advance > > > i=imread('p_6_1.jpg'); > figure,imshow(i) > title('Original image') > > %To evaluate Discrete Cosine Transform > siz=size(i); > a=zeros(1,siz(1)); > b=zeros(1,siz(1)); > c=zeros(siz(1),siz(2)); > for x=1:siz(1) > for y=1:siz(2) > for u=1:siz(1) > for v=1:siz(2) > if(u==1) > a(u)=sqrt(1/siz(1)); > else > a(u)=sqrt(2/siz(1)); > end > b=a; > c(u,v)=a(u)*b(v)*sum(sum(i(x,y)*cos(((2*x+1)*pi*u)/(2*siz(1)))*cos(((2*y+1)*pi*v)/(2*siz(1))))); > end > end > end > end
From: Roger Stafford on 28 Apr 2010 19:32 "astro mmi" <pyarsa_madhu(a)yahoo.co.in> wrote in message <hr805o$gmh$1(a)fred.mathworks.com>... > Hi everyone, > This is how I am trying to inplement the dct2 function using the equation for dct. I am not able to understand why the program runs into an infinite loop when i execute the code. Pls let me know where the error is and how I can enhance the speed of the program... > Thanx in advance > > i=imread('p_6_1.jpg'); > figure,imshow(i) > title('Original image') > > %To evaluate Discrete Cosine Transform > siz=size(i); > a=zeros(1,siz(1)); > b=zeros(1,siz(1)); > c=zeros(siz(1),siz(2)); > for x=1:siz(1) > for y=1:siz(2) > for u=1:siz(1) > for v=1:siz(2) > if(u==1) > a(u)=sqrt(1/siz(1)); > else > a(u)=sqrt(2/siz(1)); > end > b=a; > c(u,v)=a(u)*b(v)*sum(sum(i(x,y)*cos(((2*x+1)*pi*u)/(2*siz(1)))*cos(((2*y+1)*pi*v)/(2*siz(1))))); > end > end > end > end ------------ I don't think this is doing what you want at all. When you compute c(u,v) there is no summing done since the x, y, u, v are all single values at this point and not vectors. If you are doing it with nested loops, the u- and v-loops should be on the outside: for u = 1:m for v = 1:n Calculate c(u,v) here by summing with respect to x and y end end However, you really ought to be able to do all this without any for-loops using just matrix products. That would probably give the fastest results, since matrix multiplication is what matlab specializes in. Roger Stafford
From: Roger Stafford on 28 Apr 2010 23:37 I would like to point out that an efficient algorithm should take advantage of the fact that in two-dimensional DCT, as a bare minimum, the periodic cosine function needs only to be called for the m-1 values cos(pi/2*1/m), cos(pi/2*2/m), cos(pi/2*3/m), ..., cos(pi/2*(m-1)/m) and the n-1 values cos(pi/2*1/n), cos(pi/2*2/n), cos(pi/2*3/n), ..., cos(pi/2*(n-1)/n) for an m by n image, a total of m+n-2 times altogether. All other cosine values can be obtained from these from periodicity and symmetry, and it is a waste of computation time calling the 'cos' function directly for each of the argument values occurring in the DCT formula. I notice that in your code you called 'cos' a total of 2*m^2*n^2 times, which for an image of appreciable size is an immense number of times and is undoubtedly large part of the reason it was taking so long. Roger Stafford
From: astro mmi on 29 Apr 2010 11:52 Thank you Sir. I am having another query now. If I try to write the cos terms as matrix, then each time x and y is varied, there is still a dependency on u and v value which also keep varying. In the definition of DCT2 both x and u are appearing within the cosine and how canI separate them. "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hrausv$dht$1(a)fred.mathworks.com>... > I would like to point out that an efficient algorithm should take advantage of the fact that in two-dimensional DCT, as a bare minimum, the periodic cosine function needs only to be called for the m-1 values > > cos(pi/2*1/m), cos(pi/2*2/m), cos(pi/2*3/m), ..., cos(pi/2*(m-1)/m) > > and the n-1 values > > cos(pi/2*1/n), cos(pi/2*2/n), cos(pi/2*3/n), ..., cos(pi/2*(n-1)/n) > > for an m by n image, a total of m+n-2 times altogether. All other cosine values can be obtained from these from periodicity and symmetry, and it is a waste of computation time calling the 'cos' function directly for each of the argument values occurring in the DCT formula. > > I notice that in your code you called 'cos' a total of 2*m^2*n^2 times, which for an image of appreciable size is an immense number of times and is undoubtedly large part of the reason it was taking so long. > > Roger Stafford
|
Next
|
Last
Pages: 1 2 Prev: PSNR of edge and noisy edge Next: Reading a 2D array from a mat file in C++ |