Tuesday, 10 May 2016

photo editing - Is rotation an intrinsic lossy operation (for angles not multiple of 90 degrees)?


mattdm's statement "that blur is largely reversible if you do the exact same thing in reverse" raised the question in my mind. The rotation is a geometric transformation of the image that consist of a spatial transformation of the coordinates, and an intensity interpolation. If the rotation is not a multiple of 90 degrees then the interpolation algorithm plays a crucial role.


In such cases, if we use an averaging interpolation algorithm (e.g., bicubic) the operation is lossy*. But can we use a different approach (e.g., nearest neightbor interpolation) instead and have our rotated image "un-rotated"?


(*) It is only my feeling (I still cannot support it with a mathematical proof): as we cannot know for sure which pixel contributed to wich value, we cannot reverse the averaging. But I'm not confident that we cannot use probabilistic methods to accurately estimate the original values.




While I lack the required math skills, I have performed some tests myself (with gimp), but after anti-rotating, the images differ:



Test 1


source image


Figure 1 - Source image (256x256)


NN Rotated 9,5 degrees, rotated -9,5 degrees, difference


Figure 2 - From left to right: a) image rotated 9,5 degrees clockwise; b) image rotated again 9,5 degrees anti-clockwise; and c) difference between images. For these operations I have used nearest neightbor interpolation. The images are downscaled after the operations to better fit this website layout.


bicubic Rotated 9,5 degrees, rotated -9,5 degrees, difference Figure 3 - From left to right: a) image rotated 9,5 degrees clockwise; b) image rotated again 9,5 degrees anti-clockwise; and c) difference between images. For these operations I have used bicubic interpolation. The images are downscaled after the operations to better fit this website layout.


Test 2


Following @unapiedra's sugestion, I did a simpler test: rotating a 2x2 matrix. This case is uninteresting because depending upon the angle either all cells are rotated by the same angle, or no cell is rotated. That is, the rotation is always lossless.


So I tried again with a 3x3 matrix and a 30 degrees rotation:


rotating a 3x3 matrix without scaling



Figure 4 - From left to right: a) source image; b) image rotated 30 degrees clockwise; c) image rotated 30 degrees anti-clockwise; d) difference. The images are upscaled to fit this website.


In this case, the differences are evident. The rotation is clearly lossy... But what happens if I upscale before the rotation?


Test 3


rotating a previously upscaled 3x3 matrix


Figure 5 - From left to right: a) source image; b) image upscaled by a 6x factor; c) upscaled image rotated 30 degrees clockwise; d) image rotated 30 degrees anti-clockwise; e) downscaled transformed image; and f) diference (no difference). The images are upscaled to fit this website.


In this case, I'm upscaling by a 6x factor. Reasoning for choosing this factor (unfortunately incorrect as I have seen with a counter-example):


A 30-degrees rotated pixel has coordinates bottom-left to top-right: [0,0]-[0.3660, 1.3660]. That is, the shortest projected side has a 0.36 pixels length. The sampling theorem requires that we sample at a double rate.


Thus, to accurately sample a 30 degrees rotated image, I must sample each 0.17 pixels, yielding a x5.88 resizing factor; 3 x 5.88 = 17,64, thus I resample the source image to a 18x18 image.



Answer



Image rotation is a lossy operation, but rotating an image once then rotating it back likely loses very little detail, especially compared to typical JPEG compression.





Image rotation works like this mathematically:


A grey level image consists of luminance values L_(x,y) at integer pixel positions x, y. First a real-argument function f(x,y) is constructed that reproduces the values L_(x,y) at the same x,y positions, but will also give values at non-integer x,y. Hence it's interpolating between integer x,y positions (this is where interpolation methods come in---there are several possible choices for f(x,y)).


Then construct a rotated version g(x,y) = f( x cos(a) - y sin(a), x sin(a) + y cos(a) ) by angle a. This operation is mathematically lossless, not counting the numerical roundoff errors when performing the calculation on a finite precision computer.


Finally g(x,y) values are calculated at integer x,y positions again to create an image.




At this point you can ask: why is this lossy? Can't we just reverse all these calculations to reconstruct the original unrotated L_(x,y), if we know what interpolation method was used to construct f?


Theoretically that is possible, but this is not what happens when you do a rotation of the same angle in the opposite direction. Instead of reversing the original rotation operations precisely, an opposite sign rotation is performed using the same sequence of operations that the initial rotation used. The back-rotated image will not be precisely the same as the original.


Furthermore, if the values of g(x,y) were rounded to a low precision (8-bit, 0..255), the information loss is even greater.





Lots of accumulated rotations will effectively blur the image. Here's an example of rotating a 500 by 500 pixel Lena image 30 times by 12 degrees, amounting to a full 360 degree rotation:


enter image description here




There's another reason why blur-type operations will be lossy. One might naively think that mathematically reversing a blur should give us back the original unblurred image. This is theoretically true for as long as we're working with infinite precision. The procedure is called deconvolution and it is being used in practice to sharpen images that are blurry due to motion blur or optical reasons.


But there is one catch: blurring is insensitive to small changes in the source image. If you blur two similar images, you get even more similar results. De-blurring is very sensitive to small changes: if you de-blur two only slightly different images, you get two very different results. We're usually not working high precision (8 bit is quite low precision actually), and the roundoff errors will get magnified when attempting to reverse a blur.


This is the reason why blurring is "irreversible" and why it is losing detail.


No comments:

Post a Comment

Why is the front element of a telephoto lens larger than a wide angle lens?

A wide angle lens has a wide angle of view, therefore it would make sense that the front of the lens would also be wide. A telephoto lens ha...