2

I've got a function in Matlab I want to call on a greyscale image I have loaded in Python as a 2d ndarray. I have opted to use the Matlab Engine for Python for this.

Following this answer I opted to use Scipy's savemat() function, then I packed my ndarray into a dictionary as shown in this answer. Then on the Matlab side I read this file using fopen().

Somewhere in this process, the first 50 cells of the first row of the image get corrupted.

Python code:

self.eng = matlab.engine.start_matlab()
savemat(out_file_python, {'image_data': image})
FILTERED_IMAGE = self.eng.SARBM3D_Python_Helper(out_file_matlab, L, width, height)

Matlab code:

    function IMG_FILTERED = SARBM3D_Python_Helper(path, L, width, height)
    %SARBM3D_filter Takes an image path, loads the image and applies
    %    SARBM3D filter.
    %   Detailed explanation goes here
    [fp, msg] = fopen(path,'rb'); 
    assert(fp>=3,msg)
    IMG = fread(fp,[width height],'float')'; 
    fclose(fp);

    tic;
    IMG_FILTERED = SARBM3D_v10(IMG,L);
    toc,

    end

This works, the image is saved by Python, then loaded by Matlab. The problem is that the first 50 cells of the first row of the image read by Matlab is full of garbage numbers:

5.97010568981204e-07    167969504   2.04630902945563e+23    1.94329244039050e-19    7.14394306433430e+31    4.68937780150775e+27    7.54651052656269e+31    3.86486210083297e+30    7.21283771132713e+22    1.85015188025444e+28    6.34833865368594e+28    2.39079247928242e+29    6.48021303284452e-10    0.000698821619153023    1.73404984593617e-07    6.48018139148832e-10    1.72892204528396e-41    0   0   0   0   0   0   0   0   0   0   0   0   0   210767872   1.96181785005474e-44    4.65006882401547e-40    8.40779078594890e-45    1.12103877145985e-44    9.80908925027372e-45    0   7.00649232162409e-45    1.12103877145985e-44    4.03573957725547e-43    4.03573957725547e-43    1.40129846432482e-45    1.40129846432482e-44    1.06455071979708e+24    2.63295721825752e+20    3.49595940879755e-41    0   9.80908925027372e-45    4.64917199299831e-40

Then after these 50 garbage entries the image data is normal.

My theory is that somewhere a problem is happening between serialising and deserialising and some metadata is being read as part of the image. Do I need to open the 'image_data' dictionary within Matlab?

OscarVanL
  • 475
  • 4
  • 13
  • 1
    Does `load(path)` not work instead of `fopen` since you already save it as `.mat`. I am guessing it has something to do with file header and saving format. – Ehsan Apr 20 '20 at 17:09

1 Answers1

1

Try loading your file as mat object into Matlab instead of fopen. It will keep your original array shape as well.

fp = load(path); 
Ehsan
  • 10,716
  • 1
  • 15
  • 28
  • Yeah this was my mistake! Thanks, I was adapting some code that was reading raw images (hence the use of fopen and fread) rather than reading Matlab objects. The last part would be accessing the data itself with `fp.image_data` and then it's working as I wanted. Thanks for the sanity check! – OscarVanL Apr 20 '20 at 17:13
  • @OscarVanL You are welcome. Please go ahead and accept the answer if it solved your issue to close the question. Thank you. – Ehsan Apr 20 '20 at 17:14
  • 1
    Sure, gotta wait a few minutes first and then I will. – OscarVanL Apr 20 '20 at 17:15