To make faster the implemented algorithm
    7 views (last 30 days)
  
       Show older comments
    
Dear Experts,
I implemented a small algorithm which is very slow and take many hours to generate results. 
Although algorithm's output is perfect, can you please guide me to make algorithm faster using filter2 or conv2?
I need to use faster function for these two loops given  in the script attached. 
Many thanks. 
function EB = EB_Fast2D(Image, F)
k = 64; % size of patch/window
% F = 100;
n = length(Image); % image is 2D data
F = (-0.5:1/n:0.5-1/n)*F;
dc = zeros(size(Image));
for j= 1: size(Image,2) 
    for i = k+1:size(Image,1)-k
        A = Image(:,j);
        A1 = zeros(size(Image,1),1);
        A1(i-k : i+k) = A(i-k:i+k); % 
        sig_fft = fftshift(fft(A1, size(A1,1)));
        sum_Ap = cumsum(abs(sig_fft).^2);
        sum_Am = wrev(cumsum(wrev(abs(sig_fft).^2)));
        [val,idx] = min(abs(sum_Ap - sum_Am));
        dc(i,j) = F(idx);
    end
end  
EB = (0.5/F).* dc; 
end
2 Comments
  Image Analyst
      
      
 on 22 Dec 2021
				n will be the longest lateral dimension of the image you pass in.  So rows or columns, whichever is longer.  What is the point of that?
How big is your image?  As you can see from below, with a 256x256 image it takes only 1.2 seconds, not hours.  But the final is all  zeros.  Not exactly sure what you're doing because you forgot to put in any comments and it doesn't look like the algorithm makes sense to me.
And this is supposed to handle only gray scale images, right?  Not color images.  By the way (for others) you will need the Wavelet Toolbox to run this.
Image = imread('cameraman.tif');
F = 20;
tic
%function EB = EB_Fast2D(Image, F)
k = 64; % size of patch/window
% F = 100;
n = length(Image) % image is 2D data
F = (-0.5:1/n:0.5-1/n)*F;
dc = zeros(size(Image));
for j= 1: size(Image,2) 
    for i = k+1:size(Image,1)-k
        A = Image(:,j);
        A1 = zeros(size(Image,1),1);
        A1(i-k : i+k) = A(i-k:i+k); % 
        sig_fft = fftshift(fft(A1, size(A1,1)));
        sum_Ap = cumsum(abs(sig_fft).^2);
        sum_Am = wrev(cumsum(wrev(abs(sig_fft).^2)));
        [val,idx] = min(abs(sum_Ap - sum_Am));
        dc(i,j) = F(idx);
    end
end  
EB = (0.5 ./ F).* dc; % Need ./ not /
fprintf('Min EB = %f, Max EB = %f\n', min(EB(:)), max(EB(:)))
elapsedSeconds = toc
imshow(EB, [])
Answers (0)
See Also
Categories
				Find more on Continuous Wavelet Transforms in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!