C语言中的旋转算法?
我想在图像上应用角度alpha的旋转,但我的函数没有给出预期的结果。 我必须将其用于OCR项目 我使用了以下结构:C语言中的旋转算法?,c,image,rotation,C,Image,Rotation,我想在图像上应用角度alpha的旋转,但我的函数没有给出预期的结果。 我必须将其用于OCR项目 我使用了以下结构: struct pixel { int red; int blue; int green; int alpha; }; struct image_bw { int *img; int w; int h; }; struct image_rgb { int h; int w; struct pixel* img; }; 这是我的功能: vo
struct pixel
{
int red;
int blue;
int green;
int alpha;
};
struct image_bw
{
int *img;
int w;
int h;
};
struct image_rgb
{
int h;
int w;
struct pixel* img;
};
这是我的功能:
void rotation_tab(int alpha,struct image_bw* img)
{
int w = img->w;
int h = img->h;
float radians = (2*3.1416*alpha)/360;
float cosine = (float)cos(-radians);
float sine = (float)sin(-radians);
float Point1x=(h*sine);
float Point1y=(h*cosine);
float Point2x=(w*cosine-h*sine);
float Point2y=(h*cosine+w*sine);
float Point3x=(w*cosine);
float Point3y=(w*sine);
float minx=fmin(0,fmin(Point1x,fmin(Point2x,Point3x)));
float miny=fmin(0,fmin(Point1y,fmin(Point2y,Point3y)));
float maxx=fmax(0,fmax(Point1x,fmax(Point2x,Point3x)));
float maxy=fmax(0,fmax(Point1y,fmax(Point2y,Point3y)));
int destX = (int)ceil(fabs(maxx-minx));
int destY = (int)ceil(fabs(maxy-miny));
int *tab = malloc((sizeof(int))*destX*destY);
int midx = w/2;
int midy = h/2;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int xt = x - midx;
int yt = y - midy;
int xs = (int)round((cosine*xt - sine*yt) + midx);
int ys = (int)round((sine*xt + cosine*yt) + midy);
if ((xs >= 0) && (xs<w) && (ys>=0) && (ys<h))
{
tab[xs+ys*destX] = (img->img)[x+y*w];
}
}
}
img->w = destX;
img->h = destY;
img->img = tab;
}
void rotation\u选项卡(int alpha,结构图像\u bw*img)
{
int w=img->w;
inth=img->h;
浮动弧度=(2*3.1416*alpha)/360;
浮点余弦=(浮点)余弦(-弧度);
浮点正弦=(浮点)正弦(-弧度);
浮点1X=(h*正弦);
浮点1y=(h*余弦);
浮点2x=(w*余弦-h*正弦);
浮点2y=(h*余弦+w*正弦);
浮点3x=(w*余弦);
浮点3y=(w*正弦);
浮点最小值=最小值(0,最小值(点1X,最小值(点2X,点3X));
float miny=fmin(0,fmin(点1y,fmin(点2y,点3y));
float maxx=fmax(0,fmax(点1x,fmax(点2x,点3x));
浮点最大值=fmax(0,fmax(点1y,fmax(点2y,点3y));
int destX=(int)ceil(fabs(maxx minx));
int destY=(int)ceil(fabs(maxy miny));
int*tab=malloc((sizeof(int))*destX*destY);
int midx=w/2;
int midy=h/2;
对于(int x=0;x=0)&(xs=0)&(ysimg)[x+y*w];
}
}
}
img->w=destX;
img->h=destY;
img->img=tab;
}
如果你在任何细节上使用它,都会产生楼梯、莫尔图案和其他块状效果。你想使用插值来混合最接近计算坐标的像素,而不仅仅是四舍五入。你能发布预期和实际的输出吗?我尝试了三乘三矩阵和90度旋转:{10 0;0 1 0;0 0 1}预期结果是:{0 1;0 1 0;1 0 0}但实际结果是:{0 1;0-1956132512 32578;0 1 0}(每次执行程序时的值都不相同)我只是用3x3单位矩阵和360度旋转尝试了它。它产生了一个4x4矩阵