Python Forum
How to convert matlab code to python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert matlab code to python?
#1
Hello, I am trying to convert some matlab source code to python and I am having some troubles, could anyone help me?

Here's the matlab version of the code:

% Author    : Umesh Rajashekar, Ian van der Linde

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
% Define Constants                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        

w = 1024; h = 768;                                              % W x H of Images(cropped van Hateren) 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read van Hateren File                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f1              = fopen(filename,'rb', 'ieee-be');
buf             = fread(f1,[w,h],'uint16');                     % Read 16bpp image data
display_image   = buf';                                         % Note: van Hateren images are flipped
display_image   = display_image+1;
fclose(f1);
display_image = display_image/max(display_image(:))*255;

figure(1); clf;
imagesc(display_image.^0.3);colormap gray;axis image; axis off; hold on;
And... heres my version of the converted soure code.

# Author    : Umesh Rajashekar, Ian van der Linde

            arq = open(name, 'rb')

            arq = np.fromfile(arq, np.uint16, count=-1)
            #print(arq.shape)
            #print(name)
            arq = arq.reshape((self.width, self.height)).T
            arq = np.transpose(arq)
            arq = np.add(arq, 1)
            arq = arq / np.amax(arq) * 255

            plt.imshow(arq, cmap='gray');
            plt.show()
But am I having some troubles, the image isnt the same : (
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,912 Dec-17-2022, 12:24 AM
Last Post: snippsat
  convert c code python satyanarayana 5 5,471 Sep-21-2021, 07:45 PM
Last Post: deanhystad
Photo Matlab to Python Sateesh 0 1,636 Nov-04-2020, 09:11 AM
Last Post: Sateesh
  From Matlab to Python erbab 1 1,971 Oct-27-2020, 02:16 PM
Last Post: jefsummers
  How to convert a python code to binary? rsurathu 0 1,770 Aug-02-2020, 08:09 AM
Last Post: rsurathu
  Arrays in MATLAB and PYTHON cokhuatlanyeuthuongconhetmuc 2 2,156 Jul-24-2020, 10:47 AM
Last Post: cokhuatlanyeuthuongconhetmuc
  Matlab to Python -- Parallel Computing zistambo 1 1,934 Jun-10-2020, 04:59 PM
Last Post: pyzyx3qwerty
  Python v MatLab for graphs and plots CynthiaMoore 4 2,980 Apr-22-2020, 02:13 PM
Last Post: CynthiaMoore
  python equivalent to MATLAB xcov chai0404 2 3,823 Apr-02-2020, 10:29 PM
Last Post: chai0404
  Python equivalent of Matlab code kwokmaster 1 3,400 Mar-25-2020, 10:14 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020