我正在从一个文件文件夹中获取一个图像。它的尺寸是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=3,那么图像是彩色的吗?如果是ndims=2,这是否意味着图像不是彩色的?
发布于 2014-07-17 19:31:09
这是完全正确的。
彩色图像被读取到MATLAB作为3个通道(R,G,和B),因此第3维是每个通道。如果图像是灰度的,它将仅为二维。因为在灰度中,R、G和B值保证是相同的。从颜色到灰度有很多种方法(比如rgb2gray)--然后从灰度到彩色,只需将相同的2D矩阵复制到3D中即可。其中一个最短的方法,这是与爬虫功能。
下面是一个非常长的(但希望清楚地将灰度转换为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是array N数组.如果文件包含一个真彩色图像,A是一个M-byN-by-3数组.对于包含使用CMYK颜色空间的彩色图像的TIFF文件,A是一个M-byN-by4数组.有关更多信息,请参见格式特定信息部分中的TIFF。
https://stackoverflow.com/questions/24811523
复制相似问题