Reputation: 119
I have a Matlab Code Snippet and I try to write in C++, but I really don't understand what is happening there:
for c = 1:3
Id = double(I(:,:,c))/255;
Wc(:,:,c) = sum(Id(pixels).*weights, 3);
end
There is an image I of size 480x640x3. In the first Iteration the first channel of the image is saved in Id, which then has a size of 480x640. But what is happening in the next line? I just don't understand that syntax.
pixels and weights are of size 300x383x4x1.
So what does this line exactly do?:
sum(Id(pixels).*weights, 3);
Thank you.
Upvotes: 0
Views: 593
Reputation: 32920
sum(X, n)
summarizes all elements of a matrix X
across the n-th dimension, so this:
sum(Id(pixels) .* weights, 3);
really does the following:
Id
, pixels
being the indices of the extracted values: Id
is imlicitly converted to a column vector, and the resulting sub-matrix is of the same size as pixels
(each value being equal to Id(p)
, where p
is the corresponding element in pixels
).weights
element-by-element (note that the multiplication operator is .*
)You can look into the official documentation for more information.
Let's assume that:
Id = [10 20; 30 40; 50 60; 70 80];
pixels(:, :, 1) = [4 4; 4 4];
pixels(:, :, 2) = [6 6; 6 6];
pixels(:, :, 3) = [8 8; 8 8];
that is, Id
is a 2-D matrix, and pixels
is 3-D. Now, Id(pixels)
would yield:
ans(:, :, 1) =
70 70
70 70
ans(:, :, 2) =
40 40
40 40
ans(:, :, 3) =
80 80
80 80
because if you convert Id
to a column vector (try Id(:)
), 70
is the 4th element, 40
is the 6th, and 80
is the 8th. Note that the result has the same dimensions as pixels
(not Id
!).
Hope that helps!
Upvotes: 1