「玩一玩」绘制投影直方图(我希望各位需要的能自己动手写下)
我在这篇《字符识别练习 分拆字符(验证码、车牌号、身份证号等)》帖子里用到了图像的投影直方图,然后就有不少筒子跟帖「求源码」。
你们可知道?我真心希望你们阅读下我给出的参考文献,自己动脑思考下,自己动手尝试下?
做这个直方图真的一点都不难啊,怎么就养成了有原理不思考,上来就要看源码的习惯呢?也许我不是专业人士,理解不了。我的认知里面,看源码还不如看原理清晰,看到原理我可以构思出个大概,用我自己的方式来实现。看源码,我的思路完全被禁锢在作者的思路里,遇上写作习惯差的,还能把脑袋绕晕。
下面我详解下怎么做。一看就明白了,真的很简单。
以下代码并非最佳效率,但可以说是最简单的,追求效率或者其他的自行改造。
投影直方图
projection histogram
从形式上看,是这样的(黑白样本):
放大看就类似于这样:
也就是说将样本图的每一列(以垂直投影为例),投影到直方图的某个坐标x上。
最简单的实现方法就来了:
变量:
Bitmap bmp (Width x Height)
int histo[Width]
1.遍历样本的每一点bmp.GetPixel(x,y)
2.如果颜色为黑,那么histo[x]++
3.统计完成,开始绘制
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color color = bmp.GetPixel(x, y); if (color.R < 50 && color.G < 50 && color.B < 50) histo[x]++; } } Bitmap tmp = new Bitmap(bmp.Width, bmp.Height); using (Graphics g = Graphics.FromImage(tmp)) for (int i = 0; i < bmp.Width; i++) { g.DrawLine( Pens.Black, i, tmp.Height, i, tmp.Height - histo[i]); } Bitmap getProjectHistogram(Bitmap bmp) { int width = bmp.Width; int height = bmp.Height; int[] histo = null; histo = new int[width]; for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) { Color color = bmp.GetPixel(x, y); if (color.R < 50 && color.G < 50 && color.B < 50) histo[x]++; } // draw Bitmap tmp = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(tmp)) for (int i = 0; i < width; i++) g.DrawLine( Pens.Black, i, height, i, height - histo[i]); return tmp; }