user1968818
user1968818

Reputation: 67

Getting the correct polygon to show in image

really need your help now

I have some detection results.

Image 1

and i wish to show this result on this un-rotated image(please ignore the blue boxes)

Image 2

My logic for my algorithm is as follows

Given that the coordinates for the yellow box are with reference to the frame of reference(top left hand corner of Image 1), I am trying to find the coordinates for the yellow box with reference to the frame of reference of the top left hand corner of the inner image within Image 1.

So i used basic trigno(right-angled triangle at the bottom right corner of Image 1, to obtain the coordinates of the top left hand corner of the inner image and find the coordinates of the yellow box with reference to this point.

After which i use since i want to show the results on Image 2, i used the centre of the inner image as my rotation point and translate the coordinate of the yellow box with respect to this centre to the origin of the inner image(top left hand corner) and rotate using this matrix:

enter image description here[x,y] where [x,y] are for my points

My resultant polygon on the image is this:

enter image description here

which my prof took a look at it and said it was wrong.

I have lost count how many times i went through the logic and implementation...it seems correct to me. Can any kind soul help me out here?

Here's part of my code:

//code for rotated image at 135 deg

 else if(nameForcompare.equals("135")){
       angle = 225;
       minPoint.set_y(catYmin); minPoint.set_x(catXmin); maxPoint.set_y(catYmax); maxPoint.set_x(catXmax);
       //Show detection results of rotated image
       g5.setColor(Color.YELLOW);
       g5.drawRect((int)minPoint.get_x(), (int)minPoint.get_y(), (int)(maxPoint.get_x()-minPoint.get_x()), (int)(maxPoint.get_y()-minPoint.get_y()));


       rotatedX = Double.parseDouble(originalWidth)*Math.cos(Math.toRadians((int)45));
         if(catXmin < rotatedX){
              o = imageHeight - catYmin;
              a = rotatedX - catXmin;
              theta = Math.atan(o/a);
              h = (o/Math.sin(theta));

           if(theta >= Math.toRadians((int)45)){   
              thetaZ = theta - Math.toRadians((int)45);

              oZ = h*Math.sin(thetaZ); //ycoordinate
              aZ = h*Math.cos(thetaZ); //xcoordinate

              varX = checkPointsBeforeRotationX(aZ);
              varY = checkPointsBeforeRotationY(oZ);
           }
           else{
              thetaZ = Math.toRadians((int)45) - theta;
              oZ = 0;                  //ycoordinate
              aZ = h*Math.cos(thetaZ); //xcoordinate

              varX = checkPointsBeforeRotationX(aZ);
              varY = checkPointsBeforeRotationY(oZ);                  

           }
              minPoint.set_x(varX);
              minPoint.set_y(varY);

         }
         else if(catXmin == rotatedX){
             theta = Math.toRadians((int)45);
             h = imageHeight - catYmin;

             o = h*Math.sin(theta); //ycoordinate
             a = h*Math.cos(theta); //xcoordinate

             varX = checkPointsBeforeRotationX(a);
             varY = checkPointsBeforeRotationY(o);                

             minPoint.set_y(varY);
             minPoint.set_x(varX);
         }
         else if(catXmin > rotatedX){
             a = imageHeight - catYmin;
             o = catXmin - rotatedX;
             theta = Math.atan(o/a);
             h = (o/Math.sin(theta));
        if(theta <= Math.toRadians((int)45)){     
             thetaZ = theta + Math.toRadians((int)45);

             oZ = h*Math.sin(thetaZ); //ycoordinate
             aZ = h*Math.cos(thetaZ); //xcoordinate

             varX = checkPointsBeforeRotationX(aZ);
             varY = checkPointsBeforeRotationY(oZ);
        }
        else{
             thetaZ = Math.toRadians((int)45) - theta;

             oZ = 0;                  //xcoordinate
             aZ = h*Math.cos(thetaZ); //ycoordinate 

             varX = checkPointsBeforeRotationX(oZ);
             varY = checkPointsBeforeRotationY(aZ);

        }
             minPoint.set_x(varX);
             minPoint.set_y(varY);

         }

         if(catXmax < rotatedX){
              o = imageHeight - catYmax;
              a = rotatedX - catXmax;
              theta = Math.atan(o/a);
              h = (o/Math.sin(theta));
         if(theta >= Math.toRadians((int)45)){        
              thetaZ = theta - Math.toRadians((int)45);

              oZ = h*Math.sin(thetaZ); //ycoordinate
              aZ = h*Math.cos(thetaZ); //xcoordinate

              varX = checkPointsBeforeRotationX(aZ);
              varY = checkPointsBeforeRotationY(oZ);
         }
         else{
              thetaZ = Math.toRadians((int)45) - theta;
              oZ = 0;                  //ycoordinate
              aZ = h*Math.cos(thetaZ); //xcoordinate

              varX = checkPointsBeforeRotationX(aZ);
              varY = checkPointsBeforeRotationY(oZ);  
         }
              maxPoint.set_x(varX);
              maxPoint.set_y(varY);

         }
         else if(catXmax == rotatedX){
             theta = Math.toRadians((int)45);
             h = imageHeight - catYmin;

             o = h*Math.sin(theta); //ycoordinate
             a = h*Math.cos(theta); //xcoordinate

             varX = checkPointsBeforeRotationX(a);
             varY = checkPointsBeforeRotationY(o); 

             maxPoint.set_y(varY);
             maxPoint.set_x(varX);

         }
         else if(catXmax > rotatedX){
             a = imageHeight - catYmax;
             o = catXmax - rotatedX;
             theta = Math.atan(o/a);
             h = (o/Math.sin(theta));
        if(theta <= Math.toRadians((int)45)){      
             thetaZ = theta + Math.toRadians((int)45);

             oZ = h*Math.sin(thetaZ); //ycoordinate
             aZ = h*Math.cos(thetaZ); //xcoordinate

             varX = checkPointsBeforeRotationX(aZ);
             varY = checkPointsBeforeRotationY(oZ);
        }
        else{
             thetaZ = Math.toRadians((int)45) - theta;

             oZ = 0;                  //xcoordinate
             aZ = h*Math.cos(thetaZ); //ycoordinate 

             varX = checkPointsBeforeRotationX(oZ);
             varY = checkPointsBeforeRotationY(aZ);
        }

             maxPoint.set_y(varX);
             maxPoint.set_x(varY);

         }

        getCorners();
        checkPointsAfterRotation(angle);
        checkCornerPointsAfterRotation(angle);

        g2.setColor(Color.MAGENTA);
        g2.drawPolygon(xPoints, yPoints, nPoints);  
        Corners1 =0; Corners2 =0;








public static void rotate2(Point originForRotation, Point pointForRotation, Double angle){
double cos=Math.cos(angle);
double sin=Math.sin(angle);
double oX =originForRotation.get_x();
double oY =originForRotation.get_y(); 
double x=pointForRotation.get_x();
double y=pointForRotation.get_y(); 

x = x-oX;    y = y-oY; 


pointForRotation.set_x((cos*x-sin*y)+oX);
pointForRotation.set_y((sin*x+cos*y)+oY);    

pointForRotation.show();

}

public static void getCorners(){
    if((minPoint.get_x() > maxPoint.get_x()) && (minPoint.get_y() < maxPoint.get_y())){
      topleftPoint.set_x(maxPoint.get_x());  bottomrightPoint.set_x(minPoint.get_x());
      topleftPoint.set_y(minPoint.get_y());  bottomrightPoint.set_y(maxPoint.get_y());
      Corners1 = 1; 
    }
    else if((minPoint.get_x() > maxPoint.get_x()) && (minPoint.get_y() > maxPoint.get_y())){
      toprightPoint.set_x(minPoint.get_x());  bottomleftPoint.set_x(maxPoint.get_x());
      toprightPoint.set_y(maxPoint.get_y());  bottomleftPoint.set_y(minPoint.get_y());
      Corners2 = 1;
    }
    else if((minPoint.get_x() < maxPoint.get_x()) && (minPoint.get_y() < maxPoint.get_y())){
      toprightPoint.set_x(maxPoint.get_x());  bottomleftPoint.set_x(minPoint.get_x());
      toprightPoint.set_y(minPoint.get_y());  bottomleftPoint.set_y(maxPoint.get_y());
      Corners2 = 1;
    }
    else if((minPoint.get_x() < maxPoint.get_x()) && (minPoint.get_y() > maxPoint.get_y())){
      topleftPoint.set_x(minPoint.get_x());  bottomrightPoint.set_x(maxPoint.get_x());
      topleftPoint.set_y(maxPoint.get_y());  bottomrightPoint.set_y(minPoint.get_y());
      Corners1 = 1;
    }
}

public static Double checkPointsBeforeRotationX(Double pointX){
    if(pointX > (Double.parseDouble(originalWidth))){
      pointX = Double.parseDouble(originalWidth);
      }
    return pointX;
}

public static Double checkPointsBeforeRotationY( Double pointY){
    if(pointY > (Double.parseDouble(originalHeight))){
      pointY = Double.parseDouble(originalHeight);
    }
    return pointY;
}

public static void checkPointsAfterRotation(int angle){

        rotate2(origin, minPoint,  Math.toRadians((int)angle));    
        rotate2(origin, maxPoint,  Math.toRadians((int)angle)); 
         //check for out of bound points after rotation
            if(minPoint.get_y()< 0){
                minPoint.set_y(0);
            }
            else if(minPoint.get_y() > Double.parseDouble(originalHeight)){
                minPoint.set_y(Double.parseDouble(originalHeight));
            }
            if(minPoint.get_x()< 0){
                minPoint.set_x(0);
            }
            else if(minPoint.get_x() > Double.parseDouble(originalWidth)){
                minPoint.set_x(Double.parseDouble(originalWidth));
            }
            if(maxPoint.get_y()< 0){
                maxPoint.set_y(0);
            }
            else if(maxPoint.get_y() > Double.parseDouble(originalHeight)){
                maxPoint.set_y(Double.parseDouble(originalHeight));
            }
            if(maxPoint.get_x()< 0){
                maxPoint.set_x(0);
            }
            else if(maxPoint.get_x() > Double.parseDouble(originalWidth)){
                maxPoint.set_x(Double.parseDouble(originalWidth));
            }

        xPoints[0] = (int)minPoint.get_x(); 
        xPoints[2] = (int)maxPoint.get_x(); 

        yPoints[0] = (int)minPoint.get_y(); 
        yPoints[2] = (int)maxPoint.get_y(); 


}


 public static void checkCornerPointsAfterRotation(int angle){

    if(Corners1 == 0 && Corners2 == 1){

        rotate2(origin, toprightPoint,  Math.toRadians((int)angle));    
        rotate2(origin, bottomleftPoint,  Math.toRadians((int)angle));

        if(toprightPoint.get_y()< 0){
                toprightPoint.set_y(0);
            }
            else if(toprightPoint.get_y() > Double.parseDouble(originalHeight)){
                toprightPoint.set_y(Double.parseDouble(originalHeight));
            }
            if(toprightPoint.get_x()< 0){
                toprightPoint.set_x(0);
            }
            else if(toprightPoint.get_x() > Double.parseDouble(originalWidth)){
                toprightPoint.set_x(Double.parseDouble(originalWidth));
            }
            if(bottomleftPoint.get_y()< 0){
                bottomleftPoint.set_y(0);
            }
            else if(bottomleftPoint.get_y() > Double.parseDouble(originalHeight)){
                bottomleftPoint.set_y(Double.parseDouble(originalHeight));
            }
            if(bottomleftPoint.get_x()< 0){
                bottomleftPoint.set_x(0);
            }
            else if(bottomleftPoint.get_x() > Double.parseDouble(originalWidth)){
                bottomleftPoint.set_x(Double.parseDouble(originalWidth));
            }

            xPoints[1] = (int)toprightPoint.get_x(); xPoints[3] = (int)bottomleftPoint.get_x();
            yPoints[1] = (int)toprightPoint.get_y(); yPoints[3] = (int)bottomleftPoint.get_y();
        }
        else if(Corners1 == 1 && Corners2 == 0){


        rotate2(origin, topleftPoint,  Math.toRadians((int)angle));    
        rotate2(origin, bottomrightPoint,  Math.toRadians((int)angle));    

        if(topleftPoint.get_y()< 0){
                topleftPoint.set_y(0);
            }
            else if(topleftPoint.get_y() > Double.parseDouble(originalHeight)){
                topleftPoint.set_y(Double.parseDouble(originalHeight));
            }
            if(topleftPoint.get_x()< 0){
                topleftPoint.set_x(0);
            }
            else if(topleftPoint.get_x() > Double.parseDouble(originalWidth)){
                topleftPoint.set_x(Double.parseDouble(originalWidth));
            }
            if(bottomrightPoint.get_y()< 0){
                bottomrightPoint.set_y(0);
            }
            else if(bottomrightPoint.get_y() > Double.parseDouble(originalHeight)){
                bottomrightPoint.set_y(Double.parseDouble(originalHeight));
            }
            if(bottomrightPoint.get_x()< 0){
                bottomrightPoint.set_x(0);
            }
            else if(bottomrightPoint.get_x() > Double.parseDouble(originalWidth)){
                bottomrightPoint.set_x(Double.parseDouble(originalWidth));
            } 

            xPoints[1] = (int)topleftPoint.get_x(); xPoints[3] = (int)bottomrightPoint.get_x();
            yPoints[1] = (int)topleftPoint.get_y(); yPoints[3] = (int)bottomrightPoint.get_y();
        }
}

Upvotes: 0

Views: 164

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234795

That's too much code to trace through in detail, but I'll assume that your code faithfully implements the math you described. I think the problem is that you are applying a pure rotation matrix. The transformation from rotated kitty to rotated yellow box is a composition of a translation and a rotation. You need to first translate kitty (and the yellow box) from (x,y) to (0,0) (so the rotated kitty image is position with kitty's top left at the top left of figure 1). Then you need to rotate everything clockwise by 135 degrees.

Using homogeneous coordinates and matrix multiplication will make your code a lot simpler.

Upvotes: 1

Related Questions