我有一个被扫描为TIF格式(黑/白)的表格(在表格的右下角画有一条线)。
有些人可能会在表格上签名,有些人不会。那么,如何确定扫描的图像上是否包含签名?建议的任何算法或任何现有的框架或库都可以实现这一目标?
谢谢!
这是我过去用于检查签名的代码。假定图像的aBytes per pixel
为3。
该MAX_BLACK_VALUE
数字来自像素颜色(255)的最大值乘以3(因为每个像素的字节数为3),然后减半。
该代码通过对签名图像中的像素数量求和,并通过MinPixelCount
返回真值(即已检测到签名)来进行工作。否则它将返回false。
该代码还利用Lockbits
一种方法来使图像处理更快,如果java
具有类似的方法,只需确保始终UnlockBits
按照我在finally
语句中的调用进行调用。
private const int MAX_BLACK_VALUE = 382; // ((255 * 3) / 2) rounded down
private bool CheckForSignature(string SignatureFile, int MinPixelCount)
{
using (Bitmap bitmap = new Bitmap(SignatureFile))
{
int nBytesPerPixel = Bitmap.GetPixelFormatSize(bitmap.PixelFormat) / 8;
System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
try
{
byte[] baPixels = new byte[bitmapData.Stride * bitmap.Height];
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, baPixels, 0, baPixels.Length);
int nBlackPixels = 0;
for (int h = 0; h < bitmap.Height; h++)
{
int nCurrentLine = h * bitmapData.Stride;
for (int w = 0; w < (bitmap.Width * nBytesPerPixel); w += nBytesPerPixel)
{
int nBlue = baPixels[nCurrentLine + w];
int nGreen = baPixels[nCurrentLine + w + 1];
int nRed = baPixels[nCurrentLine + w + 2];
if (nBlue + nGreen + nRed <= MAX_BLACK_VALUE)
{
nBlackPixels++;
if (nBlackPixels >= MinPixelCount)
return true;
}
}
}
}
finally
{
bitmap.UnlockBits(bitmapData);
}
}
return false;
}
ps我意识到这是一个java
问题,但我只知道,C#
但您应该可以将其转换。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句