【问题标题】:ndims returned from imread从 imread 返回的 ndim
【发布时间】:2018-12-09 04:37:14
【问题描述】:

我正在从文件夹中提取图像。它的大小为 128*128。为此,我使用以下代码行:

[FileName,PathName] = uigetfile('*.jpg','Select the Cover Image');
file = fullfile(PathName,FileName);
disp(['User selected : ', file]);
cover = imread(file);
%cover = double(cover);
if ndims(cover) ~= 3
    msgbox('The cover image must be colour');
break;
end
figure(1);
subplot(1,2,1);
imshow(uint8(cover),[]);
title('Cover image');

%num specifies the number of Iterations for the Arnold Transform
num = input('\nEnter the value of num: ');
encrypted = arnold(cover,num);
imshow(encrypted);

函数arnold如下:

function [ out ] = arnold( in, iter )
    if (ndims(in) ~= 2)
        error('Oly two dimensions allowed');
    end
    [m n] = size(in);
    if (m ~= n)
        error(['Arnold Transform is defined only for squares. ' ...
        'Please complete empty rows or columns to make the square.']);
    end
    out = zeros(m);
    n = n - 1;
    for j=1:iter
        for y=0:n
            for x=0:n
                p = [ 1 1 ; 1 2 ] * [ x ; y ];
                out(mod(p(2), m)+1, mod(p(1), m)+1) = in(y+1, x+1);
            end
        end
        in = out;
        imwrite(uint8(in),'Enc.jpg');
    end
end

我收到以下错误:

??? Error using ==> arnold at 9
Only two dimensions allowed

Error in ==> deepoo at 20
    encrypted = arnold(cover,num);

有人可以解释 ndims 的用途吗?我有点困惑。 如果ndims=3,那么图像是彩色的吗?如果ndims=2,是否意味着图像没有着色?

【问题讨论】:

    标签: image matlab image-processing


    【解决方案1】:

    这完全正确。

    彩色图像作为 3 个通道(R、G 和 B)读入 MATLAB,因此第 3 维是这些通道中的每一个。如果图像是灰度的,它将只有 2 个维度。因为在灰度中,保证 R、G 和 B 值是相同的。从彩色到灰度有多种方法(一种是rgb2gray) - 然后从灰度到彩色,您只需将相同的 2D 矩阵复制到 3D 中。执行此操作的最短方法之一是使用 repmat 函数。

    下面是一个很长的(但希望清晰的将灰度转换为 3D 的方法)

    colorImg(:,:,1)=grayScaleImg;
    colorImg(:,:,2)=grayScaleImg;
    colorImg(:,:,3)=grayScaleImg;
    

    而且您可以一次性完成所有操作:

    colorImg(:,:,1:3)=grayScaleImg;
    

    希望这会有所帮助!

    这里有一些 MATLAB 文档:http://www.mathworks.com/help/matlab/ref/imread.html

    尤其是(第 3 段)

    返回值 A 是一个包含图像数据的数组。如果文件 包含一个灰度图像,A 是一个 M×N 数组。如果文件包含 真彩色图像,A 是一个 M×N×3 数组。对于 TIFF 文件 包含使用 CMYK 颜色空间的彩色图像,A 是 M×N×4 数组。请参阅格式特定信息部分中的 TIFF 了解更多信息。

    【讨论】:

    • 我使用了彩色图像。它有 3 个维度。将其转换为灰度时,显示的维度数为 1。我仍然很困惑.. 黑白图像有多少维度?
    • 1 维,你确定吗?你是怎么转换的?黑白图像有两个维度,但如果它是真正的黑白图像并且 MATLAB 识别它,它会在逻辑矩阵中读取它,并且值只会是 0 和 1。对于 .jpg 来说几乎是不可能的由于压缩,只是黑白的
    • @competesingh 我必须看到强有力的证据才能相信您正在获得一维图像。我认为您混淆了尺寸和颜色通道。
    猜你喜欢
    • 2020-08-17
    • 2013-07-30
    • 2021-02-27
    • 1970-01-01
    • 2021-04-05
    • 2015-03-23
    • 1970-01-01
    • 2019-09-22
    相关资源
    最近更新 更多