【问题标题】:Matlab to Python ConversionMatlab 到 Python 的转换
【发布时间】:2012-02-01 03:23:20
【问题描述】:

我正在尝试将一些代码从 Matlab 转换为 Python,但我不熟悉大量的 Matlab 语法和功能。我已经设法使用 PIL 和 Numpy python 包进行了一些转换,但我希望有人能够解释这段代码的某些元素发生了什么。

clear all;close all;clc;

% Set gray scale to 0 for color images. Will need more memory
GRAY_SCALE = 1


% The physical mask placed close to the sensor has 4 harmonics, therefore
% we will have 9 angular samples in the light field
nAngles = 9;
cAngles = (nAngles+1)/2;


% The fundamental frequency of the cosine in the mask in pixels
F1Y = 238;  F1X = 191;  %Cosine Frequency in Pixels from Calibration Image
F12X = floor(F1X/2);
F12Y = floor(F1Y/2);

%PhaseShift due to Mask In-Plane Translation wrt Sensor
phi1 = 300;  phi2 = 150;


%read 2D image
disp('Reading Input Image...');
I = double(imread('InputCones.png'));

if(GRAY_SCALE)
   %take green channel only
   I = I(:,:,2);
end


%make image odd size
I = I(1:end,1:end-1,:);

%find size of image
[m,n,CH] = size(I);


%Compute Spectral Tile Centers, Peak Strengths and Phase
for i = 1:nAngles
    for j = 1:nAngles
        CentY(i,j) = (m+1)/2 + (i-cAngles)*F1Y;
        CentX(i,j) = (n+1)/2 + (j-cAngles)*F1X;
        %Mat(i,j) = exp(-sqrt(-1)*((phi1*pi/180)*(i-cAngles) + (phi2*pi/180)*(j-cAngles)));
    end
end

Mat = ones(nAngles,nAngles);
% 20 is because we cannot have negative values in the mask. So strenght of
% DC component is 20 times that of harmonics
Mat(cAngles,cAngles) = Mat(cAngles,cAngles) * 20;


% Beginning of 4D light field computation
% do for all color channel

for ch = 1:CH

    disp('=================================');
    disp(sprintf('Processing channel %d',ch));

    % Find FFT of image
    disp('Computing FFT of 2D image');
    f = fftshift(fft2(I(:,:,ch)));


    %If you want to visaulize the FFT of input 2D image (Figure 8 of
    %paper), uncomment the next 2 lines
    %     figure;imshow(log10(abs(f)),[]);colormap gray;
    %     title('2D FFT of captured image (Figure 8 of paper). Note the spectral     replicas');


    %Rearrange Tiles of 2D FFT into 4D Planes to obtain FFT of 4D Light-Field
    disp('Rearranging 2D FFT into 4D');
    for i = 1: nAngles
        for j = 1: nAngles
            FFT_LF(:,:,i,j) =  f( CentY(i,j)-F12Y:CentY(i,j)+F12Y, CentX(i,j)-F12X:CentX(i,j)+F12X)/Mat(i,j);
        end
    end
    clear f

    k = sqrt(-1);
    for i = 1:nAngles
        for j = 1:nAngles
            shift = (phi1*pi/180)*(i-cAngles) + (phi2*pi/180)*(j-cAngles);
            FFT_LF(:,:,i,j) = FFT_LF(:,:,i,j)*exp(k*shift);
        end
    end



    disp('Computing inverse 4D FFT');
    LF     =    ifftn(ifftshift(FFT_LF)); %Compute Light-Field by 4D Inverse FFT
    clear FFT_LF

    if(ch==1)
        LF_R = LF;
    elseif(ch==2)
        LF_G = LF;
    elseif(ch==3)
        LF_B = LF;
    end
    clear LF

end
clear I

%Now we have 4D light fiel

disp('Light Field Computed. Done...');
disp('==========================================');




% Digital Refocusing Code
% Take a 2D slice of 4D light field
% For refocusing, we only need the FFT of light field, not the light field

disp('Synthesizing Refocused Images by taking 2D slice of 4D Light Field');

if(GRAY_SCALE)
    FFT_LF_R = fftshift(fftn(LF_R));
    clear LF_R
else
    FFT_LF_R = fftshift(fftn(LF_R));
    clear LF_R

    FFT_LF_G = fftshift(fftn(LF_G));
    clear LF_G

    FFT_LF_B = fftshift(fftn(LF_B));
    clear LF_B
end


% height and width of refocused image
H = size(FFT_LF_R,1);
W = size(FFT_LF_R,2);


count = 0;
for theta = -14:14

    count = count + 1;

    disp('===============================================');
    disp(sprintf('Calculating New ReFocused Image: theta = %d',theta));

    if(GRAY_SCALE)
        RefocusedImage = Refocus2D(FFT_LF_R,[theta,theta]);
    else
        RefocusedImage = zeros(H,W,3);
        RefocusedImage(:,:,1) = Refocus2D(FFT_LF_R,[theta,theta]);
        RefocusedImage(:,:,2) = Refocus2D(FFT_LF_G,[theta,theta]);
        RefocusedImage(:,:,3) = Refocus2D(FFT_LF_B,[theta,theta]);
    end

    str = sprintf('RefocusedImage%03d.png',count);

    %Scale RefocusedImage in [0,255]
    RefocusedImage = RefocusedImage - min(RefocusedImage(:));
    RefocusedImage = 255*RefocusedImage/max(RefocusedImage(:));

    %write as png image
    clear tt
    for ii = 1:CH
        tt(:,:,ii) = fliplr(RefocusedImage(:,:,ii)');
    end
    imwrite(uint8(tt),str);

    disp(sprintf('Refocused image written as %s',str));

end

这里是 Refocus2d 函数:

function IOut = Refocus2D(FFTLF,theta)


[m,n,p,q] = size(FFTLF);
Theta1 = theta(1);
Theta2 = theta(2);

cTem = floor(size(FFTLF)/2)  +   1;


% find the coordinates of 2D slice
[XX,YY] = meshgrid(1:n,1:m);
cc = (XX - cTem(2))/size(FFTLF,2);
cc = Theta2*cc + cTem(4);
dd = (YY - cTem(1))/size(FFTLF,1);
dd = Theta1*dd + cTem(3);

% Resample 4D light field along the 2D slice
v = interpn(FFTLF,YY,XX,dd,cc,'cubic');

%set nan values to zero
idx = find(isnan(v)==1);
disp(sprintf('Number of Nans in sampling = %d',size(idx,1)))
v(isnan(v)) =   0;

% take inverse 2D FFT to get the image
IOut = real(ifft2(ifftshift(v)));

如果有人能提供帮助,将不胜感激。

提前致谢


道歉:以下是代码作用的简要说明:

代码读取光场的图像,并通过对全光掩模的先验知识,我们存储相关的 nAngles 和掩模的基频和相移,这些用于查找光场的多个光谱副本图片。

读入图像并提取绿色通道后,我们对图像执行快速傅立叶变换,并开始从图像矩阵中获取代表光谱副本之一的切片。

然后我们对所有光谱副本进行傅里叶逆变换以产生光场。

Refocus2d 函数,然后采用 4d 数据的二维切片来重新创建重新聚焦的图像。

我正在努力解决的具体问题是:

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 函数返回什么:

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

简单解释一下如何将其转换为 python 会很有帮助。

到目前为止感谢大家:)

【问题讨论】:

  • 一些提示:使用 numpy.nan_to_num 将 nan 设置为零,研究使用 scipy 进行 ifft 和 iffshift,使用 numpy.interp 而不是 interpn,numpy 也有一个 meshgrid 函数,numpy.shape 或numpy.size 而不是 size() 等。仔细查看 numpy 示例列表将是一个好的开始。
  • 与其简单的贴出代码,能不能说的更具体点?它会让你更容易看到你在做什么。
  • 也许这个问题应该被“分解”成更多的问题,一个你遇到的每一步。如果您这样做,则更有可能获得有用的答案。
  • 两个数组,y坐标从CentY(i,j)-F12YCentY(i,j) + F12Y,x坐标从CentX(i,j)-F12XCentX(i,j) + F12X,然后存入FFT_LF的前二维,对应i,j in第三/第四昏暗。第二个类似于 python/numpy 中的m,n,p,q = FFTLP.shape
  • 我说两个数组,y 索引从CentY(i,j)-F12YCentY(i,j) + F12Y,x 索引从CentX(i,j)-F12XCentX(i,j) + F12X 用于对数组进行子集化,我相信就是这样。跨度>

标签: python matlab numpy translation scipy


【解决方案1】:

如何开始使用此页面http://www.scipy.org/NumPy_for_Matlab_Users?另外,如果您对它应该做什么有简短的描述,那就太好了

【讨论】:

    【解决方案2】:

    你是对的:FFT_LF(:,:,i,j) 指的是一个多维数组。在这种情况下,FFT_LF 是一个 4 维数组,但计算结果是一个 2 维数组。 (:,:,i,j) 告诉 MATLAB 如何将 2-D 结果放入 4-D 变量中。

    实际上,它为每对索引 (i,j) 存储一个 MxN 数组。冒号 (:) 实际上意味着“获取该维度中的每个元素”。

    [m,n,p,q] = size(FFTLF) 将返回数组中每个维度的长度。所以,如果 FFTLF 最终是一个 5x5x3x2 数组,你会得到:

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

    如果您有 MATLAB,输入“帮助大小”应该可以很好地解释它的作用。大多数 MATLAB 函数也是如此:我一直对它们的文档印象深刻。

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-19
      • 2021-01-15
      • 2014-01-23
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 2016-01-21
      • 2021-03-26
      相关资源
      最近更新 更多