我想旋转我的图像。我正在从文件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
将此问题添加到问题中 - 您可以[编辑]它。 –