delphi中应用top-hat算法对图片背景进行不均匀光照的校正
小弟近来在用delphi做不均匀光照的校正,遇到了这个问题:
1。形态学的腐蚀膨胀运算是否需要依据需要处理的图像选择结构元素?如果是,又该如何选择?
以下是我在网上找的腐蚀运算代码,可以实现运算,但是好像没有事先对所用的结构元素进行选择,应该如何改进?
function BitmapErose(Bitmap: TBitmap; Horic: Boolean): Boolean;
var
X, Y: integer;
newbmp: TBitmap;
P, Q, R, O: pByteArray;
{ IWidth, IHeight: integer;
BA: array of array of Boolean;
procedure GetBAValue;
var
X, Y: integer;
P: pByteArray;
begin
SetLength(BA, IWidth, IHeight);
begin
for Y := 0 to IHeight - 1 do
begin
P := bitmap.ScanLine[Y];
for X := 0 to IWidth - 1 do
begin
BA[X][Y] := ((P[3 * X + 2]) < 128);
end;
end;
end;
end; }
begin
newbmp := TBitmap.Create;
//动态创建位图
newbmp.Assign(bitmap);
// Horic标志是水平方向还是竖直方向腐蚀
if (Horic) then
begin
for Y := 1 to newbmp.Height - 2 do
begin
O := bitmap.ScanLine[Y];
P := newbmp.ScanLine[Y - 1];
Q := newbmp.ScanLine[Y];
R := newbmp.ScanLine[Y + 1];
for X := 1 to newbmp.Width - 2 do
begin
if ((O[3 * X] = 0) and (O[3 * X + 1] = 0) and (O[3 * X + 2]
= 0)) then
begin
// 判断黑点左右邻居是否有白色点,有则腐蚀,置该点为白色
// 白色点则保持不变
if (((Q[3 * (X - 1)] = 255) and (Q[3 * (X - 1) + 1] =
255) and (Q[3 * (X - 1) + 2] = 255)) or ((Q[3 * (X
+
1)] = 255) and (Q[3 * (X + 1) + 1] = 255) and
(Q[3 * (X + 1) + 2] = 255)) or ((P[3 * X] = 0) and
(P[3 * X + 1] = 255) and (P[3 * X + 2] = 255))
or ((R[3 * X] = 255) and (R[3 * X + 1] = 255) and
(R[3
* X + 2] = 255))) then
begin
O[3 * X] := 255;
O[3 * X + 1] := 255;
O[3 * X + 2] := 255;
//// 将满足条件的黑色点置为白色
end;
end;
end;
end;
end
else
begin
for Y := 1 to newbmp.Height - 2 do
begin
O := bitmap.ScanLine[Y];
// P := newbmp.ScanLine[Y - 1];
Q := newbmp.ScanLine[Y];
// R := newbmp.ScanLine[Y + 1];
for X := 1 to newbmp.Width - 2 do
begin
// 判断一个黑点上下邻居是否有白点,有则腐蚀,置黑点为白色
// 白色点就保持不变
if ((O[3 * X] = 0) and (O[3 * X + 1] = 0) and (O[3 * X + 2]
= 0)) then
begin
if (((Q[3 * (X - 1)] = 255) and (Q[3 * (X - 1) + 1] =
255) and (Q[3 * (X - 1) + 2] = 255)) or ((Q[3 * (X
+
1)] = 255) and (Q[3 * (X + 1) + 1] = 255) and
(Q[3 * (X + 1) + 2] = 255))) then
begin
O[3 * X] := 255;
O[3 * X + 1] := 255;
O[3 * X + 2] := 255;
// 将满足条件的黑色点置为白色
end;
end;
end;
end;
end;
result := True;
end;
2.最好希望那位大虾可以给出top-hat的源码,小弟不胜感激,一定高分相送。
[解决办法]
图像处理方面的,你可以去看看发哥的博客,他是这方面的专家。
http://blog.csdn.net/maozefa