我想在图像上应用角度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;
};
这是我的功能:
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;
}
最佳答案
如果将其用于任何细微的细节,则会产生楼梯,莫尔条纹和其他块状现象。您想使用插值来混合最接近计算坐标的像素,而不仅仅是四舍五入。
关于c - C?中的旋转算法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27279830/