# intensity transformation 16-bit image to a 8-bit image

Give an intensity transformation function T for converting a 16-bit image to a 8-bit image, i.e. T takes an integer from {0,1,2,...,65535} and returns an integer from {0,1,2,...,255}.

Can we use $s = T(r) = \left \lfloor \sqrt{r}\right \rfloor$?

• Without further specification, you could just return zero for any input. That is a fine function. It would turn all images black, however. – Ross Millikan Apr 8 '13 at 14:20
• I wildly guess you'll prefer a linear transformation. – leonbloy Apr 8 '13 at 14:25

You can certainly use whatever you'd like. However, this won't produce an even-looking image. Why? Look at a table of what input maps to what output: $$\begin{array}{|c|c|}\hline \text{Input} & \text{Output} \\ \hline 1 & 1 \\ \hline \vdots & \vdots \\ \hline 4 & 2 \\ \hline \vdots & \vdots \\ \hline 9 & 3 \\ \hline \vdots & \vdots \\ \hline 16 & 4 \\ \hline \vdots & \vdots \\ \hline 25 & 5 \\\hline \end{array}$$

Ok. So that was interesting and trivial. What does this mean? Notice that the input maps $3$ digits to $1$, $5$ digits to $2$, $7$ digits to $3$, etc. This indicates that the image will be more weighted in the lighter/higher colors.

Another way to look at this is to say that the ideal image transformation would have the midpoint be the same on both. That is, if you plug $\frac{2^{16}}{2}$ into the transformation, you'd get back $\frac{2^8}{2}$. The same goes for all other denominators: $\frac{2^{16}}{4} \to \frac{2^8}{4}$, etc.

You may want to look at using the $\log_2(x)$ function... (not sure how this would pan out, but it could work).

While the full story obviously depends on exactly what the samples in the original 16-bit image represent, and how the output 8-bit image is going to be used, in the absence of more specific instructions, the only sane thing is to scale the samples linearly -- which in this case means simply chopping off the eight least significant bits of each sample.

More technically, the sample values in digital images relate to light intensity by power laws of varying exponents and complexity -- but by convention these are almost always specified as a combination of

1. Scale the digital sample values affinely so they fall into the range 0.0 to 1.0.

2. Apply a power function (whose exponent is conventionally know as the "gamma" of the samples), possibly with some special piecewise definitions near 0 to avoid dealing with unbounded derivatives.

3. Multiply the result with the desired "full" light output of the display device.

Thus, if you want to produce a set of 8-bit samples that work more-or-less like the original ones under this procedure -- and if you don't know any better, assume that the same gamma is used in both cases -- you should aim to have the first of these steps approximate the output of the first step applied to the original sample as well as possible. That calls for a linear scaling.

In some cases, the initial affine mapping maps a restricted range of the sample space into $[0,1]$, such as mapping 0 through 16 to 0.0, 224 through 255 to 1.0 and the range between them interpolated linearly. However, if you don't have specific information about this, the best you can do is still to scale linearly.

And if what you have are color samples in a non-RGB color space, the samples will typically be converted to RGB for display using linear algebra on the raw samples -- in that case doing anything but a linear rescaling will disturb the color reconstruction.