# simple explanation of rank transform and its relation to normal distribution

I tried to understand the rank transform, but I couldn't.

The first step common to all histogram remapping techniques is the transformation of the pixel intensity values of the given image via the rank transform.

The rank transform is basically a histogram equalization procedure which renders the histogram of the given image in such a way that the resulting histogram approximates the uniform distribution.
Here, each pixel value in an $$N$$-dimensional image $$I(x,y)$$ is replaced with the index (or rank) $$R$$ the pixel would correspond to if the image pixels were ordered in an ascending manner.
For example, the most negative pixel value is assigned a ranking of $$1$$ while the most positive value is assigned a ranking of $$N$$.

Once the rank $$R$$ of each image pixel is determined, the general mapping function to match the target distribution $$f(x)$$ may be calculated from $$\frac{N-R+0.5}{N}=\int\limits^t_{x=-\infty} f(x)\,\mathrm dx$$, where the goal is to find $$t$$.

Obviously, the right hand side represents the target cumulative distribution function (CDF) while the left hand side represents a scalar value. $$f(x)$$ can be the normal distribution function $$f(x)=\frac1{\sigma\sqrt{2\pi}}\exp\left({\frac{-(x-\mu)^2}{2\sigma^2}}\right)$$.

• hey, so what's the question? Also, it's a bit unusual for pixels to have negative values, but it really wouldn't matter here, I think. However, I must say, your description doesn't make much sense to me: it basically is no transform at all if I, for example, assume pixel values from 0 to 255, then (as long as each value is used at least once in the image, which is pretty likely if the distribution is actually normal and your image is large), then 0 gets replaced with 0, 1 with 1, 2 with 2... 255 with 255. That's not a useful transform, are you sure this is what the book you're reading says? Aug 29, 2021 at 20:48
• Which text is this definition of the rank transform from? It seems pretty likely to me that you get better help if you name your sources! (as always) Aug 29, 2021 at 20:53

What the paper omits is that for the rank transform to be possible, the pixel magnitudes need to be strictly orderable – there mustn't be two equal pixels. This is typically cannot the case for digital imagery, since the a picture of bit depth $$b$$ allows for $$2^b$$ (e.g. $$2^{16}=65536$$) values, but most images a larger than that (e.g. even a tiny 640×360 px image has more than 65536 pixels), which means there's more pixels than possible values – some values have to be used multiple times.

There's tricks to still deal with such images is to increase their bit depth as necessary, and either do something naive (like adding a pixel-position dependend small value to each pixel) to make each pixel unique, or add noise (typically with a variance on the order of 1 or 2 least significant bits of the original image).

However, your formula to transform a uniform random variable to another distribution (e.g. normal) is not quite right; measure-theoretically, you'd need to integrate differently (you'd need a different measure than what you mean with $$\mathrm dx$$, and you'd need to integrate about the image of the inverse of your normal distribution pdf). Also, makes no sense to begin with, because the output of your rank transform is a discrete uniform random variable (a rank is a very discrete thing!), and the integral only becomes necessary/helpful for continously distributed random variables. So, double bad approach, I'm afraid :(

Let's derive this real quick ourselves, you'll be surprised how easy it is. being a bit stricter with notation helps us. Random variables (RV) are always capital letters, when we describe the PDF or CDF of an RV, we note that RV as index.

Let $$X$$ be an RV with discrete CDF $$F_X(x) := P(X \le x)$$.

We want to find an RV $$Y$$ by transforming $$X$$ with a (invertible) function $$g(\cdot)$$, i.e. $$Y:=g(X)$$ such that its CDF is equal to some desired function $$h$$, i.e. $$F_Y(y) = h(y)$$.

\begin{align} F_Y(y) &=P(Y\le y)\\ &=P(g(X) \le y)\\ &=P(X \le g^{-1}(y))\\ &=F_X(g^{-1}(y))\\ &\overset!= h(y) \end{align}

Now, for the discrete uniform on $$\{1,\ldots,N\}$$, the CDF $$F_X$$ is especially simple:

$$F_X(x) = x\cdot \frac1N.$$

Inserting that into the last equation above

\begin{align} h(y) &=F_X(g^{-1}(y))\\ &=g^{-1}(y)\cdot \frac1N\\ N\cdot h(y) &= g^{-1}(y)\\ g(y) &= (N\cdot h)^{-1}(y)\\ &= h^{-1}\left(\frac yN\right) \end{align}

OK, there it is: if you want your output CDF to be $$h$$, then the method to transform your rank-transformed pixel values is dividing them by $$N$$ and then applying the inverse of your desired CDF as function.

(thus, the normal CDF itself doesn't help at all – you can't derive the inverse of the PDF from that, as it doesn't have a closed form. This is something you'll need to approximate numerically.)

• I have extracted the wavelet features from an image, and I need to remap them such that they follow the normal or the lognormal distribution, how can I do this?
– Noha
Aug 31, 2021 at 17:25
• I say that, quite extensively, in my answer! Could you expand on what you don't understand / why you can't apply it that to your problem? Aug 31, 2021 at 17:31
• If there's anything I can clarify about my answer, I'd be happy – I don't want to have wasted the time spent on writing it! Sep 4, 2021 at 10:57
• I think that I have picked the idea of the paper. After the ranking step, suppose that rank 1 is assigned to the largest value in the feature matrix which needs to be mapped to normal distribution, and rank N to the smallest value in the matrix. The equation in the paper subtracts the rank of the value from the total number of elements in the matrix (N), and then divide by N.
– Noha
Sep 5, 2021 at 4:39
• If N=256, then with R=1 (corresponding to the largest value), the left hand side of the equation will be approximately 1. The left hand side of the equation represents a probability value, since it is obtained from the inverse of the cumulative density function (CDF). Thus, the value of t obtained with rank R=1 will represent the probability that the values of the random variable X lies in the range from minus infinity to t. Since the probability obtained is approximately 1 , then t must correspond to the largest value.
– Noha
Sep 5, 2021 at 4:40