蒙太奇在Matlab中 - 保存并显示 [英] Montage in Matlab - save and show

查看:1320
本文介绍了蒙太奇在Matlab中 - 保存并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时:

fileFolder = fullfile(matlabroot,'toolbox','images','imdemos');
dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.tif'));
fileNames = {dirOutput.name}'

zImg=montage(fileNames, 'Size', [2 5]);
imwrite(zImg,'C:\Users\xc\Desktop\ATMtemp.png') 

我在新图中得到蒙太奇图像,但是我可以取消它并将其存储在内存中吗?
此外,我无法保存蒙太奇。任何原因以及如何在不使用 getframe 的情况下执行此操作,因为我不想显示生成的数字?

I get the montage image in a new figure, but can I cancel it and just store it in memory? Furthermore, I cannot save the montage. Any reason why and how can I do it without using getframe as I do not want to show the figure generated?

推荐答案

蒙太奇MATLAB图像处理工具箱中的 功能仅用于显示目的,因此仅显示图形。从这个图中获取图像数据的唯一方法是,如果将函数的句柄指定为输出(在您的情况下为 zImg ),然后使用你建议的 getframe / cdata 成语。但是,这会给你一个白色边框,你也注意到了。

The montage function in MATLAB's image processing toolbox is for display purposes only and so it only shows a figure. The only way that you'd be able to get the image data from this figure is if you assign a handle to a function as output (which is zImg in your case), then use the getframe/cdata idiom you have suggested. However, this will give you a white border as you have also noticed.

如果你想创建一个与相同的图像蒙太奇,你可以构建蒙太奇正在做的事情。 蒙太奇的替代方法是读取单元格数组中的所有图像,然后手动将它们排列在蒙太奇中。我将假设您正在以行主格式堆叠图像,因此行一次填充一行。这意味着图像1到5将是第一行,而图像6到10将是第二行。

If you want to create an image that is doing the same thing as montage, you can construct what montage is doing yourself. An alternative to montage would be to read in all of the images in a cell array, then arrange them in a montage manually. I'm going to assume that you are stacking the images in row-major format, so the rows are being populated one row at a time. That means images 1 to 5 will be the first row while images 6 to 10 will be the second row.

将它变成2D矩阵的技巧是你需要使用 重塑 reshape 将以列主格式填充元素,因此您需要构建结果的转置,然后在完成后转置。之后,使用 cell2mat 消除单元格数组并制作最终的2D矩阵。

The trick to get it into a 2D matrix is that you need to use reshape. reshape will populate elements in column-major format, so you need to construct the transpose of your result, then transpose that when you're done. After, use cell2mat to eliminate the cell arrays and make a final 2D matrix.

因此,请执行以下操作:

As such, do something like this:

%//  Your code to get all of the image file names
fileFolder = fullfile(matlabroot,'toolbox','images','imdemos');
dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.tif'));
fileNames = {dirOutput.name};

%// Create a 1D cell array that will store all of the images
images = cell(1, numel(fileNames));

%// Read in the images yourself and populate the cell array
for idx = 1 : numel(fileNames);
    images{idx} = imread(fileNames{idx});
end

%// Reshape the cell array so that it's a 2 x 5 matrix, then
%// convert the 2D cell array into a final 2D matrix.
zImg = cell2mat(reshape(images, [5, 2]).');

%// Write to file
imwrite(zImg,'C:\Users\xc\Desktop\ATMtemp.png') 

这篇关于蒙太奇在Matlab中 - 保存并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆