If I understand your question correctly then you need to do this:
scaleVert:=(rectHeight/rectWidth > canvasHeight/canvasWidth);
if scaleVert then
scale:=canvasHeight/rectHeight
else
slace:=canvasWidth/rectWidth;
The value of scale is ratio by which you have to scale your image.
The Boolean variable scaleVert is used to test whether you have to fit your rectangle vertically (i.e., the height of rectangle after scaling will be the same as the height of the canvas) or horizontally.
Notice that if rectHeight/rectWidth = canvasHeight/canvasWidth
then it does not matter whether you scale vertically or horizontally - you obtain the same result.
(The two rectangles are similar.)
If the rectangle is "thinner" (i.e., the ratio rectHeight/rectWidth
is higher) you have to scale vertically and otherwise you have to scale horizontally.
To plot the histograms, try something like this:
Nbins = 256;
figure;
subplot(3,1,1);
hist( vec(im(:,:,1)), Nbins );
title( 'Channel R' );
subplot(3,1,2);
hist( vec(im(:,:,2)), Nbins );
title( 'Channel G' );
subplot(3,1,3);
hist( vec(im(:,:,3)), Nbins );
title( 'Channel B' );
To estimate the probability that a pixel is within some range of values, find the number pixels that are within that range and divide them by the total number of pixels:
length( find( vec(img(:,:,1)) < X & vec(img(:,:,1)) > Y ) ) / size(img,1) / size(img,2)
If you want to change the mean of the pixels, I'd first just bias them and clip any values under 0 or over 255. If that's good enough, then you're done. Otherwise, you'll have to come up with some method for taking the clipping into account. To change the variance, just scale the pixels rather than bias them. There may be a better way to do this that I am not aware of.
In the above, the vec()
function would be defined by
function y = vec(x)
y = x(:);
Best Answer
It's simply because
$$ \frac{1}{\sqrt{2 \pi} \sigma} \cdot \frac{1}{\sqrt{2 \pi} \sigma} = \frac{1}{(\sqrt{2 \pi})^2 \sigma^2} = \frac{1}{2 \pi \sigma^2} $$
More generally, observe that the $d-$dimensional (isotropic) gaussian is given by
$$ g_{\sigma}(\mathbf{x}) = \frac{1}{(\sqrt{2 \pi} \sigma)^d} e^{-||\mathbf{x}||^2 / 2 \sigma^2} $$ where $||x||^2 = x_1^2 + x_2^2 + \dots + x_d^2$ is the $\ell_2$ norm. This can be written as the product of $d$ 1-dimensional gaussians, since
$$ \frac{e^{-||\mathbf{x}||^2 / 2 \sigma^2}}{(\sqrt{2 \pi} \sigma)^d} = \frac{e^{-(x_1^2 + x_2^2 + \dots + x_d^2) / 2 \sigma^2}}{(\sqrt{2 \pi} \sigma)^d} = \prod_{i=1}^d \frac{e^{-(x_i^2) / 2 \sigma^2}}{\sqrt{2 \pi} \sigma} = g_{\sigma}(x_1) \cdot g_{\sigma}(x_2) \cdot \dots g_{\sigma}(x_d) $$