Computing the derivative of a transformation matrix I am trying to find a geometric transformation between two images, where the transformation is a simple scaling matrix. So, if I denote the two image functions as $r$ and $f$ and the scaling matrix as $S$, I am trying to minimize the following cost function (CF) with respect to the scaling parameters:
$$
CF = 0.5 * (r(x) - f(S(x)))^{2}
$$
So, I can take the derivative as follows:
$$
CF' = (r(x) -f(S(x))) * f'(S(x)) * S'(x) 
$$
where $f'(S(x))$ is the gradient of the transformed $f$ image according to the scaling matrix and $S'(x)$ is the derivative of the scaling matrix with respect to the scaling parameters, which is simple to compute.
Now, I have the following question: 


*

*The image is sampled at various pixel locations and I can use this formula to compute the gradient of the cost function at every pixel location. However, if I want to get the global gradient. Should I sum up these individual gradients to get the global gradient to use in the optimisation?

 A: It appears that your function $r(x)$ gives the content of image $R$ at the two-dimensional position $x$, and $f(x)$ similarly gives the content of image $F$.  Furthermore I will assume the scaling parameters are reals, since we are taking derivatives with respect to them, and so $f$ is somehow capable of giving the content of its image, as well as the gradient of the image with respect to the scaling parameters, at non-grid locations.  The images are presumably black and white or gray level images, since the cost depends on the squared difference between image contents at corresponding locations.
The cost function is probably not intended to be a function of $x$, but rather the total squared difference between the images, i.e. a sum over $x$.  This would mean that the gradient should also be summed over $x$.  But if the sum is, say, over the entire image $R$, then you need to be sure that $S$ won't map some of the $x$ values to locations outside image $F$, or if it does, you need to be sure your cost function is doing what you want it to for those portions of $R$.  Similarly, portions of $F$ could remain "uncovered" if the scaling $S$ does not make $R$ as big as $F$ -- again, you should make sure the cost function is measuring whatever it is you really want to achieve.
Finally, note that using gradient-based methods to optimize such a match between images is extremely likely to get stuck in a local optimum, unless your images each show one giant fuzzy dot.
