小弟学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);
                    
                 

解决方案 »

  1.   

       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; //二值化后的车牌照
                                
                            }
                         
                        }                }
                }
            }
      

  2.   

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

  3.   

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

  4.   

    为了便于问题的解答,我把问题整理一下,关键就是几个条件的判断的语句不明白。
    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 ) 这一句也不是很明白为什么要设这样的条件,为的是什么?
      

  5.   

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

  6.   

    if (counth[y] >= 12 && counth[(y + 1) % nHeight] >= 6 && counth[(y + 2) % nHeight] >= 11) 
    楼主,这段代码我也未能理解,请求分享