gpt4 book ai didi

c - C 中的旋转算法?

转载 作者:行者123 更新时间:2023-11-30 17:24:17 25 4
gpt4 key购买 nike

我想在图像上应用角度 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;
}

最佳答案

如果你在任何有精细细节的东西上使用它,就会产生楼梯、莫尔图案和其他 block 状图案。您想使用插值来混合最接近计算坐标的像素。而不仅仅是四舍五入。

关于c - C 中的旋转算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27279830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com