今Delphi6でラベリング処理のプログラムを作成しているんですが,ラベル付けされた画像から一番上,下,右,左に位置する座標を取ってその座標点を使って四角で囲むようにしたいんですが,おもうように動きません.自分はこのようにラベル処理を作ったのですが,どのようにして座標点のプログラムを追加すればよいんでしょうか?
//--------------軌跡表示--------------------------------------------------
//軌跡表示
//引 数 BeforBmp:元画像 DivNum:2値化する際の閾値
//戻り値 ラベリング処理結果画像
procedure TForm1.Kiseki;
var
LabelNum,i,j,x,tx,ty,total,cx,cy : Integer;
AfterBmp : TBitmap; //2値化処理した画像を格納するビットマップ
Ap : PByteArray; //画像のポインタ
Image : LabelArray; //ラベル値格納用配列
minX,minY,maxX,maxY :integer;
//ImageAdd : Labelarray;
begin
LabelNum := 0; //ラベル数は0
minX:=0;
minY:=0;
maxX:=0;
maxY:=0;
AfterBmp :=L_Bmp ; //格納する
//ラベル格納用の配列の要素数を2値画像のピクセル数と同じにする
SetLength(Image,AfterBmp.Width,AfterBmp.Height);
//配列の初期化
for i:=0 to AfterBmp.Width-1 do
for j:=0 to AfterBmp.Height-1 do
Image[i,j] := 0;
//ラベル付けを行う
for j:=0 to AfterBmp.Height-1 do
begin
Ap := AfterBmp.ScanLine[j]; //2値画像の列ポインタ取得
for i:=0 to AfterBmp.Width-1 do
begin
//画素が黒でラベル付けされていない場合
if (Ap[i*3] = 0) and (Image[i,j] = 0) then
begin
LabelNum := LabelNum + 1; //ラベル数を増やす
Image[i,j] := LabelNum; //その画素にラベルを付ける
KisekiSet(AfterBmp,Image,LabelNum); //周辺画素にラベルを付ける
end;
end;
end;
//各ラベルの重心位置にラベル値を描画
for x:=1 to LabelNum do
begin
tx := 0;
ty := 0;
total := 0;
for i:=0 to AfterBmp.Width-1 do
begin
for j:=0 to AfterBmp.Height-1 do
begin
//ラベルの入ってる座標にきたら画素数の総和をとる
if Image[i,j] = x then
begin
total := total + 1;
tx := tx + i;
ty := ty + j;
end;
end;
end;
cx := tx div total; //x座標の重心
cy := ty div total; //y座標の重心
AfterBmp.Canvas.Brush.Color:=$FF00FF;
//AfterBmp.Canvas.Font.Size := 30; //フォントサイズ
//AfterBmp.Canvas.TextOut(cx,cy,IntToStr(x)+'('+IntToStr(cx)+','+IntToStr(cy)+')'); //描画
AfterBmp.Canvas.Ellipse(cx-10, cy-10, cx+10, cy+10);
AfterBmp.Canvas.Ellipse(minX,minY,maxX,maxY);
end;
Image6.Picture.Bitmap:=AfterBmp;
end;
//ラベル値セット
//引 数 Bmp:2値画像 Image:ラベル配列(ポインタ) LabelNum:セットするラベル値
procedure TForm1.KisekiSet(Bmp : TBitmap; var Image : LabelArray; LabelNum : Integer);
var
i,j,x,y,tx,ty,count : Integer;
Bp : PByteArray; //画像のポインタ
begin
//ラベル付けが終わるまでループ
while True do
begin
count := 0;
//全画素を調べる
for j:=0 to Bmp.Height-1 do
begin
for i:=0 to Bmp.Width-1 do
begin
//すでにラベル値が付いている場合は周辺画素にラベルを付ける
if Image[i,j] = LabelNum then
begin
//周辺画素を見る
for y:=j-1 to j+1 do //上下
begin
if y < 0 then
ty := 0
else if y >= Bmp.Height then
ty := Bmp.Height-1
else
ty := y;
Bp := Bmp.ScanLine[ty]; //2値画像の列ポインタ
for x:=i-1 to i+1 do //左右
begin
if x < 0 then
tx := 0
else if x >= Bmp.Width then
tx := Bmp.Width-1
else
tx := x;
//画素が黒でラベル値がなければラベルセット
if (Bp[tx*3] = 0) and (Image[tx,ty] = 0) then
begin
Image[tx,ty] := LabelNum;
count := count + 1;
end;
end;
end;
end;
end;
end;
//ラベル付けが終わればループを抜ける
if Count = 0 then
Break;
end;
end;
ツイート | ![]() |