manipulate a matlab file with python - python

i have a .mat file containing annotations for some images, I need to read and manipulate specified values from this file using python and am stuck I tried to use h5py it doesn't work for me. with scipy I can read the file and print the whole file but I can't get a specified value.
I captured the structure of my mat file using octave can anyone help me to get the BBox values for each ImgName and save them into a variable.
screen shot of the annotation.mat file

Problem solved using this ( it may help some beginners like me) :
import scipy.io as spio
anno=spio.loadmat('annotation_1.mat')
#BBox. X y
#print(anno['annot'][0][1][0])
listX=anno['annot'][0][1][0]
print(listX[1][1])
#ImgName
#print(anno['annot'][0][1][1])
img=anno['annot'][0][1][1]
print(img)

Related

TypeError: Object does not appear to be a file-like object

I'm trying to access a png file to add to the background of my matplotlib plot. I'm doing something like this:
fn = get_sample_data('Jupiterbackground.png', asfileobj=False)
img = read_png(fn)
but I'm receiving an error like this:
TypeError: Object does not appear to be a file-like object.
So I manually typed in the path file name to this png file to see if it would work, but it still didn't work, so I'm assuming there's something wrong with the type of file I've chosen. Or am I using a flawed method?
Please include your imports next time, I assume it is
from matplotlib._png import read_png
from matplotlib.cbook import get_sample_data
fn = get_sample_data('Jupiterbackground.png', asfileobj=False)
img = read_png(fn)
fn is a string because you used asfileobj=False (you can check this with print(fn, type(fn)) which is often a good way to find TypeErrors) and read_png expects a file-object. You can either use asfileobj=True or call open on the string you get from get_sample_data.
I can not get read_png to work though and get "OSError: read past end of file". But the method is undocumented (as far as I could google) and its module name starts with an underscore, which in python conventions means it is not part of the public API (that is: it is a function for internal use by matplotlib)
Like the other answer said, use a different function to accomplish your task.
I tried that too. I got the same type of error. But exactly the same thing can be done with:
import matplotlib.image as mpimg\
img = mpimg.imread('Jupiterbackground.png')
and this gives the image array a numpy array. Then if you want you can even change the datatype with img.astype(#dataType).
I had a similar problem and I solved it by writing the whole path of the image file I want to open, like: C\\...\\Jupiterbackground.png.
I don't know why but if I didn't do it, I saw that the program searches for the file where the 'matplotlib' installation files are.

Convert .objs to .mat in python or a with a program in linux

Can anyone suggest a way, in python or a general tool in linux maybe to convert an obj into a .mat file?
I have some code like:
import eigen_solver as es
import scipy.io as sio
# load mesh
model = 'sphere4.mat'
mat = sio.loadmat(model)
V, F = mat['V'], mat['F']
But the meshes I want to use are in .obj format.
I'm not 100% sure how easy would be to write this from scratch, but I guess should be somehow straightforward, but if there's a tool I'd like to use it.
By obj I mean https://en.wikipedia.org/wiki/Wavefront_.obj_file
By mat I mean the matlab .mat format.
You can simply use meshio to read in the obj file directly.
pip install meshio
import meshio
mesh = meshio.read("foo.obj")
# mesh.points, mesh.cells, etc.

Why the form of matrix taken from matlab change when reading them by to python

I have a big file saved from matlab with version -v7.3, when reading it by python, the shape of matrix change !! is that normal ?
For example, let's have the below matrix in MATLAB,
clear all, clcl
A = randn(10,3) + randn(10,3)*i;
save('example.mat','-v7.3'); %% The saved file is example.mat with version 7.3
above, the saved file is example.mat a matrix of size (10,3)
so, let's go to python to read that file :
import numpy as np
import h5py as h5
data_try = h5.File('example.mat', 'r')
A = np.array(data_try)
A = A.view(np.complex) #here the matrix equivalent to that one in matlab
but what i find that A in python is of size (3,10) !! and also when having matrix of three dimensions, the shape is changing !!
Is that normal that python reads the transpose of matrix coming from matlab ??!! or something wrong is happening !
However when using the other way as below:
import scipy.io as spio
Data = spio.loadmat('example.mat', squeeze_me=True)
A = Data[‘A’]
in that case, everything is really nice, but unfortunately we can not use that way for big matrices !!!
please, any solution for that issue ?
You might face a problem with different memory alignment in Matlab (column-major) and Numpy (row-major)... check e.g. this question for related discussion and a solution (reshaping in Fortran-style, which is also column-major).
SciPy's .mat interface automatically takes care of this reinterpretation, which is why you don't encounter the problem when using it.

How to export complex array from python to tiff file for IDL

I have a complex 2D numpy array and I would like to save it as a tiff file to import it into IDL.
What would be the best way to do this?
Thanks!
May be it is interesintg for you to save directly a .sav file (IDL format) from numpy using the package pIDLy (https://github.com/anthonyjsmith/pIDLy).
For me, it works like that:
#starting IDL
idl = pidly.IDL()
#save the file with the save method of idle
idl.pro('save',variable,FILENAME=name_file)
#clossing IDL session
idl.close()

matplotlib PIL reading tif - ValueError: tile cannot extend outside image

I would like to read a tif file with basically the following code:
import matplotlib.pyplot as plt
filename = 'test.tif'
plt.imread(filename)
This results in the following error message (just the last lines):
File ".../miniconda2/lib/python2.7/site-packages/PIL/Image.py", line 692, in tobytes
self.load()
File ".../miniconda2/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", line 1013, in load
return super(TiffImageFile, self).load()
File ".../miniconda2/lib/python2.7/site-packages/PIL/ImageFile.py", line 204, in load
decoder.setimage(self.im, extents)
ValueError: tile cannot extend outside image
When I open the tif image with imagemagick's display and save it without making changes, everything works normally.
Nevertheless, I think it could a problem with my python environment/version, as my colleague who's working with the same code and the same files did not have this problem.
I tried many files and it ends in the same for all of them. I am aware that, for this mini example, I could make a workaround or use e.g. gdal (which works fine). But as these lines are just a part of a larger code and are supposed to work for new files immediately, I would like to have a real solution.
I am working with openSUSE 11.4, conda 4.3.23, Python 2.7.13, matplotlib 2.0.2 .
This could be a duplicate of Value Error in reading tif image with pil in python?. Due to the lack of specific information there, I open a new question. Sorry if that's the wrong way - my first post here...
Remark: I see that my tif file gets uploaded as png here. Is there a way to change that?

Categories

Resources