【发布时间】:2016-06-01 19:48:37
【问题描述】:
我想旋转我的图像。我从文件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);
}
【问题讨论】:
-
我很确定全大写对您的情况没有帮助。你应该添加一个实际的问题;目前,这只是“我想做图像旋转,这是我的代码”。它不工作吗?什么不起作用?它应该怎么做?它做了什么?
-
它确实有效,但在输出中它只是给出原始图像!对于 transform1.txt 它应该将输入图像旋转 45 度,对于 transform2.txt 它应该进行投影变换,但它会将输出作为输入图像本身
-
将其添加到问题中 - 你可以edit它。
-
我开始格式化你的代码,这真是一团糟,但我变得太累了。真是一团糟,难怪你不知道它在做什么。
标签: c image image-processing