1

Screenshot

Hey, I'd like to reflect a triangle over symmetry line parallel to one of the sides using just point coordinates and Mat::at() in OpenCV. So I simply reflect Point2d P over a line that consists triangle vertices P1,P2.

The problem is that I get some weird patterns and I can't get rid of them. I used doubles to enhance precision but it didn't help.

Any ideas?

if(P1.x-P2.x) //P1P2 is not vertical
{
    double a=((P2.y-P1.y)/static_cast<double>(P2.x-P1.x));
    double b=P1.y-a*P1.x;    //symmetry line y=ax+b
    double a1=0,b1=0;
    if(a)
    {
        a1=-1/a; // y=a1x+b1 perpendicular to y=ax+b
        b1=P.y-a1*P.x;
        double xc=(b1-b)/(a-a1); // ax+b=a1x+b1
        P.x= 2*xc-P.x;
        P.y=a1*P.x+b1;
    }
    else    //P1P2 is horizontal
    {
        P.y=2.0*P1.y-P.y;
        
    }
}
else  //P1P2 is vertical
{
    
    P.x=2*P1.x-P.x;
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • An image is just a sequence of values.. When applying a perspective projection, usually you don't get all pixels projected or they don't keep adjacent to each other. You can check in this link the same phenomena: http://www.manifold.net/doc/radian/re-projection_creates_a_new_image.htm – Ja_cpp Jan 25 '18 at 09:04
  • instead of computing pixel positions "forward", typically image transformations go the other way, they iterate over all pixels from the destination image (or destination region) and compute the corresponding image position of the source image and either choose the nearest pixel value or interpolate between different pixel values. – Micka Jan 25 '18 at 10:52
  • Ok I tried to do the transformations the other way and patterns dissapeared, so thank you! :) but still there is some loss of information.. Is there any possibility to not to lose the quality? – JackieHendrixon Jan 25 '18 at 14:54
  • Your code iterates over source pixels and rounding issue do not cover all pixels in destination. Change code to iterate over destination pixels and base on that find source pixel(s) and this will solve your problem. – Marek R Mar 11 '21 at 11:03
  • Or instead doing it by your self perform respective affine transformation open CV delivers: https://docs.opencv.org/master/dd/d52/tutorial_js_geometric_transformations.html – Marek R Mar 11 '21 at 11:20

0 Answers0