怎样得到黑白两色位图中黑色的路径?(200分或money)

foison 2004-04-29 04:48:50
我想实现类似PHOTOSHOP中魔棒来选取颜色的功能.
只针对黑白两色位图,怎样才能得到图中黑色的路径?

请高手指点.
最好有源码

另100分请到
http://expert.csdn.net/Expert/topic/3021/3021834.xml?temp=.3017084
获取
...全文
95 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
foison 2004-05-05
  • 打赏
  • 举报
回复
up
foison 2004-05-03
  • 打赏
  • 举报
回复
通过pathtoregion将路径转成区域。
求区域怎样转成路径?

快来帮忙呀
foison 2004-04-30
  • 打赏
  • 举报
回复
谢谢
TomSonChina 2004-04-30
  • 打赏
  • 举报
回复
帮你顶
foison 2004-04-30
  • 打赏
  • 举报
回复
lijinghe1(副乡长)
谢谢你哟。
foison 2004-04-30
  • 打赏
  • 举报
回复
可能通过pathtoregion将路径转成区域。
不知怎样把区域转成路径?
foison 2004-04-30
  • 打赏
  • 举报
回复
我想要的是Path.
包括point的信息pointType的信息等
lijinghe1 2004-04-29
  • 打赏
  • 举报
回复
region是区域,不是你要的路径?
foison 2004-04-29
  • 打赏
  • 举报
回复
region是什么呢,请原谅我的愚昧.
还有就是,怎样重画呢?
lijinghe1 2004-04-29
  • 打赏
  • 举报
回复
猛料中的,你改一下就行了。

思路:
扫描图象的scanline, 取不是背景色的连续坐标, 认为是一个height=1的rect,
用CreateRectRgn生成region, 再用CombineRgn(.....RGN_OR)与先前生成
的region合并生成新的region.
重复扫描完所有扫描线后就得到一个region了.

function CreateRgnFromBmp(ABmp: TBitmap; ARect: TRect; TransColor: TColor): HRGN;
var
b, e: Integer;
i, j: Integer;
p: PChar;
rg: HRGN;
rgbcolor: longint;
begin
result := 0;
if not assigned(ABmp) or ABmp.Empty then exit; // 返回空region
rgbcolor := ColorToRGB(transcolor);
rgbcolor := (
rgbcolor and $0000ff00
or (rgbcolor shr 16)
or (rgbcolor shl 16)
) and $00ffffff; // 交换RGB颜色值中的R与B, 使之与scanline中顺序相同
if IsRectEmpty(ARect) then
ARect := bounds(0, 0, abmp.width, abmp.height)
else
IntersectRect(arect, arect, bounds(0, 0, abmp.width, abmp.height));
if IsRectEmpty(arect) then exit;
ABmp.PixelFormat := pf24Bit; // 转换图象成24bit
for i := ARect.Top to ARect.Bottom - 1 do
begin
b := ARect.Left;
e := b - 1;
p := Pointer(Integer(ABmp.ScanLine[i]) + ARect.Left * 3); // scanline中起始位置
for j := ARect.Left to ARect.Right - 1 do
begin
if CompareMem(p, @rgbcolor, 3) then // 透明色
if b >= e then Inc(b)
else
begin
if result = 0 then
result := CreateRectRgn(b, i, e, i+1)
else begin
rg := CreateRectRgn(b, i, e, i+1);
CombineRgn(result, result, rg, RGN_OR);
DeleteObject(rg);
end;
b := e;
end
else if b >=e then e := b + 1
else Inc(e);
p := p + 3;
end;
if b < e then
if result = 0 then
result := CreateRectRgn(b, i, e, i+1)
else begin
rg := CreateRectRgn(b, i, e, i+1);
CombineRgn(result, result, rg, RGN_OR);
DeleteObject(rg);
end;
end;
if result <> 0 then // 将region定到(0,0)坐标
OffsetRgn(result, -arect.left, -arect.top);
end;
///////////////////////////////////////////////
const
BitMask: array[0..7] of byte = (128, 64, 32, 16, 8, 4, 2, 1);

function fcThisThat(const Clause: Boolean; TrueVal, FalseVal: Integer): Integer;
begin
if Clause then result := TrueVal else Result := FalseVal;
end;

function fcIsTrueColorBitmap(Bitmap: TBitmap): boolean;
begin
result:= Bitmap.PixelFormat = Graphics.pf24bit;
end;

function fcCreateRegionFromBitmap(ABitmap: TBitmap; TransColor: TColor): HRgn;
var
TempBitmap: TBitmap;
Rgn1, Rgn2: HRgn;
Col, StartCol, Row: integer;
Line: PByteArray;

function ColToColor(Col: integer): TColor;
begin
if fcIsTrueColorBitmap(TempBitmap) then
result:= Line[Col * 3] * 256 * 256 + Line[Col * 3 + 1] * 256 + Line[Col * 3 + 2]
else result := TColor(fcThisThat((Line[Col div 8] and BitMask[Col mod 8]) <> 0, clBlack, clWhite));
end;
begin
result := 0;
if (ABitmap <> nil) and (ABitmap.Width = 0) or (ABitmap.Height = 0) then Exit;
Rgn1 := 0;

TempBitmap := TBitmap.Create;

TempBitmap.Assign(ABitmap);
if not fcIsTrueColorBitmap(TempBitmap) then
begin
TempBitmap.Mask(TransColor);
TransColor := clBlack;
end;

with TempBitmap do
begin
for Row := 0 to TempBitmap.height-1 do
begin
Line:= scanLine[row];

Col := 0;
while Col < TempBitmap.Width do
begin
while (Col < TempBitmap.Width) and (ColToColor(Col) = TransColor) do inc(Col);
if Col >= TempBitmap.Width then Continue;

StartCol := Col;
while (Col < TempBitmap.Width) and (ColToColor(Col) <> TransColor) do inc(Col);
if Col >= TempBitmap.Width then Col := TempBitmap.Width;

if Rgn1 = 0 then Rgn1 := CreateRectRgn(StartCol, Row, Col, Row + 1)
else begin
Rgn2 := CreateRectRgn(StartCol, Row, Col, Row + 1);
if (Rgn2 <> 0) then CombineRgn(Rgn1,Rgn1,Rgn2,RGN_OR);
Deleteobject(Rgn2);
end;
end;
end;
end;
result := Rgn1;
TempBitmap.Free;
end;

tonylk 2004-04-29
  • 打赏
  • 举报
回复
看看数字图像处理的书吧,这是最简单的操作。。(记得有种叫爬虫法的,就可以)。。。

1,185

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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