scipy Matlab到Python的转换

sdnqo3pr  于 2022-12-18  发布在  Matlab
关注(0)|答案(2)|浏览(194)

我正在尝试将一些代码从Matlab转换为Python,但我对Matlab的语法和功能相当不熟悉。我已经设法使用PIL和Numpy python包完成了一些转换,但我希望有人能够解释一下此代码的一些元素是怎么回事。

  1. clear all;close all;clc;
  2. % Set gray scale to 0 for color images. Will need more memory
  3. GRAY_SCALE = 1
  4. % The physical mask placed close to the sensor has 4 harmonics, therefore
  5. % we will have 9 angular samples in the light field
  6. nAngles = 9;
  7. cAngles = (nAngles+1)/2;
  8. % The fundamental frequency of the cosine in the mask in pixels
  9. F1Y = 238; F1X = 191; %Cosine Frequency in Pixels from Calibration Image
  10. F12X = floor(F1X/2);
  11. F12Y = floor(F1Y/2);
  12. %PhaseShift due to Mask In-Plane Translation wrt Sensor
  13. phi1 = 300; phi2 = 150;
  14. %read 2D image
  15. disp('Reading Input Image...');
  16. I = double(imread('InputCones.png'));
  17. if(GRAY_SCALE)
  18. %take green channel only
  19. I = I(:,:,2);
  20. end
  21. %make image odd size
  22. I = I(1:end,1:end-1,:);
  23. %find size of image
  24. [m,n,CH] = size(I);
  25. %Compute Spectral Tile Centers, Peak Strengths and Phase
  26. for i = 1:nAngles
  27. for j = 1:nAngles
  28. CentY(i,j) = (m+1)/2 + (i-cAngles)*F1Y;
  29. CentX(i,j) = (n+1)/2 + (j-cAngles)*F1X;
  30. %Mat(i,j) = exp(-sqrt(-1)*((phi1*pi/180)*(i-cAngles) + (phi2*pi/180)*(j-cAngles)));
  31. end
  32. end
  33. Mat = ones(nAngles,nAngles);
  34. % 20 is because we cannot have negative values in the mask. So strenght of
  35. % DC component is 20 times that of harmonics
  36. Mat(cAngles,cAngles) = Mat(cAngles,cAngles) * 20;
  37. % Beginning of 4D light field computation
  38. % do for all color channel
  39. for ch = 1:CH
  40. disp('=================================');
  41. disp(sprintf('Processing channel %d',ch));
  42. % Find FFT of image
  43. disp('Computing FFT of 2D image');
  44. f = fftshift(fft2(I(:,:,ch)));
  45. %If you want to visaulize the FFT of input 2D image (Figure 8 of
  46. %paper), uncomment the next 2 lines
  47. % figure;imshow(log10(abs(f)),[]);colormap gray;
  48. % title('2D FFT of captured image (Figure 8 of paper). Note the spectral replicas');
  49. %Rearrange Tiles of 2D FFT into 4D Planes to obtain FFT of 4D Light-Field
  50. disp('Rearranging 2D FFT into 4D');
  51. for i = 1: nAngles
  52. for j = 1: nAngles
  53. FFT_LF(:,:,i,j) = f( CentY(i,j)-F12Y:CentY(i,j)+F12Y, CentX(i,j)-F12X:CentX(i,j)+F12X)/Mat(i,j);
  54. end
  55. end
  56. clear f
  57. k = sqrt(-1);
  58. for i = 1:nAngles
  59. for j = 1:nAngles
  60. shift = (phi1*pi/180)*(i-cAngles) + (phi2*pi/180)*(j-cAngles);
  61. FFT_LF(:,:,i,j) = FFT_LF(:,:,i,j)*exp(k*shift);
  62. end
  63. end
  64. disp('Computing inverse 4D FFT');
  65. LF = ifftn(ifftshift(FFT_LF)); %Compute Light-Field by 4D Inverse FFT
  66. clear FFT_LF
  67. if(ch==1)
  68. LF_R = LF;
  69. elseif(ch==2)
  70. LF_G = LF;
  71. elseif(ch==3)
  72. LF_B = LF;
  73. end
  74. clear LF
  75. end
  76. clear I
  77. %Now we have 4D light fiel
  78. disp('Light Field Computed. Done...');
  79. disp('==========================================');
  80. % Digital Refocusing Code
  81. % Take a 2D slice of 4D light field
  82. % For refocusing, we only need the FFT of light field, not the light field
  83. disp('Synthesizing Refocused Images by taking 2D slice of 4D Light Field');
  84. if(GRAY_SCALE)
  85. FFT_LF_R = fftshift(fftn(LF_R));
  86. clear LF_R
  87. else
  88. FFT_LF_R = fftshift(fftn(LF_R));
  89. clear LF_R
  90. FFT_LF_G = fftshift(fftn(LF_G));
  91. clear LF_G
  92. FFT_LF_B = fftshift(fftn(LF_B));
  93. clear LF_B
  94. end
  95. % height and width of refocused image
  96. H = size(FFT_LF_R,1);
  97. W = size(FFT_LF_R,2);
  98. count = 0;
  99. for theta = -14:14
  100. count = count + 1;
  101. disp('===============================================');
  102. disp(sprintf('Calculating New ReFocused Image: theta = %d',theta));
  103. if(GRAY_SCALE)
  104. RefocusedImage = Refocus2D(FFT_LF_R,[theta,theta]);
  105. else
  106. RefocusedImage = zeros(H,W,3);
  107. RefocusedImage(:,:,1) = Refocus2D(FFT_LF_R,[theta,theta]);
  108. RefocusedImage(:,:,2) = Refocus2D(FFT_LF_G,[theta,theta]);
  109. RefocusedImage(:,:,3) = Refocus2D(FFT_LF_B,[theta,theta]);
  110. end
  111. str = sprintf('RefocusedImage%03d.png',count);
  112. %Scale RefocusedImage in [0,255]
  113. RefocusedImage = RefocusedImage - min(RefocusedImage(:));
  114. RefocusedImage = 255*RefocusedImage/max(RefocusedImage(:));
  115. %write as png image
  116. clear tt
  117. for ii = 1:CH
  118. tt(:,:,ii) = fliplr(RefocusedImage(:,:,ii)');
  119. end
  120. imwrite(uint8(tt),str);
  121. disp(sprintf('Refocused image written as %s',str));
  122. end

下面是重聚焦2d函数:

  1. function IOut = Refocus2D(FFTLF,theta)
  2. [m,n,p,q] = size(FFTLF);
  3. Theta1 = theta(1);
  4. Theta2 = theta(2);
  5. cTem = floor(size(FFTLF)/2) + 1;
  6. % find the coordinates of 2D slice
  7. [XX,YY] = meshgrid(1:n,1:m);
  8. cc = (XX - cTem(2))/size(FFTLF,2);
  9. cc = Theta2*cc + cTem(4);
  10. dd = (YY - cTem(1))/size(FFTLF,1);
  11. dd = Theta1*dd + cTem(3);
  12. % Resample 4D light field along the 2D slice
  13. v = interpn(FFTLF,YY,XX,dd,cc,'cubic');
  14. %set nan values to zero
  15. idx = find(isnan(v)==1);
  16. disp(sprintf('Number of Nans in sampling = %d',size(idx,1)))
  17. v(isnan(v)) = 0;
  18. % take inverse 2D FFT to get the image
  19. IOut = real(ifft2(ifftshift(v)));

如果有人能帮忙,我将不胜感激。
先谢了
抱歉:下面是代码的简要描述:
该代码读取光场的图像,并且利用全光掩模的先验知识,我们存储掩模的相关nAngle和基频以及相移,这些用于找到图像的多个光谱副本。
读入图像并提取绿色通道后,我们对图像执行快速傅立叶变换,并开始从图像矩阵中获取代表光谱副本之一的切片。
然后,我们对所有光谱副本进行傅立叶逆变换,以产生光场。
然后,重新聚焦2d功能获取4d数据的2维切片,以重新创建重新聚焦的图像。
我特别纠结的事情是:

  1. FFT_LF(:,:,i,j) = f( CentY(i,j)-F12Y:CentY(i,j)+F12Y, CentX(i,j)-F12X:CentX(i,j)+F12X)/Mat(i,j);


我们从矩阵f中取一个切片,但是FFT_LF中的数据在哪里?(:,:,i,j)是什么意思?它是多维数组吗?
那么size函数返回什么

  1. [m,n,p,q] = size(FFTLF);

只要简单解释一下如何将其翻译成python就会有很大的帮助。
到目前为止感谢大家:)

goqiplq2

goqiplq21#

从这个页面http://www.scipy.org/NumPy_for_Matlab_Users开始怎么样?另外,如果你有这个页面应该做什么的简要描述,那就太好了

ipakzgxi

ipakzgxi2#

你说得对:FFT_LF(:,:,i,j)是指多维数组。在本例中,FFT_LF是4-D数组,但计算结果是2-D数组。(:,:,i,j)告诉MATLAB如何将2-D结果放入4-D变量中。
实际上,它为每对索引(i,j)存储一个MxN数组,冒号(:)实际上意味着“获取该维度中的每个元素”。
[m,n,p,q] = size(FFTLF)将返回数组中每一维的长度,因此,如果FFTLF最终是一个5x 5x 3x 2数组,则得到:

  1. m=5, n=5, p=3, q=2.

如果你有MATLAB,输入“help size”就能很好地解释它的作用,大多数MATLAB函数也是如此:我一直对他们的文档印象深刻。
希望有帮助

相关问题