日期:2014-05-17  浏览次数:20837 次

关于递归
C# code


     private void AutoGrayValue(ref Point left,ref Point right,ref int dgGrayValue,int CharsCount,bool auto)
        {
            for (int i = 0; i < mBitmap.Height; i++)      //找有效区,可能是平行四边形
            {
                for (int j = 0; j < mBitmap.Width; j++)
                {
                    int pixelValue = GetGrayNumColor(mBitmap.GetPixel(j, i));//获取指定位置的灰度值
                    if (pixelValue < dgGrayValue)     //根据阈值划分
                    {
                        if (right.X < j) right.X = j;
                        if (right.Y < i) right.Y = i;

                        if (left.X > j) left.X = j;
                        if (left.Y > i) left.Y = i;
                    };
                };
            };
            if (left.X > right.X || left.Y > right.Y) 
            {
                MessageBox.Show("");
            }
            
            if (auto)
            {
                if (right.X - left.X >= mBitmap.Width - 1)//阈值不准确 无法区分 自动递归调整
                {
                    if (dgGrayValue ==0)
                    {
                        dgGrayValue = 255;
                    }
                    dgGrayValue--;
                    left.X = mBitmap.Width - 1; left.Y = mBitmap.Height - 1;
                    right.X = 0; right.Y = 0;
                    AutoGrayValue(ref left,ref right,ref dgGrayValue, CharsCount, auto);
                }

            }
        }



执行时

  Point posMax = new Point(mBitmap.Width -1 , mBitmap.Height -1);
  Point posMin = new Point(0, 0);

  AutoGrayValue(ref posMax, ref posMin, ref dgGrayValue, CharsCount, true);

执行后得到的posMin坐标经常比posMax的大,也就是在右下边,而不是期望的posMin在左上边,代码中的MessageBox没有弹出。 求以上代码的逻辑错误。



------解决方案--------------------
还是逐步的调试吧,你可以把每步的值打印出来,不弹框那肯定是 if (left.X > right.X || left.Y > right.Y) 得不到满足