I have been trying to calculate the correlation coefficient $(\rho)$ of two variables, and noticed that in cases where either $var(X)$ or $var(Y)$ are very small, the correlation coefficient calculation gives incorrect results, due to the fact that the covariance is coming out too small.
In fact, in the limit where say $x_i = \mu_x$ for all $i$ ($\mu_x$ is the true mean), both the numerator and denominator in the correlation coefficient become zero.
As an example, take $X,Y$ points scattered along a straight line with thickness $\epsilon$. Clearly, as $\epsilon\to0$, $|\rho|\to1$, meaning that one would expect:
$$\left|\text{Cov}(X,Y)\right|\approx \sqrt{\text{var}(X)\text{var}(Y)}$$ De-facto, I'm getting that the covariance is much smaller. As an example, here is an image of pixels for which I want to calculate $\rho$, with the shade of gray giving the weight.
My calculations give:
$$\sigma_x = 0.50, \quad \sigma_y = 52.25$$ $$\text{cov(X,Y)} = 4.67 \ll \sqrt{\text{var}(X)\text{var}(Y)} = 26.11$$ Meaning that I get a $\rho$ of $0.18$, while I'd expect $\rho\approx 1$. This problem doesn't occur whenever the line is not oriented to one of the axes, e.g. when the variances are comparable.
Are there any methods for calculating the covariance that overcome this problem?