libtiff error in Matlab when attempting to use ImageMagick
Trying to use the Imagemagick toolbox (for me, installed via MacPorts) sometimes doesn’t work (for example, converting a png image) in Matlab because Matlab for some reason uses the libraries in its path over those given in system call (this is for Mac, but probably for *nix and maybe even Windows).
This is an example call:
[s,w] = system([IMAGEMAGICK_PATH,'convert ',pwd,'/',outfile,'.png -rotate 90 ',pwd,'/',outfile,'.png']);
This is the error:
error:
dyld: Library not loaded: /opt/local/lib/libtiff.3.dylib
Referenced from: /opt/local/bin/convert
Reason: Incompatible library version: convert requires version 12.0.0 or later, but libtiff.3.dylib provides version 11.0.0
The work around is to replace the Matlab library with the ImageMagick one (only need to do this once and will stay until you upgrade Matlab):
cd /Applications/MATLAB74/bin/maci/ (or wherever your Matlab is installed)
mv libtiff.3.7.1.dylib libtiff.3.7.1.dylib.MATLAB (give it a different name)
ln -s /opt/local/lib/libtiff.3.dylib libtiff.3.7.1.dylib (create a soft link to the Imagemagick library file)
EDIT: This is the much better way to handle this:
setenv(‘DYLD_LIBRARY_PATH’,['/opt/local/lib/:' getenv('DYLD_LIBRARY_PATH')])
I was quite excited when this worked, until I realized that swapping out the dylib breaks imread() with TIF files. See:
>> f = imread(’some_tiff_file.TIF’)
??? Error using ==> rtifc
Invalid TIFF image index specified.
Error in ==> readtif at 32
[X, map, details] = rtifc(filename, args.index);
Error in ==> imread at 403
[X, map] = feval(fmt_s.read, filename, extraArgs{:});
Is this the case for you as well? Or is there something different about our libTIFFs?
Yes, same thing happens to me. I was just going to tell you to not use *.tif/*.tiff files since *.png is better, but I spent a few seconds and found out this works.
1. Do NOT create (or undo) the softlink I describe above
2. Instead, set the environment path to the MacPorts library
Here’s a code snip:
setenv('PATH', ['/opt/local/lib/;' getenv('PATH') ]);
[s,w] = system(['/opt/local/bin/convert ',pwd,'/foo.png -rotate 90 ',pwd,'/foo2.png']);
Thanks for the reply.
TIFFs are, for better or for worse, a standard in many scientific disciplines, especially for microscopy and geographic data, so just swapping over to PNG is not an option. The TIFF format also has some unique features, like the way it handles metadata and multiple image frames per file.
Anyway, the code snip you sent me doesn’t solve the problem, I get the same error whether or not I change the path environment variable.
In both cases, the error (same as my above post) is:
dyld: Library not loaded: /opt/local/lib/libtiff.3.dylib
So I don’t think the path has anything to do with it.
Yes, tiff has some ‘features’ (most of them unnecessary in my opinion, but I digress) and seems to be default for many programs (even Mac’s Grab.app).
OK, I had mistakenly left the soft link from this original post in place when I tested the above. Good news is I have figured it out!:
Once for your session you need to do this:
setenv('DYLD_LIBRARY_PATH',['/opt/local/lib/:' getenv('DYLD_LIBRARY_PATH')])
Then call convert (e.g.):
[s,w] = system(['/opt/local/bin/convert ',pwd,'/foo.png -rotate 90 ',pwd,'/foo2.png'])
Sweet! This worked. Many thanks.
Thanks, needed this to get matlab calling openjpeg.
I want to load current libtiff in matlab 2009a running on windows.
Does it have to be windows compiled libtiff or can I also use linux compiled libtiff, ie DLL or .so file ? Can someone PROFICIENT with this procedure test a version and post instructions that actually work as well as some debugging information ? If libtiff has dependencies, how to debug them or discover them from error messages ?
Thanks to the star who can help
Andy
Andy,
The command I provided above should work to put the path of the library your trying to use before Matlab’s libraries path (of course, your path won’t be /opt/local/lib/). You should also look into the loadlibrary command.
As of version 7.9 (aka R2009b), there is a new Tiff Class, which acts as a gateway to the LibTIFF libarary. I have that version, and just ran it and got this:
>> Tiff.getVersion()
ans =
LIBTIFF, Version 3.7.1
Copyright (c) 1988-1996 Sam Leffler
Copyright (c) 1991-1996 Silicon Graphics, Inc.
Version 3.7.1 came out in… *gulps* 2004!!
I tried to load a newer version (I’m on a Mac, so am using the latest from MacPorts) using the loadlibrary command, but ran into 64-bit problems. Hope this info helps you somewhat. If you get really stuck, I’d contact Mathworks support.
Hi admin, plz send me an email. again you did not answer if mixing libraryies of operating systms woud cause problem or not AND you use setenv which is NOT on windows which is my matlab platform. Am I missing sth or you are not reading properly ?
Andy,
The setenv command in Matlab is very much available on the Windows platform.
I don’t understand that if it’s just a newer version of libtiff you’re trying to use, that you don’t use that library compiled for Windows(!) My guess is you will probably not be able to use libraries from a different OS. If you’re really trying to do this, it seems such a specific thing you’re trying to do that, again, I would recommend you contact Mathworks Tech Support if you can’t find help anywhere else on-line.
Andy,
I want to mention that there’s always more than one way to do something, and it may be easier for you to use the convert command from the ImageMagick toolbox, by calling it with a system call in Matlab.