+ -
当前位置:首页 → 问答吧 → C#代码图片识别分割,看不懂求注释

C#代码图片识别分割,看不懂求注释

时间:2011-12-13

来源:互联网

小弟学C#时间不长,数学也不好,这是网上的一段C#代码,图片分割,看不懂求注释
另外我想把这段代码改成自定义字符个数,怎么个改法
代码如下:
  public void SplitPics()
  {
  int jishu = 7;
  if (c_Bitmap != null)
  {
  pictureBox1.Image = c_Bitmap;
  flag1 = 1;
  BitmapData bmData = c_Bitmap.LockBits(new Rectangle(0, 0, c_Bitmap.Width, c_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  unsafe
  {
  int stride = bmData.Stride; //Bitmap 对象的跨距宽度,以字节为单位。跨距是单行像素(一个扫描行)的宽度,舍入为一个 4 字节的边界

  System.IntPtr Scan0 = bmData.Scan0; //指向存放位图像素内存中的第一个地址,即存放的值肯定是第一个像素点的值
  byte* p = (byte*)(void*)Scan0;
  int nOffset = stride - c_Bitmap.Width * 3;

  int nWidth = c_Bitmap.Width;
  int nHeight = c_Bitmap.Height;
  int[] counth = new int[nHeight]; //建立数组,大小为图像高度
  int[] counttwo = new int[nWidth];//建立数级,大小为图像宽度
  int Xh = nHeight, Xl = 0; //???
  for (int i = 0; i < nHeight; i++)
  {
  counth[i] = 0; //初始化数组为0
  }
  for (int y = 0; y < nHeight; ++y)
  {
  for (int x = 0; x < nWidth; ++x)
  {
  if ((p[0] == 0 && p[3] == 255) || (p[0] == 255 && p[3] == 0))
  {
  counth[y]++;
  }


  p += 3;
  }
  p += nOffset;

  }
  for (int y = 0; y < nHeight; ++y)
  {
  if (counth[y] >= 12 && counth[(y + 1) % nHeight] >= 6 && counth[(y + 2) % nHeight] >= 11)
  {
  if (Xh > y)
  {
  Xh = y;
  Xl = y;
  }
  if ((y - Xl) == 1)
  Xl = y + 4;
  }
  }
  byte* p1 = (byte*)(void*)Scan0;
  p1 += stride * (Xh - 1);
  for (int y = Xh; y < Xl; ++y)
  {
  for (int x = 0; x < nWidth; ++x)
  {

  if (p1[0] == 255)
  counttwo[x]++;

  p1 += 3;
  }
  p1 += nOffset;
  }
  int contg = 0, contd = 0, contr1 = 0, contl1 = 0, Yl = nWidth, Yr = 0;
  int[] Yl1 = new int[20];//?????????????????????
  int[] Yr1 = new int[20];//??????????????????????
  foreach (int i in Yr1)
  {
  Yr1[i] = 0;
  }
  foreach (int i in Yl1)
  {
  Yl1[i] = 0;
  }
  for (int y = 1; y < Xl - Xh; ++y)
  {
  for (int x = 0; x < nWidth; ++x)
  {
  if (counttwo[(x + 1) % nWidth] < y && counttwo[x] >= y&&counttwo[Math.Abs((x - 1) % nWidth)] >= y&&contg>=2 )
  {
  if (contr1 == jishu-1)//??????????
  { Yr = x; }
  if ((contr1 == 2 && (x >= Yl1[2] && Yl1[2] > 0)))
  {
  Yr1[contr1] = x;
  contr1++;
  contd = 0;
  }
  else
  {
  if ((contr1 != 2))
  {
  if (contr1 == 0 && contg < 4)
  {
  Yl1[0] = 0;
  contl1 = 0;
  }
  if ((x >= Yl1[0] && Yl1[0] > 0))
  {
  Yr1[contr1] = x;
  contr1++;
  contd = 0;
  }
  }
  }
  }
  if (counttwo[Math.Abs((x - 1) % nWidth)] < y && counttwo[x] >= y && counttwo[(x + 1) % nWidth] >= y&&contd>=2)
  {
  if (contl1 == 0&&counttwo[(x + 2) % nWidth] >= y)
  { Yl = x; }
  if ((contl1 == 2 && contd > 5))
  {
  Yl1[contl1] = x;
  contl1++;

  }
  else
  {
  if ((contl1 != 2))
  { Yl1[contl1] = x;
  contl1++;
  contg = 0;
  if (contl1 == 0 && counttwo[(x + 2) % nWidth] < y)
  { Yl1[0] = 0;
  contl1 = 0;
  }
   
  }
  }
  }
  contg++;
  contd++;

  }
  if (contr1 + contl1 >= 14)
  {
  break;
  }
  contr1 = 0;
  contl1 = 0;
  for (int i =0;i<Yr1.Length ;i++)
  {
  Yr1[i] = 0;
  }
  for (int i = 0; i < Yl1.Length;i++ )
  {
  Yl1[i] = 0;
  }
  }
  c_Bitmap.UnlockBits(bmData);
   
 

作者: anchengw   发布时间: 2011-12-13

if ((Xl - Xh) > 1 && (Yr - Yl) > 1)
  {
   
  if ((Xl - Xh) > 1 && (Yr1[1] - Yl1[1]) > 1)
  {
   
  Rectangle sourceRectangle2 = new Rectangle(Yl1[1], Xh, Yr1[1] - Yl1[1], Xl - Xh);//Xh为图片高度
  c_Bitmap3 = c_Bitmap1.Clone(sourceRectangle2, PixelFormat.DontCare);
  z_Bitmaptwo[1] = c_Bitmap.Clone(sourceRectangle2, PixelFormat.DontCare);
  pic2.Image = z_Bitmaptwo[1];
   
  }
  if ((Xl - Xh) > 1 && (Yr1[2] - Yl1[2]) > 1)
  {
   
  Rectangle sourceRectangle3 = new Rectangle(Yl1[2], Xh, Yr1[2] - Yl1[2], Xl - Xh);
  c_Bitmap4 = c_Bitmap1.Clone(sourceRectangle3, PixelFormat.DontCare);
  z_Bitmaptwo[2] = c_Bitmap.Clone(sourceRectangle3, PixelFormat.DontCare);
  pic3.Image = z_Bitmaptwo[2];
   
  }
  if ((Xl - Xh) > 1 && (Yr1[3] - Yl1[3]) > 1)
  {
  Rectangle sourceRectangle4 = new Rectangle(Yl1[3], Xh, Yr1[3] - Yl1[3], Xl - Xh);
  z_Bitmap3 = c_Bitmap1.Clone(sourceRectangle4, PixelFormat.DontCare);
  z_Bitmaptwo[3] = c_Bitmap.Clone(sourceRectangle4, PixelFormat.DontCare);
  pic4.Image = z_Bitmaptwo[3];
   
  }
  if ((Xl - Xh) > 1 && (Yr1[4] - Yl1[4]) > 1)
  {
  Rectangle sourceRectangle5 = new Rectangle(Yl1[4], Xh, Yr1[4] - Yl1[4], Xl - Xh);
  z_Bitmap4 = c_Bitmap1.Clone(sourceRectangle5, PixelFormat.DontCare);
  z_Bitmaptwo[4] = c_Bitmap.Clone(sourceRectangle5, PixelFormat.DontCare);
  pic5.Image = z_Bitmaptwo[4];
   
  }
  if ((Xl - Xh) > 1 && (Yr1[5] - Yl1[5]) > 1)
  {
  Rectangle sourceRectangle6 = new Rectangle(Yl1[5], Xh, Yr1[5] - Yl1[5], Xl - Xh);
  z_Bitmap5 = c_Bitmap1.Clone(sourceRectangle6, PixelFormat.DontCare);
  z_Bitmaptwo[5] = c_Bitmap.Clone(sourceRectangle6, PixelFormat.DontCare);
  pic6.Image = z_Bitmaptwo[5];
   
  }
  if ((Xl - Xh) > 1 && (Yr1[6] - Yl1[6]) > 1)
  {
   
  Rectangle sourceRectangle7 = new Rectangle(Yl1[6], Xh, Yr1[6] - Yl1[6], Xl - Xh);
  z_Bitmap6 = c_Bitmap1.Clone(sourceRectangle7, PixelFormat.DontCare); //原来未处理的车牌图像克隆
  z_Bitmaptwo[6] = c_Bitmap.Clone(sourceRectangle7, PixelFormat.DontCare);//二值化处理的车牌图像克隆
  pic7.Image = z_Bitmaptwo[6];
   
  }
  if ((Xl - Xh) > 1 && (Yr1[0] - Yl1[0]) > 1)
  {
   
  Rectangle sourceRectangle0 = new Rectangle(Yl1[0], Xh, Yr1[0] - Yl1[0], Xl - Xh);
   
  pic1.Image = c_Bitmap1.Clone(sourceRectangle0, PixelFormat.DontCare);
  pictureBox3.Image = c_Bitmap1;//彩图,未处理过的车牌照
  pictureBox4.Image = c_Bitmap; //二值化后的车牌照
   
  }
   
  }

  }
  }
  }

作者: anchengw   发布时间: 2011-12-13

如何计算的 Yr1[] 和 Yl1[]的值,图像分割的依据是什么?请重点讲解一下

作者: anchengw   发布时间: 2011-12-13

建议你一句句的看一下,因为里面都有注释了,如果遇到实在看不懂的就调试,实在调试不出来的再来问,这样更有效果,你这么一大段一大段的发出来别人怎么给你讲?你自己觉得呢?如果是别人这么问你,你感觉如何?

作者: yfqvip   发布时间: 2011-12-13

写的很明白了。如果看不懂说明基础没打好。打好基础再看。

作者: zanfeng   发布时间: 2011-12-13

晕哪些注释是我加上的,我就是对它的算法不明白,单句看谁都懂,可为什么要这样做,就不懂了

作者: anchengw   发布时间: 2011-12-13

比如这一句,为什么条件要设为大于等于12 大于等于6 大于等于11,依据是什么
if (counth[y] >= 12 && counth[(y + 1) % nHeight] >= 6 && counth[(y + 2) % nHeight] >= 11)

作者: anchengw   发布时间: 2011-12-13

第二段代码不用管的,结果已经计算出来了,关键是上面的计算过程,求高手解释

作者: anchengw   发布时间: 2011-12-13

不懂啊

作者: ling3wei   发布时间: 2011-12-13

为了便于问题的解答,我把问题整理一下,关键就是几个条件的判断的语句不明白。
1. if ((p[0] == 0 && p[3] == 255) || (p[0] == 255 && p[3] == 0)) 这一句判断色值的我不懂,按我的理解处理的是二值化的图像,RGB的色值不是0就是255。怎么会p[0]是255的同时p[3] == 0呢?
2. if (counth[y] >= 12 && counth[(y + 1) % nHeight] >= 6 && counth[(y + 2) % nHeight] >= 11)还有这一句的判断依据是什么,为什么会是12,6,11的值。
3. int[] counth int[] counttwo这两个数组具体起什么作用?
4.if (counttwo[(x + 1) % nWidth] < y && counttwo[x] >= y&&counttwo[Math.Abs((x - 1) % nWidth)] >= y&&contg>=2 ) 这一句也不是很明白为什么要设这样的条件,为的是什么?

作者: anchengw   发布时间: 2011-12-13

自己搞明白了,关键是这一句if ((p[0] == 0 && p[3] == 255) || (p[0] == 255 && p[3] == 0)),实际上p[3]指向的是下一个像素的一个色值,我却把它和p[0]当成一个像表。这样,counth[y]记录的是图片横向扫描黑白变化的次数。
  希望论坛上的老大们在指点新人的时候有点耐心。实际上读一段别人写的代码,同时也是在学习,有利于提高自己写代码的水平。

作者: anchengw   发布时间: 2011-12-13

搞明白就好,不过大部分老大都有工作比较忙,发这么一大段大部分人来不及看的。希望楼主能够理解他们

作者: yfqvip   发布时间: 2011-12-13

这也充分证明了楼主的能力,通过自己努力能搞明白的。

作者: yfqvip   发布时间: 2011-12-13