2014-02-25 11 views
1

C++ 프로그램에서 BMP 이미지를 정확히 회전시키는 방법에 대한 질문은 Stack Overflow에 게시되었습니다. 그러나 지금, 나는 나의 진보에 관해서 더 보여줄 것이있다.C++ 방식의 이미지 회전

방법 (또는 왜) 궁금 내 프로그램은 내가 이미지 계산을 한 후 출력하지 이미지는 : 사람이 어떤 도움이 포인터가있는 경우

void BMPImage::Rotate45Left(float point1, float point2, float point3) 
{ 
    float radians = (2 * 3.1416*45)/360; 

    float cosine = (float)cos(radians); 
    float sine = (float)sin(radians); 

    float point1Xtreme = 0; 
    float point1Yearly = 0; 
    float point2Xtreme = 0; 
    float point2Yearly = 0; 
    float point3Xtreme = 0; 
    float point3Yearly = 0; 

int SourceBitmapHeight = m_BIH.biHeight; 
int SourceBitmapWidth = m_BIH.biWidth; 

point1Xtreme = (-m_BIH.biHeight*sine); 
point1Yearly = (m_BIH.biHeight*cosine); 
point2Xtreme = (m_BIH.biWidth*cosine - m_BIH.biHeight*sine); 
point2Yearly = (m_BIH.biHeight*cosine + m_BIH.biWidth*sine); 
point3Xtreme = (m_BIH.biWidth*cosine); 
point3Yearly = (m_BIH.biWidth*sine); 

float Minx = min(0, min(point1Xtreme, min(point2Xtreme, point3Xtreme))); 
float Miny = min(0, min(point1Yearly, min(point2Yearly, point3Yearly))); 
float Maxx = max(point1Xtreme, max(point2Xtreme, point3Xtreme)); 
float Maxy = max(point1Yearly, max(point2Yearly, point3Yearly)); 

int FinalBitmapWidth = (int)ceil(fabs(Maxx) - Minx); 
int FinalBitmapHeight = (int)ceil(fabs(Maxy) - Miny); 
FinalBitmapHeight = m_BIH.biHeight; 
FinalBitmapWidth = m_BIH.biWidth; 
int finalBitmap; 

, 그것은 좋은 것입니다. 나는 그것을 언급해야한다 :

  • 나는이 프로그램의 목적을 위해 다른 외부 라이브러리를 사용할 수 없습니다
  • 그것은 메뉴 시스템을
+1

코드가 전혀 회전하지 않는 것 같아서 이미지 출력이 예상 된 결과가 아닐 수 있습니다. – drescherjm

+0

BTW, 위의 게시 요지는 우리가 당신을 위해 숙제를하러 온 것이 아닙니다. 문제를 해결하려고 시도하지만 예상대로 작동하지 않는 실제 코드를 게시하면 도움이됩니다. 게시 한 코드는 이미지 회전을 시작하지 않습니다. 회전시킬 이미지조차 없다. – drescherjm

답변

1

이미지 변환을 가지고 작은 이미지 처리 프로그램입니다 일반적으로 대상 픽셀을 원본 픽셀에 투영 한 다음 해당 대상 픽셀의 값을 계산하여 수행됩니다. 이렇게하면 다양한 보간 방법을 쉽게 통합 할 수 있습니다.

template <typename T> 
struct Image { 
    Image(T* data, size_t rows, size_t cols) : 
     data_(data), rows_(rows), cols_(cols) {} 
    T* data_; 
    size_t rows_; 
    size_t cols_; 
    T& operator()(size_t row, size_t col) { 
     return data_[col + row * cols_]; 
    } 
}; 

template <typename T> 
T clamp(T value, T lower_bound, T upper_bound) { 
    value = std::min(std::max(value, lower_bound), upper_bound); 
} 

void rotate_image(Image const &src, Image &dst, float ang) { 
    // Affine transformation matrix 
    // H = [a, b, c] 
    //  [d, e, f] 

    // Remember, we are transforming from destination to source, 
    // thus the negated angle. 
    float H[] = {cos(-ang), -sin(-ang), dst.cols_/2 - src.cols_*cos(-ang)/2, 
       sin(-ang), cos(-ang), dst.rows_/2 - src.rows_*cos(-ang)/2}; 


    for (size_t row = 0; row < dst.rows_; ++row) { 
     for (size_t col = 0; col < dst.cols_; ++cols) { 
      int src_col = round(H[0] * col + H[1] * row + H[2]); 
      src_col = clamp(src_col, 0, src.cols_ - 1); 
      int src_row = round(H[3] * col + H[4] * row + H[5]); 
      src_row = clamp(src_row, 0, src.rows_ - 1);    

      dst(row, col) = src(src_row, src_col); 
     } 
    } 
} 

위의 방법은 임의의 각도로 이미지를 회전시키고 가장 가까운 이웃 보간을 사용합니다. 나는 그것을 stackoverflow에 직접 입력 했으므로 버그가 가득합니다. 그럼에도 불구하고 개념은 거기에있다.