C#代码图片识别分割,看不懂求注释
小弟学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);