Search code examples
c#imageimage-processingemgucvimage-scaling

How to transform an image to another coordinates?


I am trying to find duplicate videos in my database, and in order to do so I grab two frames from a videos pair, resize them to the same width and height and then compare both images pixel by pixel.

I have a case where images from the a videos pair are like below:

enter image description here ----- enter image description here

These are actually the same videos (images), but because of the aspect ratio of the videos (16:9, 4:3 .. etc) the result is negative when comparing pixel by pixel (no match).

If my standard is 50x50, how can I transform any Region Of Interest to 50x50?

For the above example:

Pixel [5,0] shall be [0,0]

Pixel [45,0] shall be [50,0]

Pixel [5,50] shall be [0,50]

Pixel [45,50] shall be [50,50]

and all other pixels are transformed


Solution

  • Encouraged by OP that pseudo-code can be helpful ....
    I have no knowledge about "emgucv", so I will answer in pseudo-code.

    Definition

    Let SRC be a source image - to be read.
    Let DST be a destination image - to be written.

    Both SRC and DST are 2D-array, can be accessed as ARRAY[int pixelX,int pixelY].

    Here is the pseudo-code :-

    input : int srcMinX,srcMinY,srcMaxX,srcMaxY;
    float linearGra(float dst1,float dst2,float src1,float src2,float dst3){
        return ( (dst3-dst1)*src2+  (dst2-dst3)*src1) / (dst2-dst1);
    };
    for(int y=0;y<50;y++){     //y of DST
        for(int x=0;x<50;x++){ //x of DST
            float xSRC=linearGra(0,50,srcMinX,srcMaxX,x);
            float ySRC=linearGra(0,50,srcMinY,srcMaxY,y);
            DST[x,y]=SRC[round(xSRC),round(ySRC)];  //find nearest pixel
        }
    }
    

    Description

    The main idea is to use linear-interpolation.

    The function linearGra takes two points in a 2D graph (dst1,src1) and (dst2,src2) .

    Assuming that it is a linear function (it is true because scaling+moving is linear function between SRC and DST coordinate), it will find the point (dst3,?) that lying in the graph.

    I used this function to calculate pixel coordinate in SRC that match a certain pixel in DST.

    Further work

    If you are a perfectionist, you may want to :-

    • bounded the index (xSRC,ySRC) - so it will not index-out-of-bound
    • improve the accurary :-
      I currently ignore some pixels (I use Nearest-neighbor w/o interpolation).
      The better approach is to integrate all involved SRC's pixel, but you will get a-bit-blurry image in some cases.

    You may also be interested in this opencv link (not emgucv).