I have 200 images and I am trying to calculate Signal to Noise ratio at each pixel following the formula
SNR at each pixel = mean(pixel (x,y) value from 200 image)/std(pixel (x,y) value from 200 image).
It takes ages to process the images using the code bellow even on super powerful computer. Any ideas as to how to speed up the code are very much appreciated.
% Reading multiple image files test1_0001.edf to test1_0200.edf in a loop
for i=1:200 if i<100 filename=strcat('test1_00',num2str(i),'.edf'); [header matrix]=pmedf_read(filename); variablename=strcat('test1_00',num2str(i)); eval([variablename '=matrix;']); else filename=strcat('test1_0',num2str(i),'.edf'); [header matrix]=pmedf_read(filename); variablename=strcat('test1_0',num2str(i)); eval([variablename '=matrix;']); end end
% Getting mean count per pixel from all images x=2500; y=1500; for r=1:x % rows for c=1:y % columns for i=1:200 if i<100 pixel_image(i)=eval(strcat('test1_00',num2str(i),'(r,c)')); else signal_value(i)=eval(strcat('test1_0',num2str(i),'(r,c)')); end signal_pixel(r,c)=sum(signal_value(:))/200; std_pixel(r,c)=std(signal_value(:)); snr_pixel(r,c)=signal_pixel(r,c)/std_pixel(r,c);
end end end
No products are associated with this question.
You probably want to do something like the following (to adapt to your case, and check that it is working):
n_img = 200 ; dim_x = 2500 ; dim_y = 1500 ;
buffer = zeros(dim_x, dim_y, n_img) ;
for k = 1 : n_img filename = sprintf('test1_%03d.edf', k) ; [header, matrix] = pmedf_read(filename) ; buffer(:,:,k) = matrix ; end
signal = mean(buffer, 3) ; noise = std(buffer, , 3) ; snr = signal ./ noise ;
What we do here is to store each image as a "page" in a 3D array. We then operate along the 3rd dimension to compute the mean and std.
If you want to experiment to understand the mechanism on a simpler case, you can proceed as follows: generate two fake 3x4 "images" with random pixel values and compute mean, std, etc along the 3rd dim:
>> buffer = randi(10, [3,4,2]) buffer(:,:,1) = % Page/image 1. 8 8 10 9 7 7 7 4 7 10 9 2 buffer(:,:,2) = % Page/image 2 8 7 2 2 3 2 3 9 6 3 9 3
>> mean(buffer, 3) ans = 8.0000 7.5000 6.0000 5.5000 5.0000 4.5000 5.0000 6.5000 6.5000 6.5000 9.0000 2.5000
>> std(buffer, , 3) ans = 0 0.7071 5.6569 4.9497 2.8284 3.5355 2.8284 3.5355 0.7071 4.9497 0 0.7071
The eval() are slowing you down.