Matlab的imread、Matlab的拖拽如何添加独特的图片格式?
How to add unique image formats to Matlab's imread, and Matlab's drag and drop?
我们有许多内部图像格式,我在 Matlab 中进行处理。我有 read/write 个功能。具体来说,考虑 TGA image format, for which there is a file exchange image reader.
Matlab 对 imread 支持的图像格式具有合理的拖放支持。
也就是说,您可以从资源管理器中拖动图像,将其放在 "Workspace" 窗格中,Matlab 将读入图像,并将其复制到您的工作区中。
我希望能够添加对 TGA 文件的拖放支持和 imread 支持。 (例如,imread 有很好的文件名自动完成功能,tga_read_image 没有。)
我想 this 就是您要找的。引用官方文档:
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.
例如:
function opentga(file)
% Your logic for reading and, eventually,
% displaying TGA files when drag and drop
% or other opening events occur.
end
这是直接取自 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
另一种方法是重载 uiopen
函数,如 this 文件交换版本所示。
从 开始,我在我的 MATLAB 路径上创建了以下 M 文件:
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
将 ICS 文件拖放到 MATLAB 的命令 window 上会在命令行中显示以下内容:
>> uiopen('/Users/cris/newdip/examples/cermet.ics',1)
assigning into base: cermet
检查:
>> whos cermet
Name Size Bytes Class Attributes
cermet 256x256 65714 dip_image
阅读 uiopen
的代码(您只需键入 edit uiopen
)表明这会调用 open
和文件名,然后调用 openics
和文件名没有输出参数。
您也可以输入
img = open('/Users/cris/newdip/examples/cermet.ics');
调用 openics
并将图像加载到变量 img
。
注意 1:我正在使用 ICS,因为我没有任何 TGA 图像可供测试。 ICS 是一种显微镜图像文件格式。
注2:readim
是DIPimage
中的函数
注意 3:这很酷,我以前从来没有费心尝试将文件拖放到 MATLAB 上。 :)
其他答案解决了“拖放”问题。他们没有解决如何将专有图像格式集成到 imread
中的问题。这可以使用 imformats
命令相当直接地完成。
how/why 的问题我花了 3.5 年时间才弄清楚,恐怕仍然没有答案......该功能已经存在超过 15 年了。
我们有许多内部图像格式,我在 Matlab 中进行处理。我有 read/write 个功能。具体来说,考虑 TGA image format, for which there is a file exchange image reader.
Matlab 对 imread 支持的图像格式具有合理的拖放支持。
也就是说,您可以从资源管理器中拖动图像,将其放在 "Workspace" 窗格中,Matlab 将读入图像,并将其复制到您的工作区中。
我希望能够添加对 TGA 文件的拖放支持和 imread 支持。 (例如,imread 有很好的文件名自动完成功能,tga_read_image 没有。)
我想 this 就是您要找的。引用官方文档:
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.
例如:
function opentga(file)
% Your logic for reading and, eventually,
% displaying TGA files when drag and drop
% or other opening events occur.
end
这是直接取自 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
另一种方法是重载 uiopen
函数,如 this 文件交换版本所示。
从
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
将 ICS 文件拖放到 MATLAB 的命令 window 上会在命令行中显示以下内容:
>> uiopen('/Users/cris/newdip/examples/cermet.ics',1)
assigning into base: cermet
检查:
>> whos cermet
Name Size Bytes Class Attributes
cermet 256x256 65714 dip_image
阅读 uiopen
的代码(您只需键入 edit uiopen
)表明这会调用 open
和文件名,然后调用 openics
和文件名没有输出参数。
您也可以输入
img = open('/Users/cris/newdip/examples/cermet.ics');
调用 openics
并将图像加载到变量 img
。
注意 1:我正在使用 ICS,因为我没有任何 TGA 图像可供测试。 ICS 是一种显微镜图像文件格式。
注2:readim
是DIPimage
注意 3:这很酷,我以前从来没有费心尝试将文件拖放到 MATLAB 上。 :)
其他答案解决了“拖放”问题。他们没有解决如何将专有图像格式集成到 imread
中的问题。这可以使用 imformats
命令相当直接地完成。
how/why 的问题我花了 3.5 年时间才弄清楚,恐怕仍然没有答案......该功能已经存在超过 15 年了。