Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zilotvn

macrumors newbie
Original poster
Dec 5, 2013
3
0
I have done the Matlab code like this:

Code:
k=double(imread('testHologram.bmp'))/255*2*pi;

Mtheta=3/4*pi;

Mphi=pi/6000;
wl = 522e-9;

[x,y]=meshgrid(-960*p:p:959*p,540*p:-p:-539*p);

xm=x*cos(Mtheta)+y*sin(Mtheta);

ym=-x*sin(Mtheta)+y*cos(Mtheta);

wave=exp(j*2*pi/wl*sin(Mphi)*xm);

khologram = exp(j*k).*wave;

phase1=angle(khologram)+pi;

% Hologram

kkhologram=phase1/pi/2;

imshow(kkhologram,'Parent',handles.axes2);

And also did some work in Java, but the pattern is different from each other.

I will be very appreciate if you guys can help

Best regards
 
Post the Java code you have so far.

Any reason you don't just use Octave if you don't have access to Matlab?

B
 
I am Java newbie so, my code looks so messy :)

Code:
public double[][] compute(File file) {
        
        try {
            BufferedImage img = ImageIO.read(file);
            
            Raster raster = img.getData();
            int w = raster.getWidth(), h = raster.getHeight();
            
            double pixels[][] = new double[w][h];
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    pixels[x][y] = raster.getSample(x, y, 0);
                }
            }

            return pixels;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
Code:
    private int phase2gray(double phase) {
        int scale = gray2phase.length;
        phase = phase / 2.0d / Math.PI;
        phase -= Math.floor(phase);
        int gray = Math.min((int) Math.round(phase * scale), scale - 1);
        return gray2phase[gray];
    }
Code:
public void paint() {
        double[][] iArray2;
        File file = new File("./src/resources/diagram/testHologram.bmp");
        
        
        iArray2 = compute(file); 
        
        WritableRaster raster = canvas.getRaster();
        double[] iArray = new double[1];
        double phase, x, y;
        
        double phy = Math.PI/6000;
        double theta = Math.PI*0.75;
        double lambda1 = 522e-9;
        
        double xm = Math.sin(phy) * Math.cos(theta);
        double ym = Math.sin(phy) * Math.sin(theta);
        double fixpart = 2.0 * Math.PI / lambda1;
        
        int w = raster.getWidth(), h = raster.getHeight();
        

        double temp;
        phase = 0;
        for (int i = 0; i < iArray2.length; i++) {
            x = (double) (i - iArray2.length / 2 + 1) * pxsize;
            x = xm * x;
            for (int j = 0; j < iArray2[0].length; j++) {
                y = (double) (j - iArray2[0].length / 2 + 1) * pxsize;
                y = ym * y;
                for(int k = 0; k < iArray2.length; k++) {
                    temp = ((double)iArray2[k][j]/255)*2*Math.PI;
                    phase += Math.getExponent(fixpart * (x + y))*temp;
                    
                }
                
                iArray[0] = phase2gray(phase);
                
                raster.setPixel(j, i, iArray);
                phase = 0;
            }
        }
        
    }
 
I attached the result from Matlab:

The area above is testHologram.bmp, and the area below is after using the algorithm
 

Attachments

  • Result_matlab.jpg
    Result_matlab.jpg
    264.8 KB · Views: 218
one obvious difference

I see one obvious difference that may explain your problem.

In your Matlab code you have:

Code:
xm=x*cos(Mtheta)+y*sin(Mtheta);
ym=-x*sin(Mtheta)+y*cos(Mtheta);

Whereas in your Java code you have:

Code:
double xm = Math.sin(phy) * Math.cos(theta);
double ym = Math.sin(phy) * Math.sin(theta);

Notice that the Matlab code doesn't depend on phi, it uses only the theta variable.

HTH
 
I see one obvious difference that may explain your problem.

In your Matlab code you have:

Code:
xm=x*cos(Mtheta)+y*sin(Mtheta);
ym=-x*sin(Mtheta)+y*cos(Mtheta);

Whereas in your Java code you have:

Code:
double xm = Math.sin(phy) * Math.cos(theta);
double ym = Math.sin(phy) * Math.sin(theta);

Notice that the Matlab code doesn't depend on phi, it uses only the theta variable.

HTH

I'm noticing those lines you pointed out aren't even close to the same. The fact that sin, cos, theta, and multiplication are used are all preserved, along with the resultant variable names of xm and ym, but the java code lacks the addition, the use of x and y, and adds in phy.
 
please help me convert matlab coding into java

main.m
Code:
clc;
clear all;
close all;

warning off all

H=(imresize(imread('peppers.png'),[511 511]));
figure,imshow(H),title('Input Image');
[m n d]=size(H);
if d==3
    H=rgb2gray(H);
end


figure,imshow(H),title('Cover Image')
H1=H;
[rows, cols]=size(H);



H=double(H);
D=zeros(rows,cols);

for i=1:rows
    for j=1:cols
        iso = isodd(j);
        if iso==1
        if j==1
            D(i,j)=fix(H(i,j)-H(i,j+1));
        elseif j==cols
            D(i,j)=fix(H(i,j)-H(i,j-1));
        else
            D(i,j)=fix(H(i,j)-((H(i,j-1)+H(i,j+1))/2));
        end
        else
        D(i,j)=H(i,j);
        end
        
    end
end

figure,
[n,xout] = hist(D);
bar(xout,n,'r','LineWidth',2),title('Histogram Of Predictive Error')



Empty_M=zeros(rows,fix(cols/2)+1);

m=1;

[R C]=size(Empty_M);

for x=1:C   
            
Empty_M(:,x)=D(:,m);
              m=m+2;
              if m>R
                  m=1;
              end    
        
   
end

figure,
[n,xout] = hist(Empty_M);
bar(xout,n,'r','LineWidth',2),title(' Odd Columns Histogram Of Predictive Error')
isodd.m
Code:
function iso = isodd(x)
%ISODD True for odd numbers.
%   ISODD(X) is 1 for the elements of X that are odd and 0 for even elements.
%
%   Class support for input X:
%      float: double, single
%      integer: [u]int8, [u]int16, [u]int32, [u]int64
%
%   An error is raised if X is a float and is:
%   1) too large or small to determine if it is odd or even (this includes -Inf
%      and +Inf), or
%   2) contains a fraction (e.g. isodd(1.5) raises an error), or
%   3) NaN (not-a-number).
%
%   See also ISEVEN.

% By Ulf Carlberg 29-MAY-2011.

if isa(x, 'float') && isreal(x)
    ax = abs(double(x));
    if isa(x, 'single') && any(ax(: )>=2^24)
        % Also x=Inf or x=-Inf is will be taken care of here.
        error('isodd:InputOutOfRange', ...
            'The maximum value of abs(X) allowed is 2^24-1 for singles.');
    end
    if any(ax(: )>=2^53)
        % Also x=Inf or x=-Inf is will be taken care of here.
        error('isodd:InputOutOfRange', ...
            'The maximum value of abs(X) allowed is 2^53-1 for doubles.');
    end
    if any(isnan(x(: )))
        error('isodd:InputNaN', 'No entries of X are allowed to be NaN.');
    end
    if any(floor(ax(: ))~=ax(: ))
        error('isodd:InputNotInt', 'All entries of X must be integers.');
    end
    iso = logical(bitget(ax, 1));
elseif isa(x, 'integer')
    if isa(x, 'uint64')
        % MOD can not handle 64 bit numbers on some Matlab versions, but BITGET
        % works on unsigned 64-bit integers.
        iso = logical(bitget(x, 1));
    elseif isa(x, 'int64')
        % MOD can not handle 64 bit numbers on some Matlab versions, but ABS
        % works, and BITGET works on unsigned 64-bit integers.
        ax = uint64(abs(x));
        ax(x==-2^63) = 0;  % "the weird number" is even
        iso = logical(bitget(ax, 1));
    else
        iso = logical(mod(x, 2));
    end
else
    error('isodd:UnsupportedClass', 'Unsupported class.');
end
.
 
Last edited by a moderator:
Thank you for your unformatted code dump - do you have a question?

What language is that even? If it's MatLab, it's been too long since I last used it because I can't even understand it... it certainly doesn't look like Java.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.