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 480×640. 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.
sum(X, n)summarizes all elements of a matrixXacross the n-th dimension, so this:really does the following:
Id,pixelsbeing the indices of the extracted values:matrix
Idis imlicitly converted to a column vector, and the resulting sub-matrix is of the same size aspixels(each value being equal toId(p), wherepis the corresponding element inpixels).weightselement-by-element (note that the multiplication operator is.*)You can look into the official documentation for more information.
EDIT: A Simple Example
Let’s assume that:
that is,
Idis a 2-D matrix, andpixelsis 3-D. Now,Id(pixels)would yield:because if you convert
Idto a column vector (tryId(:)),70is the 4th element,40is the 6th, and80is the 8th. Note that the result has the same dimensions aspixels(notId!).Hope that helps!