在Matlab中先前定义和使用变量时,如何解决“未定义函数或变量”错误?

时间:2019-06-19 22:23:12

标签: image matlab

该代码应该在某个文件夹中查找所有子文件夹。然后,应该在每个子文件夹中查找每个图像并将它们平均在一起。然后,它将平均图像保存到单独的文件夹中,并对所有剩余的子文件夹重复。

您可以通过复制并粘贴到自己的MatLab程序中并更改给定的目录来直接测试此代码,而无需依赖。

我的问题:变量'sumImage'在for循环的最后捕获了一个错误。

finImage = uint8(sumImage / numberOfImages);

即使sumImage在前面的代码中经常使用,也没有错误。

我认为这是彼此之间存在for循环的障碍,但是,当我第一次运行该程序时,它几乎可以完美地运行。但是,进一步尝试运行代码已导致看到编译器错误。我尝试过更改变量名,甚至在一开始就将其声明为双精度,但无济于事。

folder1 = dir('C:\Users\slenka\Desktop\Image_Analysis\Original');
numberOfFolders = length(folder1);

for z = 1 : numberOfFolders
    currD = folder1(z).name;
    cd(currD);
    imageFiles = [dir(fullfile(currD,'*.TIF'));         dir(fullfile(currD,'*.PNG')); dir(fullfile(currD,'*.BMP'));     dir(fullfile(currD,'*.jpg'))];
    numberOfImages = length(imageFiles);

    for k = 1 : numberOfImages
    fullFileName = fullfile(currD, imageFiles(k).name);
    fprintf('About to read %s\n', fullFileName);
    thisImage=imread(fullFileName);
    [thisRows, thisColumns, thisNumberOfColorChannels] = size(thisImage);
    if k == 1
        % Save the first image.
        sumImage = double(thisImage);
        % Save its dimensions so we can match later images' sizes to this first one.
        rows1 = thisRows;
        columns1 = thisColumns;
        numberOfColorChannels1 = thisNumberOfColorChannels;
        theyreColorImages = numberOfColorChannels1 >= 3;
    else
        % It's the second, or later, image.
        if rows1 ~= thisRows || columns1 ~= thisColumns
            % It's not the same size, so resize it to the size of the first image.
            thisImage = imresize(thisImage, [rows1, columns1]);
        end
        % Make sure the colors match - either all color or all gray scale, according to the first one.
        if thisNumberOfColorChannels == 3 && numberOfColorChannels1 == 1
            % We have color.  Need to change it to grayscale to match the first one.
            thisImage = rgb2gray(thisImage);
            theyreColorImages = false;
        elseif thisNumberOfColorChannels == 1 && numberOfColorChannels1 == 3
            % We have grayscale.  Need to change it to RGB to match the first one.
            thisImage = cat(3, thisImage, thisImage, thisImage);
            theyreColorImages = true;
        end
        % Now do the summation.
        sumImage = sumImage + double(thisImage); % Be sure to cast to double to prevent clipping.   [rows, columns, numberOfColorBands]=size(thisImage);

        % It can't display an RGB image if it's floating point and more than 255.
        % So divide it by the number of images to get it into the 0-255 range.
        if theyreColorImages
            displayedImage = uint8(sumImage / k);
        else
            displayedImage = sumImage;
        end
        imshow(displayedImage, []);
        drawnow;
    end
    end


%--------------------------------------------------------------------------------
% Compute and display the final image:
finImage = uint8(sumImage / numberOfImages);
baseFileName = sprintf('Image #%d.png', z);
fullFileName =     fullfile('C:\Users\slenka\Desktop\Image_Analysis\Averaged', baseFileName);
imwrite(finImage, fullFileName);
    end

预期结果:一个输出文件夹,其中包含该程序访问的每个文件夹的平均图像(总共10个文件夹)

实际结果:编译器错误和输出文件夹中的两个图像失真

编辑:由于评论,我可以使用它,但是现在它可以使用了,它将平均桌面上的所有图像。有任何想法吗?我只希望它平均特定文件夹中的图像。

0 个答案:

没有答案