-------------如何取得相连的像素点?大虾帮看看-----------

雄牛 2013-07-15 04:36:22
现在小弟在学图片处理,路过的大虾帮看一下..

是这样的:
有一堆record,是从整个图片是取下来的,上面有坐标和颜色,格式:
x,y,color

有两个问题想请教:
1.如何判断在同一颜色下,相连(上下左右)的相素点?
2.如何将相连的像素点的数据抽出,存到别一个记录里?

(仅有35分了,路过的帮看看..)
...全文
224 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
雄牛 2013-11-16
  • 打赏
  • 举报
回复
引用 1 楼 HSFZXJY 的回复:
广度优先搜索:

uses Classes;

TPixel =record    //定义点类型
          x,y:integer;
          color: TColor;
        end;
{Canvas为原图画布,pt为起始搜索点,返回一个与pt颜色相同的点的列表}
function (Canvas: TCanvas;pt:TPixel): TList;
const
  direct :array [1..4,1..2] of shortint =((0,-1),(0,1),(-1,0),(1,0)); //方向增量
var
  dl: array [1..10000] of TPixel;  //队列
  head,tail,i:Integer;  //head为队首指针,tail为队尾指针
  tmp,tmp2:TPixel;
begin
  head:=0;  tail:=1; FillChar(dl,sizeof(dl),0); dl[tail]:=pt; //初始化队列
  result := TList.Create;
  while head<>tail do
  begin
    Inc(head);  //队首加1
    tmp:=dl[head];  //出队
    for i:= 1 to 4 do  //遍历四个方向
    begin
      tmp2.X :=tmp.X+direct[i,1];
      tmp2.Y :=tmp.Y+direct[i,2];
      if (tmp2.X<0)or(tmp2.X>Canvas.Width)or(tmp2.Y<0)or(tmp2.Y>Canvas.Width) then 
        continue;   //如果越界则跳过
      tmp2.Color := Canvas.Pixels[tmp2.X,tmp2.Y];
      if tmp2.Color = pt.Color then  //如果颜色相同则入队,并加入结果中
      begin
        Inc(tail);
        dl[tail]:=tmp2;
        result.Add(@tmp2);
      end;
    end; 
  end;
end;
请问大虾,如何把结果输出到Stringlist? 我怎么改也实现不了?
hsfzxjy 2013-08-05
  • 打赏
  • 举报
回复
是那个FillChar么。。
雄牛 2013-08-05
  • 打赏
  • 举报
回复
引用 9 楼 HSFZXJY 的回复:
对呀就这样。。
编译可以,但运行后,执行到那个函数时,第一行就报错了,内存错误
hsfzxjy 2013-08-05
  • 打赏
  • 举报
回复
对呀就这样。。
hsfzxjy 2013-08-05
  • 打赏
  • 举报
回复
把100000改成10000
雄牛 2013-08-05
  • 打赏
  • 举报
回复
是的
引用 11 楼 HSFZXJY 的回复:
是那个FillChar么。。
雄牛 2013-08-04
  • 打赏
  • 举报
回复
引用 4 楼 HSFZXJY 的回复:
总之就是你那个图形控件的高度的宽度就是了。。 if (tmp2.X<0)or(tmp2.X>Canvas.Width)or(tmp2.Y<0)or(tmp2.Y>Canvas.Width) then 改为: if (tmp2.X<0)or(tmp2.X>Canvas.Width)or(tmp2.Y<0)or(tmp2.Y>Canvas.Height) then
我的代码如下: TPointNote=record X:Integer; Y:Integer; Color:TColor; end; PointNote1:Array of TPointNote; { 取得相连的点 Canvas为原图画布,pt为起始搜索点,返回一个与pt颜色相同的点的列表} function TForm1.GetTxtPoint(Cavs1:TCanvas;pt:TPointNote): TList; const direct :array [1..4,1..2] of shortint =((0,-1),(0,1),(-1,0),(1,0)); //方向增量 var dl:array [1..100000] of TPointNote; //队列最大 head,tail,i:Integer; //head为队首指针,tail为队尾指针 tmp,tmp2:TPointNote; begin head:=0; tail:=1; FillChar(dl,sizeof(dl),0); dl[tail]:=pt; //初始化队列 result := TList.Create; while head<>tail do begin Inc(head); //队首加1 tmp:=dl[head]; //出队 for i:= 1 to 4 do //遍历四个方向 begin tmp2.X :=tmp.X+direct[i,1]; tmp2.Y :=tmp.Y+direct[i,2]; {if (tmp2.X<0)or(tmp2.X>Cavs1.Width)or(tmp2.Y<0)or(tmp2.Y>Cavs1.Width) then continue; //如果越界则跳过 } tmp2.Color := Cavs1.Pixels[tmp2.X,tmp2.Y]; if tmp2.Color = pt.Color then //如果颜色相同则入队,并加入结果中 begin Inc(tail); dl[tail]:=tmp2; result.Add(@tmp2); end; end; end; end; //调用 procedure TForm1.Button1Click(Sender: TObject); var Point1:TPointNote; List1:TList; begin try Point1.X:=w;//w为图片宽度 Point1.Y:=h;//h为图片高度 Point1.Color:=clYellow;//取出是黄色的颜色 {PointNote1 是别的地方取到的整个图片都是黄色的X,Y,color的一维护数组,但这里不知道怎么用了,几天了,你的代码我还是看不太懂} List1:=TList.Create; List1:=GetTxtPoint(Image1.Picture.Bitmap.Canvas,Point1); finally List1.Free; end; end;
雄牛 2013-08-04
  • 打赏
  • 举报
回复
引用 6 楼 HSFZXJY 的回复:
[quote=引用 5 楼 gzzai 的回复:] [quote=引用 2 楼 HSFZXJY 的回复:] //注:我不是大虾。。
无论从技术还是热心助人来衡量,你已经是个大虾了。[/quote][/quote] 我的所有取到的相同颜色的坐标点信息,都保存在PointNote1,我要如何改才能用上你的代码? 定义如下: PointNote1:Array of TPointNote; TPointNote的定义参考了你的代码,只改动结构名,如下: TPointNote=record X:Integer; Y:Integer; Color:TColor; end;
广州佬 2013-07-17
  • 打赏
  • 举报
回复
引用 2 楼 HSFZXJY 的回复:
//注:我不是大虾。。
无论从技术还是热心助人来衡量,你已经是个大虾了。
hsfzxjy 2013-07-17
  • 打赏
  • 举报
回复
引用 5 楼 gzzai 的回复:
[quote=引用 2 楼 HSFZXJY 的回复:] //注:我不是大虾。。
无论从技术还是热心助人来衡量,你已经是个大虾了。[/quote]
hsfzxjy 2013-07-16
  • 打赏
  • 举报
回复
总之就是你那个图形控件的高度的宽度就是了。。 if (tmp2.X<0)or(tmp2.X>Canvas.Width)or(tmp2.Y<0)or(tmp2.Y>Canvas.Width) then 改为: if (tmp2.X<0)or(tmp2.X>Canvas.Width)or(tmp2.Y<0)or(tmp2.Y>Canvas.Height) then
雄牛 2013-07-16
  • 打赏
  • 举报
回复
引用 2 楼 HSFZXJY 的回复:
//注:我不是大虾。。
你好,H, if (tmp2.X<0)or(tmp2.X>Canvas.Width)or(tmp2.Y<0)or(tmp2.Y>Canvas.Width) then 这句通不过,没有Width这个对象呢
hsfzxjy 2013-07-16
  • 打赏
  • 举报
回复
//注:我不是大虾。。
hsfzxjy 2013-07-16
  • 打赏
  • 举报
回复
广度优先搜索:

uses Classes;

TPixel =record    //定义点类型
          x,y:integer;
          color: TColor;
        end;
{Canvas为原图画布,pt为起始搜索点,返回一个与pt颜色相同的点的列表}
function (Canvas: TCanvas;pt:TPixel): TList;
const
  direct :array [1..4,1..2] of shortint =((0,-1),(0,1),(-1,0),(1,0)); //方向增量
var
  dl: array [1..10000] of TPixel;  //队列
  head,tail,i:Integer;  //head为队首指针,tail为队尾指针
  tmp,tmp2:TPixel;
begin
  head:=0;  tail:=1; FillChar(dl,sizeof(dl),0); dl[tail]:=pt; //初始化队列
  result := TList.Create;
  while head<>tail do
  begin
    Inc(head);  //队首加1
    tmp:=dl[head];  //出队
    for i:= 1 to 4 do  //遍历四个方向
    begin
      tmp2.X :=tmp.X+direct[i,1];
      tmp2.Y :=tmp.Y+direct[i,2];
      if (tmp2.X<0)or(tmp2.X>Canvas.Width)or(tmp2.Y<0)or(tmp2.Y>Canvas.Width) then 
        continue;   //如果越界则跳过
      tmp2.Color := Canvas.Pixels[tmp2.X,tmp2.Y];
      if tmp2.Color = pt.Color then  //如果颜色相同则入队,并加入结果中
      begin
        Inc(tail);
        dl[tail]:=tmp2;
        result.Add(@tmp2);
      end;
    end; 
  end;
end;

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧