图片颜色颜色格式化成黑白两种颜色
如题:不能够格式化成功!求救!
/**/
/// <summary>
/// 得到深色、浅色--深色的RGB刚好相差30
/// </summary>
/// <param name="Img"></param>
/// <returns></returns>
private Color[] GetDeepColor(Bitmap Img)
{
Color c = Color.White;//深色
Color c1 = Color.White;//浅色
Color[] cc = new Color;
if (Img == null)
{//检查图片是否有效
return null;
}
for (int w = 0; w <= Img.Width - 1; w++)
{//从做到右开始找。
int h = 4;
do
{//经过观察,y方向4-6坐标的三行绝对可以找到这2种颜色
Color temp = Img.GetPixel(w, h);// 当前色
if (temp.R < 0xff)
{//'有颜色的时候才在处理
if (c.R > 0)
{
//用找到的颜色和当前颜色比较
//'如果找到的颜色比当前颜色要小
// '那么 c就是深色
// 'c1就是浅色
if (c.R < temp.R)
{
c1 = temp;
}
if (c.R > temp.R)
{
c1 = c;
c = temp;
}
}
else
{
c = temp;
}
if (c1.ToArgb() > 0)
{
break;
}
}
h++;
}
while (h <= 6);
}
cc = c;
cc = c1;
return cc;
}
/**/
/// <summary>
/// 去背景,单色显示
/// </summary>
/// <param name="Img"></param>
/// <param name="Cc"></param>
/// <returns></returns>
private Bitmap FormatImg(Bitmap Img, Color[] Cc)
{
object obj = null;
//循环处理每一点
for (int w = 0; w <= Img.Width - 1; w++)
{
for (int h = 0; h <= Img.Height - 1; h++)
{
//如果当前颜色是深色的话,就转化成黑色
//否则就转化成白色
Color c = Img.GetPixel(w, h);
if (c.ToArgb() == Cc.ToArgb())
{
Img.SetPixel(w, h, Color.White);
}
if (c.ToArgb() == Cc.ToArgb())
{
Img.SetPixel(w, h, Color.Black);
}
}
}
return Img;
} 杀个发先,等高手指教! 不懂,不过有兴趣想看看完整点的代码学习下...
PS:觉得单就一个“不成功”描述不是很到位.. 不懂,不过有兴趣想看看完整点的代码学习下...
PS:觉得单就一个“不成功”描述不是很到位..
iptton 发表于 2009-5-21 19:15 https://www.gdutbbs.com/images/common/back.gif
呵呵.....
我昨天做了,好像解决了!
不过没有深入总结!
我贴多点代码上去吧!
很乱,没整理! //图形
Bitmap bmp = null;
//保存深浅2种颜色
Color[] cc = new Color;
int[] left = new int;//左边的点
int[] right = new int;//右边的点
int[] top = new int;//顶点
int[] bottom = new int;//底点
try
{
//获取图片源
bmp = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\bzhang\Desktop\图片辨析\test.jpg");
#region 转换灰度的算法
//转换灰度的算法
Color c1;
int luma;
for (int w1 = 0; w1 <= bmp.Width - 1; w1++)
{
for (int h1 = 0; h1 <= bmp.Height - 1; h1++)
{
c1 = bmp.GetPixel(w1, h1);
luma = (int)(c1.R * 0.3 + c1.G * 0.59 + c1.B * 0.11);//转换灰度的算法
bmp.SetPixel(w1, h1, Color.FromArgb(luma, luma, luma));
}
}
#endregion
#region 取色
cc = GetDeepColor(bmp);
#endregion
#region 去杂色,转换为黑白图片
for (int w2 = 0; w2 <= bmp.Width - 1; w2++)
{
for (int h2 = 0; h2 < bmp.Height - 1; h2++)
{
Color c2 = bmp.GetPixel(w2, h2);
if (c2.R > cc.R)
{
bmp.SetPixel(w2, h2, Color.FromArgb(255, 255, 255));
}
else
{
bmp.SetPixel(w2, h2, Color.FromArgb(0, 0, 0));
}
}
}
#endregion
#region 处理干扰点
this.RemoveMacula(bmp);
#endregion
#region 动态得到每个数字的边界
bool myColumn; //标示该列是否出现黑点
bool charStart = false; //标示首次取到黑点
int[] widthStartX = new int;
int[] heightStartY = new int;
int[] widthEndX = new int;
int[] heightEndY = new int;
int charNum = 0;
for (int x = 0; x < bmp.Width; x++)
{
myColumn = true;
for (int y = 0; y < bmp.Height; y++)
{
Color c = bmp.GetPixel(x, y);
if (c.R == 0 && charStart == false)//第一次出现黑点
{
widthStartX = x;
charStart = true;
break;
}
if (c.R == 0 && charStart == true)//后续出现黑点
{
myColumn = false;
break;
}
}
if (myColumn == true && charStart == true && widthStartX < x)//如果当列没有黑点并且前面出现过黑点还没结束
{
widthEndX = x - 1;
charStart = false;
charNum++;
}
if (charStart == true && myColumn == false && x == (bmp.Width - 1))//如果开始出现黑点了,并且最后一列也有黑点
{
widthEndX = x;
charStart = false;
charNum++;
}
}
#endregion
#region 得到每个字符的特征码
string str1= string.Empty;
string str2 = string.Empty;
string str3 = string.Empty;
string str4 = string.Empty;
for (int x1 = 0; x1 < bmp.Width; x1++)
{
for (int y1 = 0; y1 < bmp.Height; y1++)
{
if (widthStartX < x1 && x1 < widthEndX)
{
Color cnum1 = bmp.GetPixel(x1, y1);
if (cnum1.R == 0)
{
str1 = str1 + "1";
}
else
{
str1 = str1 + "0";
}
}
else if (widthStartX < x1 && x1 < widthEndX)
{
Color cnum2 = bmp.GetPixel(x1, y1);
if (cnum2.R == 0)
{
str2 = str2 + "1";
}
else
{
str2 = str2 + "0";
}
}
else if (widthStartX < x1 && x1 < widthEndX)
{
Color cnum3 = bmp.GetPixel(x1, y1);
if (cnum3.R == 0)
{
str3 = str3 + "1";
}
else
{
str3 = str3 + "0";
}
}
else if (widthStartX < x1 && x1 < widthEndX)
{
Color cnum4 = bmp.GetPixel(x1, y1);
if (cnum4.R == 0)
{
str4 = str4 + "1";
}
else
{
str4 = str4 + "0";
}
}
}
}
//MessageBox.Show("8: " + str1 + "\n"+ "9: "+ str2+"\n"+"1: "+ str3+"\n"+"5: "+ str4);
#endregion
bmp.Save(@"c:\1.bmp");
//这里分析单个字符的上下左右点,这样就可以确定范围了
//识别出来
//释放资源
bmp.Dispose();
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
finally
{
} //去杂点,只能判断黑白图片的杂点.
public void RemoveMacula(Bitmap Img)
{//这里循环找出每个点的周围8个点,如果周围8个点都是白色的话,这个点就判断为杂点
for (int w = 0; w <= Img.Width - 1; w++)
{
for (int h = 0; h <= Img.Height - 1; h++)
{
int bcount = 0;
if (Img.GetPixel(w, h).ToArgb() == Color.Black.ToArgb())
{
for (int ww = -1; ww <= 1; ww++)
{
for (int hh = -1; hh <= 1; hh++)
{
if ((w + ww) < 0 | (h + hh) < 0 | (w + ww) >= Img.Width | (h + hh) >= Img.Height)
{
bcount += 1;//这里判断处于边缘的点,应为边缘的点没有周围的8个点
}
else if (Img.GetPixel(w + ww, h + hh).ToArgb() == Color.White.ToArgb())
{
bcount += 1;
}
}
}
}
if (bcount > 7)
{
Img.SetPixel(w, h, Color.White);
}
}
}
}
//截取图片上每个数字的小图片
private void AnalyzePicWithSmallPic(Bitmap bmp, int x) //'bmp要传入的图片,X从什么位置开始(这里指的是宽度)
{
//'先获取每个字符的左右范围,再获取这个字符的上下范围
//'这里是根据灰度的强弱来判断当前点应该是黑色还是白色
Bitmap tempBmp;
int width = 0;//'得到小图片的宽度
int height = 0;// '得到小图片的高度
int widthX = 0; //'得到小图片起始宽的坐标点
int heightY = 0; //'得到小图片起始高的坐标点
int widthEndX = 0; //'得到小图片结束宽的坐标点
int heightEndY = 0; //'得到小图片结束高的坐标点
//'记录都没黑点的行
bool bb = true; //找到第一个黑点 则为false
bool wb = true; //找到后续黑点 则为true
Color c;
//'得到宽度
for(int w = 0; w<=bmp.Width - 1; w++)
{
wb = true;
for(int h= 0; w<=bmp.Height - 1; h++)
{
c = bmp.GetPixel(w, h);
if (c.ToArgb() == Color.Black.ToArgb() && bb)
{
widthX = w;//'找到第一个黑点
heightY = h;
bb = false;
}
else if( c.ToArgb() == Color.Black.ToArgb() && !bb)//'找到第一个黑点后,找到此列中所有像素都为白点的列号
{
wb = false;
break;
}
}
if (!bb && wb && widthX < w)
{
widthEndX = w;
break;
}
if (!bb && wb)
{
break;
}
}
}
/**/
/// <summary>
/// 得到深色、浅色--深色的RGB刚好相差30
/// </summary>
/// <param name="Img"></param>
/// <returns></returns>
private Color[] GetDeepColor(Bitmap Img)
{
Color c = Color.White;//深色
Color c1 = Color.White;//浅色
Color[] cc = new Color;
if (Img == null)
{//检查图片是否有效
return null;
}
for (int w = 0; w <= Img.Width - 1; w++)
{//从做到右开始找。
int h = 4;
do
{//经过观察,y方向4-6坐标的三行绝对可以找到这2种颜色
Color temp = Img.GetPixel(w, h);// 当前色
if (temp.R < 0xff)
{//'有颜色的时候才在处理
if (c.R > 0)
{
//用找到的颜色和当前颜色比较
//'如果找到的颜色比当前颜色要小
// '那么 c就是深色
// 'c1就是浅色
if (c.R < temp.R)
{
c1 = temp;
}
if (c.R > temp.R)
{
c1 = c;
c = temp;
}
}
else
{
c = temp;
}
if (c1.ToArgb() > 0)
{
break;
}
}
h++;
}
while (h <= 6);
}
cc = c;
cc = c1;
return cc;
} 改天整理后再用附件传上去!
一个客户叫做的,
昨晚要跟他收订金,
那人想我整个做完再一起给钱,
谈不大陇,就直接找了别人做了,
让我很郁闷!
所以先不接着做了!
先发点代码,跟兄弟姐妹们交流下! 能不能整个附件上来看?
页:
[1]