2013-03-13 3 views
0

지금 도움이 필요합니다.이미지에 표시 할 올바른 다각형 가져 오기

몇 가지 감지 결과가 있습니다.

Image 1

나는 (파란색 상자를 무시하십시오)이 해제 회전 된 이미지에이 결과를 표시 할

Image 2

내 알고리즘에 대한 나의 논리는

다음과 같다 노란색 상자의 좌표가 참조 프레임 (이미지 1의 왼쪽 상단 구석)을 기준으로 할 때, 프레임의 참조와 함께 노란색 상자의 좌표를 찾으려고합니다. 이미지 1의 내부 이미지의 왼쪽 상단 모서리에 대한 참조.

그래서 이미지 1의 오른쪽 하단 모서리에 기본 trigno (직각 삼각형)를 사용하여 내부 이미지를 찾고이 점을 참조하여 노란색 상자의 좌표를 찾으십시오.

이미지 2에 결과를 표시하고 싶기 때문에 내부 이미지의 중심을 회전 점으로 사용하고이 중심과 관련하여 노란색 상자의 좌표를 내부의 원점으로 변환합니다.

:

enter image description here [X, y]는 내 포인트

이미지에 내 생성 된 다각형들은 [X, Y는이있다 : 화상 (왼쪽 상단)과이 매트릭스를 이용하여, 회전 enter image description here

내 교수가 그것을 살펴보고 잘못되었다고 말했습니다.

내가 로직과 구현을 몇 번이나 통과했는지 계산을 잃어 버렸습니다 ... 나에게 맞는 것 같습니다. 어떤 영혼이 나를 도와 줄 수 있습니까?

여기 내 코드의 일부 : 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

에서 회전 된 이미지에 대한

// 코드 상세히을 통해 추적 너무 많은 코드입니다,하지만 난 가정합니다 코드를 충실하게 구현하는 당신이 묘사 한 수학. 문제는 당신이 순수 회전 행렬을 적용하고 있다고 생각합니다. 회전 키티에서 회전 된 노란색 상자로의 변환은 이동과 회전의 합성입니다. 키티 (및 노란 상자)를 (x, y)에서 (0,0)으로 변환해야합니다 (회전 된 키티 이미지는 키티의 왼쪽 상단이 그림 1의 왼쪽 상단에 위치합니다). 그런 다음 모든 것을 시계 방향으로 135도 회전시켜야합니다.

동질적인 좌표와 행렬 곱셈을 사용하면 코드가 훨씬 간단 해집니다.

+0

당신이 그것을 놓친 경우에, 나는 내 고소가 실제로 번역과 회전 모두를 사용한다고 설명했다. 그러나 당신이 제안한 것 대신에, 나는 키티 이미지의 중심을 발견하고 그것을 그림 1의 왼쪽 상단 모서리로 번역하고 중심 주위의 노란색 상자 좌표를 번역했습니다. 그 후에 이미지를 225도 반 시계 방향으로 회전 시켰습니다. 이건 당신이 제안한 것과 같은가요? – user1968818

+0

@ user1968818 - 키티 이미지의 왼쪽 상단을 그림 1의 왼쪽 상단으로 번역 할 것을 제안했습니다.나는 회전이 그림 1의 왼쪽 상단에 있어야한다고 언급하는 것을 게을리했다. 나는 이것이 그 기원이라고 생각했다. 센터를 번역하는 경우, 센터를 회전 후 가정 된 위치로 다시 변환하면 작동합니다. 그렇지 않으면 키티 이미지는 그림 1의 왼쪽 상단에 위치하게됩니다. –

관련 문제