我在matlab中遇到了一些我不明白的问题下面的代码分析了一组图像,并应该返回一个连贯的图像(而且总是这样)。
但是由于我在第二个for循环中设置了if条件(为了优化),它返回一个隔行图像。
我不明白为什么,正准备把我的电脑扔出窗外我怀疑这与ind2sub有关,但据我所知,一切正常有人知道它为什么这么做吗?

function imageMedoid(imageList, resizeFolder, outputFolder, x, y)

    % local variables
    medoidImage = zeros([1, y*x, 3]);
    alphaImage = zeros([y x]);
    medoidContainer = zeros([y*x, length(imageList), 3]);

    % loop through all images in the resizeFolder
    for i=1:length(imageList)

        % get filename and load image and alpha channel
        fname = imageList(i).name;
        [container, ~, alpha] = imread([resizeFolder fname]);

        % convert alpha channel to zeros and ones, add to alphaImage
        alphaImage = alphaImage + (double(alpha) / 255);

        % add (r,g,b) values to medoidContainer and reshape to single line
        medoidContainer(:, i, :) = reshape(im2double(container), [y*x 3]);

    end

    % loop through every pixel
    for i=1:(y * x)

        % convert i to coordinates for alphaImage
        [xCoord, yCoord] = ind2sub([x y],i);

        if alphaImage(yCoord, xCoord) == 0

            % write default value to medoidImage if alpha is zero
            medoidImage(1, i, 1:3) = 0;

        else

        % calculate distances between all values for current pixel
        distances = pdist(squeeze(medoidContainer(i,:,1:3)));

        % convert found distances to matrix of distances
        distanceMatrix = squareform(distances);

        % find index of image with the medoid value
        [~, j] = min(mean(distanceMatrix,2));

        % write found medoid value to medoidImage
        medoidImage(1, i, 1:3) = medoidContainer(i, j, 1:3);

        end

    end

    % replace values larger than one (in alpha channel) by one
    alphaImage(alphaImage > 1) = 1;

    % reshape image to original proportions
    medoidImage = reshape(medoidImage, y, x, 3);

    % save medoid image
    imwrite(medoidImage, [outputFolder 'medoid_modified.png'], 'Alpha', alphaImage);

end

我没有包括整个代码,只是这个函数(为了简洁起见),如果有人需要更多(为了更好地理解它),请让我知道,我会包括它。

最佳答案

当你调用ind2sub时,你给出了大小[x y],但是实际的alphaImage的大小是[y x],所以你不能用xCoordyCoord索引正确的位置。

10-07 21:40