因项目需要,花了点时间做了个小程序来演示京东商城的图片价格识别,初步试用效果还不错。
先上个图:
用了个最简单的算法:
/// <summary>
/// 图片解码
/// </summary>
/// <param name="srcBitmap">0-9和.的源图像</param>
/// <param name="distBitmap">目标图像</param>
/// <returns></returns>
public static string DecodeGrayBitmap(IList<Bitmap> srcBitmap, Bitmap[] distBitmap)
{
StringBuilder sb = new StringBuilder();
foreach (Bitmap dist in distBitmap)
{
int val = 0;
double max = 0;
for (int pos = 0; pos < srcBitmap.Count; pos++)
{
double n = ClacMatchRate(srcBitmap[pos], dist);
if (n > max)
{
max = n;
val = pos;
}
if (max > 0.9)
break;
}
if (val == 10)
{
sb.Append('.');
}
else
{
sb.Append(val);
}
}
return sb.ToString();
}
public static double ClacMatchRate(Bitmap src, Bitmap dist)
{
double black = 0;
double match = 0;
int w = src.Width < dist.Width ? src.Width : dist.Width, h = src.Height < dist.Height ? src.Height : dist.Height;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int sc = PixelGrayValue(src.GetPixel(x, y));
int dc = PixelGrayValue(dist.GetPixel(x, y));
if (sc == 0 || dc == 0)
{
black++;
if (sc == dc)
match++;
}
}
}
return match / black;
}
/// 图片解码
/// </summary>
/// <param name="srcBitmap">0-9和.的源图像</param>
/// <param name="distBitmap">目标图像</param>
/// <returns></returns>
public static string DecodeGrayBitmap(IList<Bitmap> srcBitmap, Bitmap[] distBitmap)
{
StringBuilder sb = new StringBuilder();
foreach (Bitmap dist in distBitmap)
{
int val = 0;
double max = 0;
for (int pos = 0; pos < srcBitmap.Count; pos++)
{
double n = ClacMatchRate(srcBitmap[pos], dist);
if (n > max)
{
max = n;
val = pos;
}
if (max > 0.9)
break;
}
if (val == 10)
{
sb.Append('.');
}
else
{
sb.Append(val);
}
}
return sb.ToString();
}
public static double ClacMatchRate(Bitmap src, Bitmap dist)
{
double black = 0;
double match = 0;
int w = src.Width < dist.Width ? src.Width : dist.Width, h = src.Height < dist.Height ? src.Height : dist.Height;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int sc = PixelGrayValue(src.GetPixel(x, y));
int dc = PixelGrayValue(dist.GetPixel(x, y));
if (sc == 0 || dc == 0)
{
black++;
if (sc == dc)
match++;
}
}
}
return match / black;
}