基本图象处理代码(1)

[图像的旋转和翻转] 数据挖掘研究院

以下代码用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[i];
      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.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[Height - j];
      Inc(rowOut,i);
      rowOut^ := rowIn^;
      Inc(rowIn);
    end;
  end;
  Bitmap.Assign(Bmp);
end;

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

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

[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:由图像的灰度化看基本图像处理(2)
下一篇:由图像的灰度化看基本图像处理(3)
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • GDI+简介
  • COM与DCOM的区别与联系
  • 使用Delphi解析XML 文档
  • 如何设置delphi/cbuilder/BDE/MSSQL
  • BORLAND在“迫害”程序员?
  • 将image的图片保存为JPG格式图片方法
  • InstallShieldExpressfordelphi制作安装程
  • Real Programmers Use Pascal
  • 关于在COM中使用可选参数的研究
  • TStrings的AddObject方法应用
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • BORLAND在“迫害”程序员?
  • 李维:我的回忆和一些有趣的事(精彩绝伦)
  • 李维看.net和DELPHI6(含李维照片)
  • 《代码大全》电子版1.01发布了
  • Real Programmers Use Pascal
  • Kylix安装手记
  • Borland与Microsoft关于Delphi的对话
  • InstallShieldExpressfordelphi制作安装程
  • 关于在COM中使用可选参数的研究
  • msagent经典用法
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静