图像旋转(80分)

  • 主题发起人 主题发起人 noall
  • 开始时间 开始时间
N

noall

Unregistered / Unconfirmed
GUEST, unregistred user!
图你围绕着某个点进行一定角度的旋转??
 
通过公式
x = x' cos a + y' sin a
y = - x' sin a + y' cos a
把结果位图的像素影射回原来位图,通过插值得到结果位图的像素颜色。
 
为什么不检索一下呢,这样的问题很多的.
 
我以前写的《JPEG、BMP等数字图像的快速处理(二)》的片断,在我的主页上
http://winprogram.yeah.net 的我的文章里有全文
procedure rotate(sur:pchar;bmp:Tbitmap;cx,cy:integer;angle:integer);
var
x1,y1,x3,y3 :integer //x1,y1 为平移坐标 x3,y3为旋转的新坐标
x,y,h,w,widthbytes,dst,v:integer;
hcos,hsin:double;
tmp:PFcolor;
hu:double;
begin
hu:=angle*3.1415/180;
hcos:=cos(hu); hsin:=sin(hu);
h:=bmp.Height; w:=bmp.Width ;
widthbytes:=integer(bmp.scanline[0])-integer(bmp.scanline[1]);
dst:=integer(bmp.ScanLine[h-1]);
fillchar(pchar(dst)^,widthbytes*h,0); //使图像全黑
for y:=0 to h-1 do
begin
y1:=y -(h-cy); //平移下的y
for x:=0 to W-1 do
begin
x1:=x-cx; //平移下的x
x3:=round(x1*hcos-y1*hsin);
y3:=round(x1*hsin+y1*hcos); //平移下的新坐标
x3:=x3+cx;
y3:=y3+(h-cy); //转换回原坐标
if (x3>=0) and (x3<w-1) and (y3>=0) and (y3<h-1) then
pfcolor(dst+x*3+y*widthbytes)^:=
pfcolor(integer(sur)+x3*3+y3*widthbytes)^;
end;
end;
end;
angle为旋转的角度,cx、cy为旋转中心, cx=width div 2,cy=height div 2即以图像的中心为旋转中心。此程序处理速度快,可处理大图片。
 
noall:
给你一个图像旋转算法。

调用方法:
bmp_rotate(Image1.Picture.Bitmap, Image2.Picture.Bitmap, RAngle);

procedure TfrmColor.bmp_rotate(src,dst:tbitmap;angle:extended);
var
c1x,c1y,c2x,c2y:integer;
p1x,p1y,p2x,p2y:integer;
radius,n:integer;
alpha:extended;
c0,c1,c2,c3:tcolor;
begin
//将角度转换为PI值
angle := (angle / 180) * pi;
// 计算中心点,你可以修改它
c1x := src.width div 2;
c1y := src.height div 2;
c2x := dst.width div 2;
c2y := dst.height div 2;

// 步骤数值number
if c2x < c2y then
n := c2y
else
n := c2x;
dec (n,1);

// 开始旋转
for p2x := 0 to n do begin
for p2y := 0 to n do begin
if p2x = 0 then
alpha:= pi/2
else
alpha := arctan2(p2y,p2x);
radius := round(sqrt((p2x*p2x)+(p2y*p2y)));
p1x := round(radius * cos(angle+alpha));
p1y := round(radius * sin(angle+alpha));

c0 := src.canvas.pixels[c1x+p1x,c1y+p1y];
c1 := src.canvas.pixels[c1x-p1x,c1y-p1y];
c2 := src.canvas.pixels[c1x+p1y,c1y-p1x];
c3 := src.canvas.pixels[c1x-p1y,c1y+p1x];

dst.canvas.pixels[c2x+p2x,c2y+p2y]:=c0;
dst.canvas.pixels[c2x-p2x,c2y-p2y]:=c1;
dst.canvas.pixels[c2x+p2y,c2y-p2x]:=c2;
dst.canvas.pixels[c2x-p2y,c2y+p2x]:=c3;
end;
application.processmessages
end;
end;
 
多人接受答案了。
 
to David Jiang
我很想了解一些图像编程的知识,可否提供点资料! 谢了。

QQ:7216199
 
后退
顶部