Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add unique image formats to Matlab's imread, and Matlab's drag and drop?

We have a number of internal image formats which I process in Matlab. I have read/write functions for all of them. For specificity, consider the TGA image format, for which there is a file exchange image reader.

Matlab has reasonable drag and drop support for image formats supported by imread.

That is, you can drag an image from explorer, drop it on the "Workspace" pane, and Matlab will read in the image, and copy it into your workspace.

I'd like to be able to add drag and drop support, and imread support for TGA files. (imread has nice autocomplete for filenames for instance, tga_read_image does not.)

like image 681
John Avatar asked Feb 05 '18 22:02

John


2 Answers

I think this is what you are looking for. Quoting the official documentation:

open name opens the specified file or variable in the appropriate application

You can extend the functionality of open by defining your own file-handling function of the form openxxx, where xxx is a file extension. For example, if you create a function openlog, then the open function calls openlog to process any files with the .log extension. The open function returns any single output defined by your function.

For example:

function opentga(file) 
    % Your logic for reading and, eventually,
    % displaying TGA files when drag and drop
    % or other opening events occur.
end

And here is a full working example directly taken from the link:

function opentxt(filename)
   [~, name, ext] = fileparts(filename); 
   fprintf('You have requested file: %s\n', [name ext]);

   if exist(filename, 'file') == 2
     fprintf('Opening in MATLAB Editor: %s\n', [name ext]);
     edit(filename);
   else
      wh = which(filename);
      if ~isempty(wh)
         fprintf('Opening in MATLAB Editor: %s\n', wh);
         edit(wh);
      else
        warning('MATLAB:fileNotFound', ...
                'File was not found: %s', [name ext]);
      end
   end
end

An alternative path consists in overloading the uiopen function, as shown in this File Exchange release.

like image 184
Tommaso Belluzzo Avatar answered Sep 26 '22 00:09

Tommaso Belluzzo


Starting off from Tommaso's answer, I created the following M-file on my MATLAB path:

function out = openics(filename)
img = readim(filename);
if nargout==1
   out = img;
else
   [~,varname] = fileparts(filename);
   disp(['assigning into base: ',varname])
   assignin('base',varname,img);
end

Dragging and dropping an ICS file onto MATLAB's command window shows the following on the command line:

>> uiopen('/Users/cris/newdip/examples/cermet.ics',1)
assigning into base: cermet

Check:

>> whos cermet
  Name          Size             Bytes  Class        Attributes
  cermet      256x256            65714  dip_image              

Reading the code for uiopen (you can just type edit uiopen) shows that this calls open with the filename, which then calls openics with the filename and no output argument.

You can also type

img = open('/Users/cris/newdip/examples/cermet.ics');

to call openics and load the image into variable img.

NOTE 1: I'm using ICS because I don't have any TGA images to test with. ICS is a microscopy image file format.

NOTE 2: readim is a function in DIPimage

NOTE 3: This is cool, I had never bothered trying to drag and drop files onto MATLAB before. :)

like image 45
Cris Luengo Avatar answered Sep 24 '22 00:09

Cris Luengo