当前位置: 首页 > 知识库问答 >
问题:

获取正确的多边形以在图像中显示

吉栋
2023-03-14

现在真的需要你的帮助

我有一些检测结果。

我希望在这个未旋转的图像上显示这个结果(请忽略蓝色框)

我的算法逻辑如下:

假设黄色框的坐标是参考参照系(图像1的左上角),我试图找到黄色框相对于图像1内部图像左上角参照系的坐标。

所以我使用了基本的trigno(图片1右下角的直角三角形,来获得内部图片左上角的坐标,并根据这个点找到黄色方框的坐标。

在这之后,由于我想在图像2上显示结果,我使用了内部图像的中心作为旋转点,并将黄色框相对于该中心的坐标平移到内部图像的原点(左上角),然后使用此矩阵进行旋转:

我在图像上的结果多边形是这样的:

我的教授看了一眼,说这是错误的。

我已经记不清自己经历了多少次逻辑和实现...在我看来这是正确的。有好心人能帮我吗?

以下是我的部分代码:

135度旋转图像的代码

 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();
        }
}

共有1个答案

苏嘉志
2023-03-14

太多的代码需要详细追踪,但我假设您的代码忠实地实现了您描述的数学。我认为问题在于您正在应用一个纯旋转矩阵。从旋转的kitty到旋转的黄色框的转换是一个平移和一个旋转的组合。您需要首先将kitty(和黄色框)从(x, y)转换为(0,0)(因此旋转的kitty图像是与kitty的左上角在图1左上角的位置)。然后您需要将所有内容顺时针旋转135度。

使用齐次坐标和矩阵乘法将使代码更加简单。

 类似资料:
  • 问题内容: 有谁知道一种从点到给定距离内获取MySQL数据库中所有多边形的方法?实际距离不是那么重要,因为它是稍后为找到的每个多边形计算的,但是对“接近”的多边形进行该计算将是一个巨大的优化。 我已经看过MBR并包含函数,但是问题是某些多边形不包含在围绕该点绘制的边界框中,因为它们很大,但是它们的某些顶点仍然很接近。 有什么建议么? 问题答案: 慢速版本(无空间索引): 要使用空间索引,您需要对表

  • 但是宽度和高度较低的图像成功地加载到图像视图中! 有人能告诉我代码有什么问题吗?我做错了什么?我只想从库中导入相机图像,并在图像视图中显示它们!

  • 问题内容: 我想从s3获取图像并将其显示在HTML页面上。 Angular HTML文件: Angular Controller文件: 我找到了一个叫做 FileReader的 东西并尝试了这个: 但是它显示错误: Uncaught TypeError:无法在’FileReader’上执行’readAsDataURL’:参数1的类型不是’Blob’。 请使用代码帮助我在img标签中显示图像文件 我

  • 我已经了解了如何使用PIL检测图像中的边缘(图像大部分是白色背景和黑色绘图标记)。如何检测包含这些边的矩形,以便裁剪图像。 例如,我想裁剪如下内容: 成: 或者这个: 成: 我熟悉PIL中的裁剪,但不知道如何围绕对象自动居中。 我已通过执行以下操作来检测边缘: 如何得到包含所有这些边的矩形?

  • 所以我猜没有注册的ImageReader?我怎么能通过Jaspersoft Studio修复这样的东西? 编辑:我尝试使用java.io.InputStream作为这里建议的类类型,但结果是相同的错误。算是吧。一个很大的区别是,在Jaspersoft Studio中,如果出现错误,您可以将图像设置为显示为空白。如果我使用java.awt.Image,那么这个设置什么也不做。我仍然得到一个错误,报告