【问题标题】:Convert char output into image in MATLAB在MATLAB中将char输出转换为图像
【发布时间】:2017-03-05 05:30:00
【问题描述】:

有没有办法将matlab文本输出转换成图片文件?

例如,如果我有这样的输出:

17    24     1     8    15
23     5     7    14    16
 4     6    13    20    22
10    12    19    21     3
11    18    25     2     9

我可以将其转换为图像吗?就像拍摄屏幕快照 这个矩阵区域的?

谢谢。

注意:结果需要像文本输出一样对齐。 我知道如何使用不同的字体对齐。我在找一个 对齐数字的另一种方法。我只是在寻求可能性 拍摄快照效果。

【问题讨论】:

  • 我不明白否定检查。是不是因为太简单了?如果是,至少在检查否定之前提供一个答案。

标签: matlab


【解决方案1】:

您可以使用msgbox 函数在figure 中显示矩阵;矩阵必须用functinonum2str转换成字符串,然后你必须:

  • 删除确定按钮:
    • 使用findobj函数获取按钮的handle
    • 使用delete 函数将其删除
  • 设置消息框图形HandleVisibilityon(默认设置为callback
  • 使用函数print将消息框图形打印为图像

这是实现:

% Create the message box
x=msgbox(num2str(a),'Dump of matrix a')
% Set the message box figure HandleVisibility to on
x.HandleVisibility='on'
% Find the handle of the OK pushbutton
hp=findobj(x,'style','pushbutton')
% Delete the OK pushbutton
delete(hp)
% Print the figure
print -djpeg99 image_of_a_matrix

编辑以回答关于文本对齐方式的评论

在文本框中表示的数字被表示为字符串;即使在生成字符串时正确对齐数字,字符串的表示也取决于所选字体。

如果您使用Monospaced font,例如 Courier、Courier New、Lucida Console,您可以获得 dsesired 对齐。

相反,如果你使用比例字体,你会失去对齐,因为不同的字符占据不同的水平空间。

对于上述建议的代码,您可以这样设置一个等宽字体(例如Courier):

% Create the message box
x=msgbox(num2str(a),'Dump of matrix a')
% Set the message box figure HandleVisibility to on
x.HandleVisibility='on'
% Find the handle of the OK pushbutton
hp=findobj(x,'style','pushbutton')
% Delete the OK pushbutton
delete(hp)
% Get the handle of the text item
txt_h=x.Children.Children
% Change the text font
txt_h.FontName='Courier'
txt_h.FontWeight='bold'
% Print the figure
print -djpeg99 image_of_a_matrix_1

这是比例字体的图片:

这是等宽字体的图片:

希望这会有所帮助,

卡普拉'

【讨论】:

  • 谢谢。但我正在寻找一个一致的结果。结果中的数字未对齐。
  • 我已经更新了答案:alignement 的问题是由字体类型引起的。您可以通过使用Courier等_Monospaced_字体来实现。
  • 非常感谢。我正在寻找获得对齐结果的替代方法,即图像捕获。但是,还是要感谢它。
  • 好的,很抱歉没能帮上忙,you could try this
【解决方案2】:

您可以使用insertText 函数将文本插入图像中。

这是我的代码示例:

A = [17    24     1     8    15
     23     5     7    14    16
      4     6    13    20    22
     10    12    19    21     3
     11    18    25     2     9];

[n_rows, n_cols] = size(A);

%Set background color to [240, 240, 240] (light gray).
I = zeros(n_rows*24+12, n_cols*10*5+8, 3, 'uint8') + 240; %Background color

text_str = cell(n_rows, 1); 
box_color = zeros(n_rows, 3) + 240; %Set box color to same as background color
text_color = repmat([125, 39, 39], n_rows, 1); %Set text color to brown.

%Fill rows of A into cell array text_str
for i = 1:n_rows
    text_str{i} = sprintf('%5d', A(i, :));
end

%Set position of each of the rows.
pos_x = zeros(1, n_rows) + 4;
pos_y = (0:n_rows-1)*24 + 6;
position = [pos_x', pos_y'];

%Insert text into image I.
I = insertText(I, position, text_str, 'FontSize', 14, 'BoxColor', box_color, 'TextColor', text_color,...
               'Font', 'Courier New Bold', 'BoxOpacity', 1);

figure;imshow(I);

结果:


不同字体的对齐文本:

应用我的解决方案来对齐没有固定宽度的文本,每个数字需要不同的坐标(总共 25 个协调和 25 个字符串)。

以下解决方案使用Arial字体:

A = [17    24     1     8    15
     23     5     7    14    16
      4     6    13    20    22
     10    12    19    21     3
     11    18    25     2     9];

[n_rows, n_cols] = size(A);
n_elms = numel(A);

%Set background color to [240, 240, 240] (light gray).
I = zeros(n_rows*24+12, n_cols*10*5+8, 3, 'uint8') + 240; %Background color

text_str = cell(n_elms, 1);
box_color = zeros(n_elms, 3) + 240; %Set box color to same as background color
text_color = repmat([125, 39, 39], n_elms, 1); %Set text color to brown.

%Fill rows and columns of A into cell array text_str
counter = 1;
for y = 1:n_rows
    for x = 1:n_cols
        text_str{counter} = sprintf('%5d', A(y, x));
        counter = counter + 1;
    end
end

%Set position of each of the rows and columns.
[pos_x, pos_y] = ndgrid(0:n_cols-1, 0:n_rows-1);
pos_x = pos_x(:)*10*5 + 4;
pos_y = pos_y(:)*24 + 6;
position = [pos_x, pos_y];

%Insert text into image I.
I = insertText(I, position, text_str, 'FontSize', 14, 'BoxColor', box_color, 'TextColor', text_color,...
               'Font', 'Arial Bold', 'AnchorPoint','RightTop', 'BoxOpacity', 1);

figure;imshow(I);

结果:

【讨论】:

猜你喜欢
  • 2017-12-31
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多