Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中显示.mat格式的灰度图像_R_Image_Matlab - Fatal编程技术网

在R中显示.mat格式的灰度图像

在R中显示.mat格式的灰度图像,r,image,matlab,R,Image,Matlab,我阅读并天真地试图显示一个灰度图像,其中包含从.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, le

我阅读并天真地试图显示一个灰度图像,其中包含从
.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)))
如果在构建了一个
20x20
矩阵之后,我将自己限制为仅显示矩阵
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
其结果是:


这会将矩阵行重塑为块。虽然不能保证效率很高,但对于像您的示例中那样的100x400矩阵,速度非常快

# get 100 random rows of X
X <- data$X
X <- X[sample(nrow(X), size=100, replace=FALSE),]

# allocate empty image matrix (200 by 200 pixels)
Z <- matrix(rep(0, length(X)), nrow=200)

# fill empty image matrix
for (row in 0:9) {
    rmin <- 1 + (row)*20
    for (col in 0:9) {
        cmin <- 1 + (col)*20
        Z[rmin:(rmin+19), cmin:(cmin+19)] <- X[row * 10 + col + 1,]
    }
}
# plot (after rotating matrix 90 degrees)
image(t(apply(Z, 2, rev)))
#随机获取100行X

谢谢。不过,我得到了一个完全不同的图像。检查我编辑的作品中的图像。看起来有很多零。也许训练数据中的数字是有序的。。。您是否执行了随机化步骤?是的,我更改了
X
的名称,但我忽略了
sample()
过程,保留了最后一个
X
的原样。代码末尾缺少一个括号。请加上它。我冒昧地发布了结果图像-您可以留下它,或者在插入最后一个括号时将其删除。
# get 100 random rows of X
X <- data$X
X <- X[sample(nrow(X), size=100, replace=FALSE),]

# allocate empty image matrix (200 by 200 pixels)
Z <- matrix(rep(0, length(X)), nrow=200)

# fill empty image matrix
for (row in 0:9) {
    rmin <- 1 + (row)*20
    for (col in 0:9) {
        cmin <- 1 + (col)*20
        Z[rmin:(rmin+19), cmin:(cmin+19)] <- X[row * 10 + col + 1,]
    }
}
# plot (after rotating matrix 90 degrees)
image(t(apply(Z, 2, rev)))