Matlab到Python的转换

2024-06-16 10:44:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一些代码从Matlab转换成Python,但我对大量的Matlab语法和功能不熟悉。我已经成功地使用PIL和numpython包进行了一些转换,但是我希望有人能够解释这段代码的某些元素是怎么回事。在

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函数:

^{pr2}$

如果有人能帮忙,我们将不胜感激。在

提前谢谢


抱歉:下面是对代码的简要说明:

该代码读取光场的图像,根据对全光掩模的先验知识,我们存储了相关的毫微光、掩模的基频和相移,这些都可以用来找到图像的多个光谱副本。在

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

然后对所有光谱副本进行傅里叶逆变换,得到一个光场。在

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中的数据在哪里?(:,:,i,j)是什么意思?它是多维数组吗?在

大小函数返回什么:

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

简单地解释一下如何将其转换为python将是一个很大的帮助。在

感谢大家:)


Tags: oftheinimagefftforendlight
2条回答

从这个页面开始怎么样http://www.scipy.org/NumPy_for_Matlab_Users?另外,如果你有一个简短的描述,这将是很好的

你说得对:FFT_LF(:,:,i,j)是指多维数组。在本例中,FFT_LF是一个4维数组,但计算结果是一个二维数组。(:,:,i,j)告诉MATLAB如何将二维结果放入4d变量中。在

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

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

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

如果你有可用的MATLAB,输入“help size”可以很好地解释它的作用。对于大多数MATLAB函数也可以这样说:我对它们的文档一直印象深刻。在

希望有帮助

相关问题 更多 >