# How to use DCT to compare images?

I want to compare 2 images X and Y, and I have the DCT results dct(X), dct(Y) (but not the original image).

The simplest distance metric I come up with is just pixel-wise difference, say $$\sum |x_{ij}-y_{ij}|$$. Are there any methods to use DCT results to directly compute this distance without converting them back to spatial domain ?

Also, I'd be happy to have a distance metric which is shift/rotation invariant.

---------edits---------

I'm comparing images rastered from polygons (which is from a gds/oasis design layout), so there is no illuminance issues. • DCT blockwise or global ?
– user67664
Sep 19 at 13:47
• opencv's default dct method, i thought it was global. @YvesDaoust Sep 19 at 16:22

"Comparing two images" is a bit too vague. What do you compare for? Two identical photographs, say of the identical scene containing a cat, exposed differently, will have shifted histograms, and will be very different in your metric. But if you underexpose all your images, then a picture of a cat and of the ocean will both be very grey-in-grey and have very low difference. So, I wonder what it is that you actually are trying to describe with that number "difference".

Anyways, math.

I want to compare 2 images X and Y, and I have the DCT results dct(X), dct(Y) (but not the original image).

Well, since the DCT is invertible, you do have the original image. If any operation was necessary to be done in the spatial domain, you could at any time just idct(dct(X)) and get over with it. (The IDCT isn't really a computationally heavy thing.)

That being said, your metric happens to be the entry-wise $$p$$-norm for $$p=1$$. Note that the DFT is a linear operation; if we introduce $$Z=X-Y$$, then $$\operatorname{DCT}(X)-\operatorname{DCT}(Y)=\operatorname{DCT}(Z)$$, and your $$\|X-Y\|_1=\|Z\|_1 = \|\operatorname{IDCT}(\operatorname{DCT}(X))-\operatorname{IDCT}(\operatorname{DCT}(Y))\|_1$$.

Furthermore, if we go from $$p=1$$ to the Frobenius norm, Parseval's theorem kicks in (the DCT being an orthogonal and thus unitary operator) and tells us that the energy in the frequency and spatial domains are identical, so that $$\|Z\|_F = \alpha\|\operatorname{DFT}(Z)\|_F, \quad \alpha\text{ const.}$$; note that the Frobenius norm often is a bit more useful, as large abberations get weighed quadratically; it's simply the square root of the sum of all quadratic entries of $$Z$$ (i.e., root sum squared error!). The takeaway here is that for this norm, it doesn't matter whether you calculate it on the DCT of the image or the image itself.

Also, I'd be happy to have a distance metric which is shift/rotation invariant.

You'd want to look into specifically rotationally invariant descriptors, then, like SIFT/RIFT / PCA-SIFT / SURF… but that makes an assumption on you wanting to compare pictures of physical objects. That might not at all be the case here– maybe you want to compare scattering plane images of some accelerator experiment's particle detector sensors, or radar velocity/distance plane images, or…

• I'm comparing rastered images of polygons, so no exposure issues Sep 20 at 1:23
• I wish you'd have mentioned that in the original version of your post! So, what's the purpose of this comparison? Finding intellectual property infringements? Detecting defects? Finding orientation so that dies can be cut, probed or such? Sep 20 at 6:54
• The purpose is to cluster possible defect positions, thus reducing the manual check workload (similar parts do not need to be checked again) Sep 20 at 9:40
• ah but that means what you really want to do is compare a photograph (pixel image) to a layout (vector image); in that case, don't start by converting the vector image (which contains accurate representation of where corners, edges, etc are) to pixels. Instead, I'd try to extract every corner and every edge that I see in the photograph, and compare these with the positions of corners and edges that are accurately described by the vector description. Sep 20 at 9:50