急急!!!!!!!图像如何饶某点旋转呀???(100分)

  • 主题发起人 主题发起人 ljmljm
  • 开始时间 开始时间
L

ljmljm

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]怎么处一个图片通过用鼠标拖动的方式让图片饶某点(20.34)自由的旋转.最好图片的模式是24bit
 
Delphi中的基本图象处理代码
[图像的旋转和翻转]

以下代码用ScanLine配合指针移动实现,用于24位色,至于其它色深的处理自行改写!

//旋转90度
procedure Rotate90(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Height;
Bmp.Height := Bitmap.Width;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine;
Inc(rowOut,Height - j);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;

//旋转180度
procedure Rotate180(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Width;
Bmp.Height := Bitmap.Height;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[Height - j];
Inc(rowOut,Width - i);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;

//旋转270度
procedure Rotate270(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Height;
Bmp.Height := Bitmap.Width;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[Width - i];
Inc(rowOut,j);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;

任意角度旋转就是将原图上的像素映射到新图上,以下代码为了好理解,并未进行充分优化!但速度上已经比较快
了!我的另一篇文章中对优化问题进行了些简单说明,也就是尽量少用浮点运算!

前面为判断部分,如果是90,180,270这类特殊角度,将调用上面的代码,这样效率更高!

//任意角度
function RotateBitmap(Bitmap:TBitmap;Angle:Integer;BackColor:TColor):TBitmap;
var
i,j,iOriginal,jOriginal,CosPoint,SinPoint : integer;
RowOriginal,RowRotated : pRGBTriple;
SinTheta,CosTheta : Extended;
AngleAdd : integer;
begin
Result:=TBitmap.Create;
Result.PixelFormat := pf24bit;
Result.Canvas.Brush.Color:=BackColor;
Angle:=Angle Mod 360;
if Angle<0 then Angle:=360-Abs(Angle);
if Angle=0 then
Result.Assign(Bitmap)
else if Angle=90 then
begin
Result.Assign(Bitmap);
Rotate90(Result);//如果是旋转90度,直接调用上面的代码
end
else if (Angle>90) and (Angle<180) then
begin
AngleAdd:=90;
Angle:=Angle-AngleAdd;
end
else if Angle=180 then
begin
Result.Assign(Bitmap);
Rotate180(Result);//如果是旋转180度,直接调用上面的过程
end
else if (Angle>180) and (Angle<270) then
begin
AngleAdd:=180;
Angle:=Angle-AngleAdd;
end
else if Angle=270 then
begin
Result.Assign(Bitmap);
Rotate270(Result);//如果是旋转270度,直接调用上面的过程
end
else if (Angle>270) and (Angle<360) then
begin
AngleAdd:=270;
Angle:=Angle-AngleAdd;
end
else
AngleAdd:=0;
if (Angle>0) and (Angle<90) then
begin
SinCos((Angle + AngleAdd) * Pi / 180, SinTheta, CosTheta);
if (SinTheta * CosTheta) < 0 then
begin
Result.Width := Round(Abs(Bitmap.Width * CosTheta - Bitmap.Height * SinTheta));
Result.Height := Round(Abs(Bitmap.Width * SinTheta - Bitmap.Height * CosTheta));
end
else
begin
Result.Width := Round(Abs(Bitmap.Width * CosTheta + Bitmap.Height * SinTheta));
Result.Height := Round(Abs(Bitmap.Width * SinTheta + Bitmap.Height * CosTheta));
end;
CosTheta:=Abs(CosTheta);
SinTheta:=Abs(SinTheta);
if (AngleAdd=0) or (AngleAdd=180) then
begin
CosPoint:=Round(Bitmap.Height*CosTheta);
SinPoint:=Round(Bitmap.Height*SinTheta);
end
else
begin
SinPoint:=Round(Bitmap.Width*CosTheta);
CosPoint:=Round(Bitmap.Width*SinTheta);
end;
for j := 0 to Result.Height-1 do
begin
RowRotated := Result.Scanline[j];
for i := 0 to Result.Width-1 do
begin
Case AngleAdd of
0:
begin
jOriginal := Round((j+1)*CosTheta-(i+1-SinPoint)*SinTheta)-1;
iOriginal := Round((i+1)*CosTheta-(CosPoint-j-1)*SinTheta)-1;
end;
90:
begin
iOriginal := Round((j+1)*SinTheta-(i+1-SinPoint)*CosTheta)-1;
jOriginal := Bitmap.Height-Round((i+1)*SinTheta-(CosPoint-j-1)*CosTheta);
end;
180:
begin
jOriginal := Bitmap.Height-Round((j+1)*CosTheta-(i+1-SinPoint)*SinTheta);
iOriginal := Bitmap.Width-Round((i+1)*CosTheta-(CosPoint-j-1)*SinTheta);
end;
270:
begin
iOriginal := Bitmap.Width-Round((j+1)*SinTheta-(i+1-SinPoint)*CosTheta);
jOriginal := Round((i+1)*SinTheta-(CosPoint-j-1)*CosTheta)-1;
end;
end;
if (iOriginal >= 0) and (iOriginal <= Bitmap.Width-1)and
(jOriginal >= 0) and (jOriginal <= Bitmap.Height-1)
then
begin
RowOriginal := Bitmap.Scanline[jOriginal];
Inc(RowOriginal,iOriginal);
RowRotated^ := RowOriginal^;
Inc(RowRotated);
end
else
begin
Inc(RowRotated);
end;
end;
end;
end;
end;

虽然这已经实现了图像任意角度的旋转,但注意看看上面的代码,都是用Round取整,也就是在原图中找最近的像
素填充到新图中,这样在边缘会产生锯齿!如果想效果更好,应该是用四个像素的值运算后得到新像素的值!

如对应到原图的坐标是(6.3,8.6),用第一种方法可能就用(6,9)填充了该像素,但如果想消除边缘锯齿,就应该用
(6,8)、(6,9)、(7,8)、(7,9)运算得出新点的像素,而边缘就用颜色等于背景色的点代替!

感兴趣的朋友可以找些二次线性插值资料看看,或者以后再讨论,这里就不多说了!

后面的浮雕滤镜效果其实就用到了线性插值的思想!

//水平翻转
procedure FlipHorz(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Width;
Bmp.Height := Bitmap.Height;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[j];
Inc(rowOut,Width - i);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;

//垂直翻转
procedure FlipVert(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Height := Bitmap.Height;
Bmp.Width := Bitmap.Width;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[Height - j];
Inc(rowOut,i);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;

其实旋转90、180、270度,水平垂直翻转的原理基本类似,所以也可以改写为一个过程!

如果灵活的使用指针,减少用ScanLine的次数,代码效率还能有所提升~~~

[亮度、对比度、饱和度的调整]

function Min(a, b: integer): integer;
begin
if a < b then
result := a
else
result := b;
end;

function Max(a, b: integer): integer;
begin
if a > b then
result := a
else
result := b;
end;
 
转Image 任意角度的旋转
调用方法:
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;
*************8
----把一个点绕原点旋转α角度后,新的坐标位置与原坐标位置的
关系是:

X=x?cosα-y?sinα
Y=x?sinα+y?cosα
例如要把位图顺时针旋转90度,坐标变换公式为:X=-yY=x

----把这一公式用到Image构件上,显示位图的主要问题是Image构
件显示的位图只有一个象限,并且x、y坐标也是互相颠倒的,为了
解决这个问题,必须在Image构件上建立一个新的坐标原点。下面就
举例说明。

----1.新建一工程project1,在form1上添加image1、image2、
image3、image4,其Autosize属性设为True,image1用来显示原
图,image2、image3、image4分别用来显示旋转90度、180度和270
度后的图像。双击image1,选定一幅bmp图。

----2.添加Button1、Button2、Button3和Button4按钮,其
caption属性分别为&quot;原图&quot;、&quot;旋转90度&quot;、&quot;旋转180度&quot;、
&quot;旋转270度&quot;。

----3.编写&quot;旋转90度&quot;按钮的OnClick事件。

procedureTForm1.Button2Click(Sender:TObject);
var
i,j:integer;
begin
//确定旋转后位图的大小
image2.Picture.Bitmap.Height:=image1.picture.width;
image2.Picture.Bitmap.Width:=image1.picture.height;
fori:=0toimage1.Heightdo
forj:=0toimage1.Widthdo
image2.canvas.Pixels[(-i+image1.Height),
j]:=image1.canvas.Pixels[j,i];
end;


----4.编写&quot;旋转180度&quot;按钮的OnClick事件。

procedureTForm1.Button3Click(Sender:TObject);
var
i,j:integer;
begin
//确定旋转后位图的大小
image3.Picture.Bitmap.Height:=image1.picture.Height;
image3.Picture.Bitmap.Width:=image1.picture.Width;
fori:=0toimage1.Heightdo
forj:=0toimage1.Widthdo
image3.canvas.Pixels[(image1.Width
-j),(image1.Height-i)]:=image1.canvas.Pixels[j,i];
end;

----5.编写&quot;旋转270度&quot;按钮的OnClick事件。代码和步骤3相
似,只需要用image4替换image2,然后用以下的语句替换步骤3for
循环中的原有的语句。

image4.canvas.Pixels[i,(image1.Width-j)]:=image1.canvas.Pixels[j,i];

procedure Rotate(Bmp,Dst:TFastRGB;cx,cy:Integer;Angle:Extended);
var
cAngle,
sAngle: Double;
xDiff,
yDiff,
xpr,ypr,
ix,iy,
px,py,
x,y: Integer;
Tmp: PFColor;{what means?}
begin
Angle:=-Angle*Pi/180;
sAngle:=Sin(Angle);
cAngle:=Cos(Angle);
xDiff:=(Dst.Width-Bmp.Width)div 2;
yDiff:=(Dst.Height-Bmp.Height)div 2;
Tmp:=Dst.Bits;{what means?}
for y:=0 to Dst.Height-1 do
begin
py:=2*(y-cy)+1;
for x:=0 to Dst.Width-1 do
begin
px:=2*(x-cx)+1;
xpr:=Round(px*cAngle-py*sAngle);
ypr:=Round(px*sAngle+py*cAngle);
ix:=((xpr-1)div 2+cx)-xDiff;
iy:=((ypr-1)div 2+cy)-yDiff;
if(ix>-1)and(ix<Bmp.Width)and(iy>-1)and(iy<Bmp.Height)then
Tmp^:=Bmp.Pixels[iy,ix]; {what means?}
Inc(Tmp);
end;
Tmp:=Pointer(Integer(Tmp)+Dst.Gap); {what means?}
end;
end;

原理:
cos(Alpha), sin(Alpha), 0
只需要用源矩阵乘以 -sin(Alpha),cos(Alpha), 0
0, 0, 1

如果你发现转过来的图形带有很整齐的花点,解决的办法是反向计算,即从目标求的源点的坐标和像素值。
以上的例子就是这样的。

如果真的按下面下矩阵计算每个点,目标区有一些点会是白点(因为有些源点通过计算和四舍五入在目标中凑到一起了),我以前解决的办法是从目标求的源点的坐标和像素值,不过首先要取到目标区的区域(往往是斜的)。
cos(Alpha), sin(Alpha), 0
-sin(Alpha),cos(Alpha), 0
0, 0, 1

下载我说的控件吗,有现成的例子及DEMO!

http://www.crosswinds.net/~khojasteh/delphi-components.html

TRotateImage v1.21
This component is a visual component similar to TImage with ability to rotate the image in any arbitrary angle. TRotateImage can be used on Delphi 3, 4, and 5.
 
那能不能通过鼠标时事的的旋转啊!麻烦你们想一下!
 

Similar threads

D
回复
0
查看
793
DelphiTeacher的专栏
D
D
回复
0
查看
828
DelphiTeacher的专栏
D
D
回复
0
查看
660
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部