【发布时间】:2016-05-28 23:14:11
【问题描述】:
我阅读了this post,并天真地试图显示一个灰度图像,其中包含从.mat 格式的矩阵中随机抽取的 100 个手写数字,该矩阵包含 5000 行,每行对应一个手写数字。我使用以下代码:
library(R.matlab)
data <- readMat('data.mat')
X = data$X
X = X[sample(nrow(X), size=100, replace=F),]
par(mar = rep(0, 4))
image(X, axes = FALSE, col = grey(seq(0, 1, length = 256)))
结果并不好,因为事实证明矩阵中的每个 400 元素行向量都为一个像素的 20 x 20 像素图像提供了信息;因此,如果我们想让它们并排显示,则必须先验地设置各个数字的单独显示图像的高度和宽度。这就是我的编码“技能”不足的地方。
我已保存dataset here。需要明确的是,图像位于 R str(data) 列表的 X 对象中:
List of 2
$ X: num [1:5000, 1:400] 0 0 0 0 0 0 0 0 0 0 ...
$ y: num [1:5000, 1] 10 10 10 10 10 10 10 10 10 10 ...
- attr(*, "header")=List of 3
..$ description: chr "MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Sat May 28 18:21:58 2016 "
..$ version : chr "5"
..$ endian : chr "little"
如果我在用它构建20 x 20 矩阵后限制自己只显示矩阵X 的一行,我就更接近目标了:
X = data$X
X = X[sample(nrow(X), size = 1 ,replace=F),]
X = matrix(X, nrow = 20, byrow= T)
par(mar = rep(0, 4))
image(X, axes = FALSE, col = grey(seq(0, 1, length = 256)))
matlab中将矩阵数据拆解成图像的代码是:
function [h, display_array] = displayData(X, example_width)
%DISPLAYDATA Display 2D data in a nice grid
% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
% stored in X in a nice grid. It returns the figure handle h and the
% displayed array if requested.
% Set example_width automatically if not passed in
if ~exist('example_width', 'var') || isempty(example_width)
example_width = round(sqrt(size(X, 2)));
end
% Gray Image
colormap(gray);
% Compute rows, cols
[m n] = size(X);
example_height = (n / example_width);
% Compute number of items to display
display_rows = floor(sqrt(m));
display_cols = ceil(m / display_rows);
% Between images padding
pad = 1;
% Setup blank display
display_array = - ones(pad + display_rows * (example_height + pad), ...
pad + display_cols * (example_width + pad));
% Copy each example into a patch on the display array
curr_ex = 1;
for j = 1:display_rows
for i = 1:display_cols
if curr_ex > m,
break;
end
% Copy the patch
% Get the max value of the patch
max_val = max(abs(X(curr_ex, :)));
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
reshape(X(curr_ex, :), example_height, example_width) / max_val;
curr_ex = curr_ex + 1;
end
if curr_ex > m,
break;
end
end
% Display Image
h = imagesc(display_array, [-1 1]);
% Do not show axis
axis image off
drawnow;
end
导致:
【问题讨论】: