在matlab中为多个图像添加噪声

时间:2017-03-10 06:50:26

标签: image matlab edit noise

我尝试使用Matlab在一个文件夹中为多个图像添加噪点,并使用以下代码将它们保存在另一个文件夹中。

%save the noise parameter.
noise = [0.01,0.02,0.03];

for i = 1:4

%we generate the filename (you can adapt this code)
imname = dir(fullfile('C:\Users\bluen\Pictures\AI selection\Amplifier',sprintf('*d%.tiff',i)));
%read the image.
im = imread(imname);

for j = 1:length(noise)

%apply the noise
J = imnoise(im,'salt & pepper',noise(j));
%save image in the right folder
imwrite(J,dir(fullfile('C:\Users\bluen\Pictures\AI selection\amp-matlab',sprintf('amp-matlab%d',j))));

end
end

运行代码时出现以下错误:

error

3 个答案:

答案 0 :(得分:1)

正如错误所述,imread需要一个字符串,并且你从dir给它结构输出......

for i = 1:4
imstruct = dir(fullfile('C:\Users\bluen\Pictures\AI selection\Amplifier',sprintf('*d%.tiff',i)));
% If you know that there is only 1 file which will be returned by your
% dir command (not always a safe assumption) then you can use
% im = imread(imstruct.name);
% If you were using wildcards then there would be multiple elements in the 
% imstruct struct. Say you wanted the first one, you would use
im = imread(imstruct(1).name);
% This will work if there is only 1 item returned, so safer to use anyway

% ... other code

imread文档:https://uk.mathworks.com/help/matlab/ref/imread.html

dir文档:https://uk.mathworks.com/help/matlab/ref/dir.html

答案 1 :(得分:0)

注意:代码dir中的imname = dir(...)命令将返回目录列表,而代码imread中的im = imread(imname);将返回单个文件名。

您已遍历目录项,并逐个阅读,如下所示:

files = dir('*.tiff');
for file = files'
    im = imread(file.name);
    % Do some stuff
end

答案 2 :(得分:0)

我发现你的实施中几乎没有问题 在尝试遵循原始逻辑时,我尽可能多地修复代码。

主要问题:

  • d%替换为%d

原文:

imname = dir(fullfile('C:\Users\bluen\Pictures\AI selection\Amplifier', sprintf('*d%.tiff',i)));

更正:

imname = dir(fullfile('C:\Users\bluen\Pictures\AI selection\Amplifier', sprintf('*%d.tiff',i)));
  • dir命令用于列表文件夹内容,在编写文件时不能使用它。
  • 您可以使用两个索引作为目标文件名,因为您每个源图像生成3个目标图像 更好的方法是使用输入文件的名称,并将索引连接到它的名称。

原文:

imwrite(J, dir(fullfile('C:\Users\bluen\Pictures\AI selection\amp-matlab',sprintf('amp-matlab%d',j))));

更正:

imwrite(J, fullfile('C:\Users\bluen\Pictures\AI selection\amp-matlab', sprintf('amp%d_%s', j, imname)));

创建输出文件名,如:
amp1_im1.tiff
amp2_im1.tiff
amp3_im1.tiff
amp1_im2.tiff
...

我的代码示例的第一部分,构建输入文件,用于在我的PC中测试我的实现 您可以将它用作基线(不要忘记将其删除)。

我在代码中就少数几个小问题发表了一些评论。

代码:

%Create input data for testing in my PC.
%Used only for verifiying my implementation is correct.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I = imread('cameraman.tif');

for i = 1:4
    imname = ['C:\Users\bluen\Pictures\AI selection\Amplifier\', sprintf('im%d.tiff', i)];
    imwrite(imresize(I, 1/i), imname); %Build images with different sizes for testing...
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%save the noise parameter.
noise = [0.01,0.02,0.03];

%Input folder:
indir = 'C:\Users\bluen\Pictures\AI selection\Amplifier\';

%Following loop assumes there are exactly 4 input tiff images in the input folder.
for i = 1:4
    %Dir command: return a list of files.
    imname = dir(fullfile(indir, sprintf('*%d.tiff',i)));

    %Assume only the first image is relevant (just for example purpose).
    imname = imname(1).name;

    %Attach input folder to file name.
    full_imname = fullfile(indir, imname);

    %Read the image.
    im = imread(full_imname);

    for j = 1:length(noise)
        %apply the noise
        J = imnoise(im,'salt & pepper',noise(j));

        %save image in the right folder
        imwrite(J, fullfile('C:\Users\bluen\Pictures\AI selection\amp-matlab', sprintf('amp%d_%s', j, imname)));
    end
end
相关问题