我想旋轉我的圖像。我正在從文件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);
}
我很確定ALL UPPERCASE並沒有幫助你的情況。你應該添加一個實際的問題;此刻,這只是「我想要做圖像旋轉,這是我的代碼」。它不起作用嗎?什麼不行?它應該做什麼?它做什麼呢? –
它確實工作,但在輸出它只是給原始圖像!對於transform1.txt,它應該將輸入圖像旋轉45度,對於transform2.txt,它應該進行投影變換,但是它將輸出作爲輸入圖像本身添加 – yadoabhi
將此問題添加到問題中 - 您可以[編輯]它。 –