2016-02-20 81 views
-1

我想旋轉我的圖像。我正在從文件transform1(應旋轉圖像45度)和transform2(做投影變換)的輸入矩陣。這些矩陣用於我必須遵循的逆映射算法。程序工作,但給出的輸出原始圖像,而不是旋轉它。圖像旋轉(雙線性插值和最近鄰)

void ntransform1 (char * filename, image * img) 
{ 
    int i,j,x,y,n,m; 
    float det=0; 
    double** mat = malloc(1000000 * sizeof(double*)); 

    for (i = 0; i < 1000000; ++i) 
     mat[i] = malloc(4 * sizeof(double)); 

    FILE *file; 
    file=fopen(filename, "r"); 

    for (i = 0; i < 3; i++) 
    { 
     for (j = 0; j < 3; j++) 
     { 
      if (!fscanf(file, "%lf", &mat[i][j])) break; 
      // mat[i][j] -= '0'; 
      // printf("%lf\n",mat[i][j]); 
     } 
    } 
    // printf("%lf\n",mat[1][1]); 
    det = (mat[0][0]*mat[1][1]) - (mat[0][1]*mat[1][0]) 
     - (mat[0][0]*mat[1][2]*mat[2][1]) 
     + (mat[2][0]*mat[0][1]*mat[1][2]) 
     + (mat[0][2]*mat[1][0]*mat[2][1]) 
     - (mat[0][2]*mat[1][1]*mat[2][0]); 

    // inverse of matrix is 
    mat[0][0] = (mat[1][1]-(mat[1][2]*mat[2][1]))/det; 
    mat[0][1] = -(mat[0][1]-(mat[0][2]*mat[2][1]))/det; 
    mat[0][2] = ((mat[0][1]*mat[1][2])-(mat[0][2]*mat[1][1]))/det; 
    mat[1][0] = -(mat[1][0]-(mat[1][2]*mat[2][0]))/det; 
    mat[1][1] = (mat[0][0]-(mat[0][2]*mat[2][0]))/det; 
    mat[1][2] = -((mat[0][0]*mat[1][2])-(mat[0][2]*mat[1][0]))/det; 
    mat[2][0] = ((mat[1][0]*mat[2][1])-(mat[1][1]*mat[2][0]))/det; 
    mat[2][1] = -((mat[0][0]*mat[2][1])-(mat[0][1]*mat[2][0]))/det; 
    mat[2][2] = ((mat[0][0]*mat[0][2])-(mat[0][1]*mat[1][0]))/det; 

    for(y=0;y<img->y;y++) 
    { 
     for(x=0;x<img->x;x++) 
     { 
      double xnew,ynew; 
      // calculating the new rotated pixel value 
      xnew = (float)(((x-(img->x)/2)) 
       * (mat[0][0]))+((y-(img->y)/2) 
       * (mat[0][1]))+mat[0][2]+(img->x)/2; 
      ynew = (float)(((x-(img->x)/2)) 
       * (mat[1][0]))+((y-(img->y)/2) 
       * (mat[1][1]))+mat[1][2]+(img->y)/2; 
      m = (int)round(xnew); 
      n = (int)round(ynew); 
      image2[n][m]; 
     } 
    } 
} 

void bitransform1 (char * filename, image * img) 
{ 
    int i,j; 
    float det=0; 
    /*matrix*/ 

    double** mat = malloc(1000000 * sizeof(double*)); 
    for(i=0;i<1000000;++i) 
     mat[i]=malloc(4*sizeof(double)); 

    FILE *file; 
    file=fopen(filename, "r"); 

    for (i = 0; i < 3; i++) 
    { 
     for (j = 0; j < 3; j++) 
     { 

      if (!fscanf(file, "%lf", &mat[i][j])) break; 
      // mat[i][j] -= '0'; 
      // printf("%lf\n",mat[i][j]); 
     } 
    } 
    // printf("%lf\n",mat[1][1]); 
    det = (mat[0][0]*mat[1][1])-(mat[0][1]*mat[1][0])-(mat[0][0]*mat[1][2]*mat[2][1])+(mat[2][0]*mat[0][1]*mat[1][2])+(mat[0][2]*mat[1][0]*mat[2][1])-(mat[0][2]*mat[1][1]*mat[2][0]); 

    // inverse of matrix is 
    mat[0][0]=(mat[1][1]-(mat[1][2]*mat[2][1]))/det; 
    mat[0][1]=-(mat[0][1]-(mat[0][2]*mat[2][1]))/det; 
    mat[0][2]=((mat[0][1]*mat[1][2])-(mat[0][2]*mat[1][1]))/det; 
    mat[1][0]=-(mat[1][0]-(mat[1][2]*mat[2][0]))/det; 
    mat[1][1]=(mat[0][0]-(mat[0][2]*mat[2][0]))/det; 
    mat[1][2]=-((mat[0][0]*mat[1][2])-(mat[0][2]*mat[1][0]))/det; 
    mat[2][0]=((mat[1][0]*mat[2][1])-(mat[1][1]*mat[2][0]))/det; 
    mat[2][1]=-((mat[0][0]*mat[2][1])-(mat[0][1]*mat[2][0]))/det; 
    mat[2][2]=((mat[0][0]*mat[0][2])-(mat[0][1]*mat[1][0]))/det; 


int y,x,m,n; 
double xfrac,yfrac,gray_new; 

for(y=0;y<img->y;y++) 
{ 
    for(x=0;x<img->x;x++) 
    { 

     double xnew,ynew; 
// caclulating the new rotated pixel value 
    xnew=(((x-(img->x)/2))*(mat[0][0]))+((y-(img->y)/2)*(mat[0][1]))+mat[0][2]+(img->x)/2; 
    ynew=(((x-(img->x)/2))*(mat[1][0]))+((y-(img->y)/2)*(mat[1][1]))+mat[1][2]+(img->y)/2; 
    m=(int)floor(xnew); 
    n=(int)floor(ynew); 
    xfrac=xnew-m; 
    yfrac=ynew-n; 

// calculating the 4 neighbors of the pixel 
if (m >= 0 && m+1 < img->x && n >= 0 && n+1 < img->y) 
{ 
    gray_new = (1.0 - yfrac)*(1.0 - xfrac)*image1[n][m] + (1.0-xfrac)*(yfrac) *image1[n][m+1] + (1.0-yfrac)*(xfrac)*image1[n+1][m] + xfrac*yfrac *image1[n+1][m+1]; 

    image2[y][x] = (unsigned char)gray_new; 
     } 
else if (m+1 == img->x && n >= 0 && n < img->y || n+1 == img->y && m >= 0 && m <img->x) 
{ 
    //image2[y][x] = image1[n][m]; 
     } 
else 
{ 
    image2[y][x] = 255; 
     } 
} 
    } 
    fclose(file); 
}  
+0

我很確定ALL UPPERCASE並沒有幫助你的情況。你應該添加一個實際的問題;此刻,這只是「我想要做圖像旋轉,這是我的代碼」。它不起作用嗎?什麼不行?它應該做什麼?它做什麼呢? –

+0

它確實工作,但在輸出它只是給原始圖像!對於transform1.txt,它應該將輸入圖像旋轉45度,對於transform2.txt,它應該進行投影變換,但是它將輸出作爲輸入圖像本身添加 – yadoabhi

+0

將此問題添加到問題中 - 您可以[編輯]它。 –

回答

0

你不會對你的目標圖像image2做任何事情。基本上,代碼應該是這樣的:

for (y = 0; y < img->y; y++) { 
    for (x = 0; x < img->x; x++) { 
     int X = (x - img->x/2) * c - (y - img->y/2) * s + img2->x/2; 
     int Y = (x - img->x/2) * s - (y - img->y/2) * c + img2->y/2; 

     if (X >= 0 && X < img2->x && Y >= 0 && Y <=img2->y) { 
      img2->data[y][x] = img->data[Y][X]; 
     } 
    } 
} 

其中xy是目標圖像img2XY的像素座標源圖像img的像素座標。

你的代碼中有許多特點:

  • 你爲了保持3 × 3旋轉矩陣分配1000000個× 4雙打兩級載體。爲什麼你想要使用3d旋轉矩陣呢? (我收集第三列是翻譯矢量。)
  • 什麼是image和`image2?他們是全球陣列嗎?像素值不應該是圖像結構的一部分嗎?
  • 您的代碼將從模塊化中受益:編寫函數以讀取矩陣;另一個反轉它的功能等等。目前,你有很多重複的代碼。
+0

是image1是一個用於拍攝輸入圖像的數組,而image2用於拍攝輸出圖像......我必須使用雙線性插值,所以我必須拍攝4個相鄰像素,在這種情況下您還沒有拍攝 – yadoabhi

+0

此語句:'image2 [n] [m];'什麼也沒做。你必須以某種方式分配給'image2'。儘管如此,你可以在第二個函數中這樣做。你是否嘗試過調試並找到你計算出的座標是什麼? –