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

3

7

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.)

Fullgrown answered 5/2, 2018 at 22:36 Comment(0)
V
3

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. :)

Venosity answered 6/2, 2018 at 6:9 Comment(8)
What version of matlab are you using? (I'm using 2017b). Where is opencis on your path? Very curious that I can't get this to work.Fullgrown
When I set a break point at the beginning of uiopen, it doesn't get hit when I drag a .tga file into my workspace. (Again, using 2017b, or even 2016b)Fullgrown
@John: my bad, I said "workspace" when I should have said "command window". My MATLAB only has a command window, I close all the other bits because they don't interest me. :) I use R2017a, but I don't think the behavior has changed in many years.Venosity
When dragging to the "workspace", the area that shows all current variables, uiopen is not executed for me either, it opens some Excel-like tool for an ICS file. When dragging to the editor everything is opened as a text file, but that makes sense. I don't know why the different behavior between the command window and the workspace window.Venosity
Okay, so, I can drag to the command window (cool!) and uiopen is getting called (cool!) but it's not putting the image into the workspace, it is just calling my opentga file, and not putting th eimage anywhere that I can see..... Puzzling....Fullgrown
The path are all the directories you see when you type path. These are mostly set by toolboxes, but there'll be a user directory at the top of the list. You can add directories to the path using addpath.Venosity
Copy the structure of the function I wrote. Especially the part that says assignin.Venosity
Oh. I see. Thanks!Fullgrown
M
4

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.

Matterhorn answered 5/2, 2018 at 23:1 Comment(4)
opentga works with the "open" command, which does solve some of my problems, (i.e. reasonable auto-filename completion). But, it doesn't solve all my problems, i.e. the drag and drop functionality. I have not tested overwriting uiopen but my issue with that, is it requires my users to carefully manage their paths, which they do not do, and rebel against vehemently whenever it is suggested.Fullgrown
That's curious. Using the example above, the automatic drag & drop loading works fine for me whenever i put a text file into the Matlab workspace.Matterhorn
For what concerns the paths management, in order to suppress any potential rebellion, you could tell your users to put the custom open function into a folder (a safe one, in the Matlab installation path for example), and register it with addpath. This will make it work no matter where they are located and they will just forget about it.Matterhorn
What version of matlab are you using? The drag&drop is not loading my tga files (it thinks the tga is a text file, and isn't calling opentga). I'm seeing this in both 2016b and 2017b. If I set a breakpoint in uiopen, it never gets hit on a drag and drop for a .tga file (for me).Fullgrown
V
3

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. :)

Venosity answered 6/2, 2018 at 6:9 Comment(8)
What version of matlab are you using? (I'm using 2017b). Where is opencis on your path? Very curious that I can't get this to work.Fullgrown
When I set a break point at the beginning of uiopen, it doesn't get hit when I drag a .tga file into my workspace. (Again, using 2017b, or even 2016b)Fullgrown
@John: my bad, I said "workspace" when I should have said "command window". My MATLAB only has a command window, I close all the other bits because they don't interest me. :) I use R2017a, but I don't think the behavior has changed in many years.Venosity
When dragging to the "workspace", the area that shows all current variables, uiopen is not executed for me either, it opens some Excel-like tool for an ICS file. When dragging to the editor everything is opened as a text file, but that makes sense. I don't know why the different behavior between the command window and the workspace window.Venosity
Okay, so, I can drag to the command window (cool!) and uiopen is getting called (cool!) but it's not putting the image into the workspace, it is just calling my opentga file, and not putting th eimage anywhere that I can see..... Puzzling....Fullgrown
The path are all the directories you see when you type path. These are mostly set by toolboxes, but there'll be a user directory at the top of the list. You can add directories to the path using addpath.Venosity
Copy the structure of the function I wrote. Especially the part that says assignin.Venosity
Oh. I see. Thanks!Fullgrown
F
1

The other answers address the "drag and drop" question. They do not address the question of how to integrate a proprietary image format into imread. This can be done fairly straight forwardly with the imformats command.

The issue of how/why it took me 3.5 years to figure that out will remain unanswered I'm afraid.... The feature has been around for 15+ years.

Fullgrown answered 11/8, 2021 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.