I have a few questions. Bear with me please. I wrote down a program and am trying to optimize it. I am posting only the code snippets that I believe that can be further optimized.
1. First of all I am trying to calculate the gradient of an image. I previously used the gradient operator of the MATLAB but found it to be slow. So I wrote down my own code. Please find my code at my gradient code here!
I ran the profiler on MATLAB and found that my program still had a few bottlenecks. I am posting an image here :
As you can see I am using two extra matrices A and B two times each for the calculation of the gradient in the horizontal & vertical direction. Is it really necessary ? Can't the operation be integrated in line 5 & line 8 without the use of extra matrices A & B. Will the removal of the extra matrices A & B make my code run faster ?
2. Secondly I needed to use the Laplacian operator in my code and found it to be one of the bottlenecks of my program. I have already read the matlab documentation & also read the links Discrepancy between Matlab del2 and Matlab gradient of gradient and Understanding DEL2 function in Matlab in order to code it in C++.
I then checked the profile of del2 and found this. . My question is this what are the functions of the children function parse_inputs and ipermute in this case ? It is to be noted that I am working with an image matrix whose size will always be greater than 64X64.
I read the explanation for permute and it said that "Permute so that the del2 is always taken along the columns.". What does it mean actually ? I read somewhere that MATLAB uses column major format . Could anyone please explain what it means ?
Regarding the parse_inputs sub-function I read the explanation where it stated that
%PARSE_INPUTS % [ERR,F,LOC,CFLAG] = PARSE_INPUTS(F,V) returns the spacing LOC % along the x,y,z,... directions and a column vector flag CFLAG. ERR % will be true if there is an error.
I have no idea what this function does. Please can anyone explain ?
3. I also tried to write down my own del2 function for matlab so as to make it faster but strangely I could only cope with the interior points. Is the implementation in MATLAB correct for del2 ? Why does it do extrapolation at the boundary points ?
Should not the basis of the Laplacian operator be always that it should hold the equality ?
laplacian = del2(image); [x, y] = gradient(image); [xx, xy] = gradient(x); [yx, yy] = gradient(y); laplacian = xx + yy;
This code was taken from Please check code. I read all the posts there but the explanation did not satisfy me. Please could you explain why the above code does not give the same output ?
Thanks everyone for reading this long and boring set of questions. Any help will be greatly appreciated. If anyone has a problem I will split the questions and post it separately. Just say so guys!
Thanks in advance !
EDIT : After slogging through the whole day finally managed to remove the offending matrices A & B. I am writing down the code below :
function [dx dy] = gradient_my_code(I) dx=[I(:,2)-I(:,1) (I(:,3:end)-I(:,1:end-2))./2 I(:,end)-I(:,end-1)]; dy=[I(2,:)-I(1,:) ; (I(3:end,:)-I(1:end-2,:))./2 ; I(end,:)-I(end-1,:)];
For timing purposes I did this :
xaxis = [10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000,4000,5000]; n1 = zeros(1,length(xaxis)); n2 = zeros(1,length(xaxis)); for i=1:length(xaxis); x=xaxis(i); u=magic(x); tic [dx dy] = gradient(u); n1(i)=toc; tic [dx dy] = gradient_my_code(u); n2(i)=toc; end plot(n1,'r','LineWidth',3); hold on; plot(n2,'b','LineWidth',3); hold off;
And I plotted the speed comparison . Please check and guys give your comments.