Creating a Vector from Average Image Values

I'm trying to create a vector of the averages of a 3x3 kernel for each layer of an image. I have the following code where I have initialized red/green/blueAvg as 0, and red/green/blue as an empty vector [ ]. I have checked the debugger and the vector values seem to be the right size, at 1x296, after only the for y = ... loop is run but once the for x = ... loop is exited, the vector value increases to 1x48544....Any ideas? (For more info: red/green/blueDoub are equal to double values at that layer (instead of uint8))
for x = 2:row-1
for y = 2:col-1
redAvg = mean(mean(redDoub(x - 1: x +1, y - 1: y + 1)));
greenAvg = mean(mean(greenDoub(x - 1: x +1, y - 1: y + 1)));
blueAvg = mean(mean(blueDoub(x - 1: x +1, y - 1: y + 1)));
red = [red redAvg];
green = [green greenAvg];
blue = [blue blueAvg];
end
end

 採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 2 日

1 投票

That sounds about right. 48544 is 296 * 164, so you are probably working with a 298 x 166 matrix and appending one entry for each interior member of the matrix. This is what you say you want to do, since you asked to create a vector for each layer of an image.
Have you considered simplifying your code?
redavg = conv2(redDoub, ones(3,3)/9, 'same');
greenavg = conv2(greenDoub, ones(3,3)/9, 'same');
blueavg = conv2(blueDoub, ones(3,3)/9, 'same');
red = reshape( redavg(2:end-1, 2:end-1), 1, []);
blue = reshape( blueavg(2:end-1, 2:end-1), 1, []);
green = reshape( greenavg(2:end-1, 2:end-1), 1, []);

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by