智能车图像处理12-正入十字补线1

前言

这篇文章主要讲述基本的正入十字补线方法。

一、函数主体

void buzhongxian2()
{
    secondmid = 0;

    if (turepodaoflag == 0 && (countpodao == 0 || countpodao > 3) && rukuflag == 0 && (huandao_memory == 0 && huandao_memoryforleft == 0) && (threemode == 0 || threemode == 6 || threemode == 12 ) && haveleftku == 0)//|| threemode == 3 || threemode == 9
    {

        if (firstmid == 0)
        {

            int buzhongxianflag = 0;
            int min = 0;
            int max = 0;
            float leftk = 0;
            float rightk = 0;
            int fanhui = 0;
            int ewai = 0;

            int flaghang = 0;
            int zuida = 0;
            leftdownflag = 0;
            leftupflag = 0;
            rightdownflag = 0;
            rightupflag = 0;

            buzhongxianmin = 6;
            buzhongxianmax = 40;
            shizimax = 0;
            if (break_hangshu >= 22) buzhongxianmax = break_hangshu - 2;
            if (buzhongxianmax >= 43) buzhongxianmax = 43;




            find_leftup_point((int)(6), (int)(buzhongxianmax), CROSSROAD);

                        if (leftupflag == 1) { if (buzhongxianmin >= left_turn_up[0] && left_turn_up[0] >= 4) { buzhongxianmin = left_turn_up[0] - 2; } find_leftdown_point((int)(buzhongxianmin), (int)(left_turn_up[0] - 1), CROSSROAD); }
                        else if (leftupflag == 0) find_leftdown_point((int)(buzhongxianmin), (int)(buzhongxianmax), CROSSROAD);
                        find_rightup_point((int)(6), (int)(buzhongxianmax), CROSSROAD);
                        if (rightupflag == 1) { if (buzhongxianmin >= left_turn_up[0] && left_turn_up[0] >= 4) { buzhongxianmin = left_turn_up[0] - 2; } find_rightdown_point((int)(buzhongxianmin), (int)(right_turn_up[0] - 1), CROSSROAD); }
                        else if (rightupflag == 0) find_rightdown_point((int)(buzhongxianmin), (int)(buzhongxianmax), CROSSROAD);
                        // SetText("buzhongxianmin  " + buzhongxianmin);
                        //   SetText("buzhongxianmax  " + buzhongxianmax);
                        int tiaobianleft = 0;
                        int tiaobianright = 0;
                        int j;
                        if (leftdownflag == 1 && leftupflag == 1&& left_turn_down[0]>=0)
                        {
                            for (j = left_turn_up[0]; j > left_turn_down[0];j--)
                            {
                             if(My_Abs(L_black[j],L_black[j-1])>=8)
                                {
                                    tiaobianleft = 1;
                                    break;
                                }
                            }
                        }
                        if (rightdownflag == 1 && rightupflag == 1 && right_turn_down[0] >= 0)
                        {
                            for (j = right_turn_up[0]; j > right_turn_down[0]; j--)
                            {
                                if (My_Abs(R_black[j], R_black[j - 1]) >= 8)
                                {
                                    tiaobianright = 1;
                                    break;
                                }
                            }
                        }

                        if (leftdownflag == 1 && rightdownflag == 1 && leftupflag == 1 && rightupflag == 1
                            && right_turn_down[0] < right_turn_up[0]&& left_turn_down[0] < left_turn_up[0]
                            &&(tiaobianleft==1|| tiaobianright==1)
                            && right_turn_up[1] < left_turn_up[1]
                            )//如果同时发现4个拐点
                        {
                            if (left_turn_down[0] <= right_turn_down[0])
                                min = left_turn_down[0];
                            else
                                min = right_turn_down[0];
                            if (left_turn_up[0] <= right_turn_up[0])
                                max = right_turn_up[0];
                            else
                                max = left_turn_up[0];
                            if (left_turn_down[0] >= 2)
                            {
                                advanced_regression(1, left_turn_down[0] - 2, left_turn_down[0], left_turn_up[0], left_turn_up[0] + 2);
                                leftk = parameterB;
                            }
                            else
                            {
                                advanced_regression(1, 0, left_turn_down[0], left_turn_up[0], left_turn_up[0] + 2);
                                leftk = parameterB;
                            }
                            if (right_turn_down[0] >= 2)
                            {
                                advanced_regression(2, right_turn_down[0] - 2, right_turn_down[0], right_turn_up[0], right_turn_up[0] + 2);
                                rightk = parameterB;
                            }
                            else
                            {
                                advanced_regression(2,0, right_turn_down[0], right_turn_up[0], right_turn_up[0] + 2);
                                rightk = parameterB;
                            }
                            fanhui = panduanfuhao(leftk, rightk);

                           if (fanhui == 0)
                            {

                            if (left_turn_down[0] >= 2)
                               {
                                        advanced_regression(1, left_turn_down[0] - 2, left_turn_down[0], left_turn_up[0], left_turn_up[0] + 2);
                        if (left_turn_down[0] > 12) leftlinefuzhi(parameterA, parameterB, (int)(left_turn_down[0] - 2), (int)(left_turn_up[0] + 2));
                                       if(left_turn_down[0]<=12) leftlinefuzhi(parameterA, parameterB, 0, (int)(left_turn_up[0] + 2));
                    }
                            else
                                {
                                        advanced_regression(1, 0, left_turn_down[0], left_turn_up[0], left_turn_up[0] + 2);
                                        leftlinefuzhi(parameterA, parameterB, 0, (int)(left_turn_up[0] + 2));
                                 }

                            if (right_turn_down[0] >= 2)
                                 {
                                        advanced_regression(2, right_turn_down[0] - 2, right_turn_down[0], right_turn_up[0], right_turn_up[0] + 2);
                                if(right_turn_down[0]> 12)        R_blackfuzhi(parameterA, parameterB, (int)(right_turn_down[0] - 2), (int)(right_turn_up[0] + 2));
                                if (right_turn_down[0]<= 12) R_blackfuzhi(parameterA, parameterB, 0, (int)(right_turn_up[0] + 2));
                    }
                            else
                                 {
                                        advanced_regression(2, 0, right_turn_down[0], right_turn_up[0], right_turn_up[0] + 2);
                                        R_blackfuzhi(parameterA, parameterB,0, (int)(right_turn_up[0] + 2));
                                  }

                                zhongxianfuzhi((char)(min ), (char)(max ));
                                secondmid = 1;
                                leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0;
                                shizimax = max;
                            }
                            if (fanhui == 1) { leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0; };
                        }
                        if (secondmid == 0 && leftdownflag == 0 && rightdownflag == 0 && leftupflag == 1 && rightupflag == 1
                                && right_turn_up[1] < left_turn_up[1]&&My_Abs(right_turn_up[0], left_turn_up[0])<=15
                        )//按照两个上拐点补线
                        {
                            int minup = 0;
                            if (left_turn_up[0] <= right_turn_up[0])
                            {
                                max = right_turn_up[0];
                                minup = left_turn_up[0];
                            }
                            else
                            { max = left_turn_up[0];
                                minup = right_turn_up[0];
                            }
                find_leftup_point((int)(6), (int)(minup+5), CROSSROAD);
                find_rightup_point((int)(6), (int)(minup + 5), CROSSROAD);
                advanced_regression(1, left_turn_up[0] , left_turn_up[0] + 1, left_turn_up[0] + 3, left_turn_up[0] + 4);
                                leftk = parameterB;

                            advanced_regression(2, right_turn_up[0] , right_turn_up[0] + 1, right_turn_up[0] + 3, right_turn_up[0] + 4);
                                rightk = parameterB;

                            fanhui = panduanfuhao(leftk, rightk);

                            if (fanhui == 0 && ((calkuan[minup - 1] > 90 && minup > 5) || minup <= 5))
                            {

                                if (left_turn_up[0] >= 35)
                                {
                                    if(break_hangshu- left_turn_up[0]>4)

                                    advanced_regression(1, left_turn_up[0], left_turn_up[0] + 1, left_turn_up[0] + 3, left_turn_up[0] + 4);
                                    if (break_hangshu - left_turn_up[0]<= 4)
                                        regression(1, left_turn_up[0], left_turn_up[0] + 2);
                                }
                                if (left_turn_up[0] >= 20 && left_turn_up[0] <= 34)
                                { advanced_regression(1, left_turn_up[0], left_turn_up[0] + 1, left_turn_up[0] + 3, left_turn_up[0] + 4); }
                                if (left_turn_up[0] < 20)
                                { advanced_regression(1, left_turn_up[0] + 2, left_turn_up[0] + 3, left_turn_up[0] + 5, left_turn_up[0] + 6); }
                                //  regression(1, left_turn_up[0], left_turn_up[0] + 1);

                                leftlinefuzhi(parameterA, parameterB, 0, (int)(left_turn_up[0] + 1));
                                if (right_turn_up[0] >= 35)
                                {
                                   if(break_hangshu- right_turn_up[0]>4)

                                    advanced_regression(2, right_turn_up[0], right_turn_up[0] + 1, right_turn_up[0] + 3, right_turn_up[0] + 4);
                                    if (break_hangshu - right_turn_up[0]<= 4)
                                    regression(2, right_turn_up[0], right_turn_up[0] + 2);
                                }
                                if (right_turn_up[0] >= 20 && right_turn_up[0] <= 34)
                                { advanced_regression(2, right_turn_up[0], right_turn_up[0] + 1, right_turn_up[0] + 3, right_turn_up[0] + 4); }
                                if (right_turn_up[0] < 20)
                                { advanced_regression(2, right_turn_up[0] + 2, right_turn_up[0] + 3, right_turn_up[0] + 5, right_turn_up[0] + 6); }
                                // regression(2, right_turn_up[0], right_turn_up[0] + 1);
                                R_blackfuzhi(parameterA, parameterB, 0, (int)(right_turn_up[0] + 1));
                                zhongxianfuzhi(0, 69);
                                secondmid = 1;
                                leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0;
                                shizimax = max;
                            }
                if (fanhui == 1) { leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0; };

                        }

                        if (secondmid == 0 && leftdownflag == 0 && rightdownflag == 1 && leftupflag == 1 && rightupflag == 1
                             && right_turn_down[0] < right_turn_up[0]
                            && ( tiaobianright == 1)

                            && right_turn_up[1] < left_turn_up[1]
                                                               &&My_Abs(right_turn_up[0], left_turn_up[0])<=20
                             )//如果发现两个上拐点加右下拐点
                        {

                            min = right_turn_down[0];
                            max = right_turn_up[0];
                            ewai = left_turn_up[0];
                            if (right_turn_up[0] < left_turn_up[0]) zuida = left_turn_up[0];
                            if (right_turn_up[0] > left_turn_up[0]) zuida = right_turn_up[0];
                            advanced_regression(1, 3, 5, ewai , ewai + 2);
                            leftk = parameterB;
                            if (min >= 4)
                            { advanced_regression(2, min - 4, min - 2, max, max + 2); }
                            if (min < 4)
                            { advanced_regression(2, 1, 2, max, max + 2); }
                            rightk = parameterB;
                            fanhui = panduanfuhao(leftk, rightk);

                            if (fanhui == 0)
                            {

                                  advanced_regression(1, 3, 5, ewai, ewai + 2);
                                  leftlinefuzhi(parameterA, parameterB, (char)0, (char)(ewai ));
                                if (min >= 2)
                                { advanced_regression(2, min - 2, min, max, max + 2); }
                                if (min <2)
                                { advanced_regression(2, 0, 1, max, max + 2); }
                                if(min>= 12)
                                R_blackfuzhi(parameterA, parameterB, (char)(min ), (char)(max ));
                    if (min < 12)
                        R_blackfuzhi(parameterA, parameterB, (char)(0), (char)(max));
                    if (R_black[0] == 0 && R_black[1] == 0 && R_black[2] == 0 && R_black[3] == 0 && R_black[4] == 0)
                        R_blackfuzhi(parameterA, parameterB, (char)(0), (char)(max));

                    zhongxianfuzhi(0, 69);
                                  secondmid = 1;
                                    leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0;
                                shizimax = max;
                            }
                            if (fanhui == 1) { leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0; };
                        }
                        if (secondmid == 0 && leftdownflag == 1 && rightdownflag == 0 && leftupflag == 1 && rightupflag == 1
                           && left_turn_down[0] < left_turn_up[0]
                           && (tiaobianleft == 1 )

                           && right_turn_up[1] < left_turn_up[1]
                                                              &&My_Abs(right_turn_up[0], left_turn_up[0])<=20

                           )//如果发现两个上拐点加左下拐点
                        {
                            min = left_turn_down[0];
                            max = left_turn_up[0];
                            ewai = right_turn_up[0];
                            if (right_turn_up[0] < left_turn_up[0]) zuida = left_turn_up[0];
                            if (right_turn_up[0] > left_turn_up[0]) zuida = right_turn_up[0];
                            if (min >= 4)
                            { advanced_regression(1, min - 4, min - 2, max, max + 2); }
                            if (min < 4)
                            { advanced_regression(1, 1, 2, max, max + 2); }
                            leftk = parameterB;
                            advanced_regression(2, 3 ,5, ewai , ewai + 2);
                            rightk = parameterB;
                            fanhui = panduanfuhao(leftk, rightk);

                            if (fanhui == 0)
                            {

                                if (min >= 2)
                                { advanced_regression(1, min - 2, min, max, max + 2); }
                                if (min < 2)
                                { advanced_regression(1, 0, 1, max, max + 2); }
                                if(min>= 12)
                                leftlinefuzhi(parameterA, parameterB, (char)(min ), (char)(max ));
                    if (min < 12)
                        leftlinefuzhi(parameterA, parameterB, (char)(0), (char)(max));

                    if (L_black[0] == 186 && L_black[1] == 186 && L_black[2] == 186 && L_black[3] == 186 && L_black[4] == 186)
                        leftlinefuzhi(parameterA, parameterB, (char)(0), (char)(max));


                    advanced_regression(2, 3, 5, ewai, ewai + 2);
                                    R_blackfuzhi(parameterA, parameterB, (char)0, (char)(ewai ));
                                zhongxianfuzhi(0, 69);
                                secondmid = 1;
                                    leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0;
                                shizimax = max;
                            }
                            if (fanhui == 1) { leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0; };
                        }
                        if (secondmid == 0 && leftdownflag == 1 && rightdownflag == 1 && leftupflag == 1 && rightupflag == 0
                                && left_turn_down[0] < left_turn_up[0]
                             && (tiaobianleft == 1)

                                )//如果发现两个下拐点加左上  左上左下 右下
                          {

                              min = left_turn_down[0];
                              max = left_turn_up[0];
                              ewai = right_turn_down[0];
                              int zuixiao = 0;
                              if (right_turn_down[0] < left_turn_down[0]) zuixiao = right_turn_down[0];
                              if (right_turn_down[0] > left_turn_down[0]) zuixiao = left_turn_down[0];
                              advanced_regression(1, min - 4, min - 2, max, max + 2);
                              leftk = parameterB;
                              if(ewai>20) { advanced_regression(2, 5, 9, 14, 19); }
                              if (ewai > 10&& ewai<=20)
                              { advanced_regression(2, 3, 5, 7, 9); }
                              if (ewai <=10)
                              { advanced_regression(2,1,2,3,4); }
                              rightk = parameterB;
                              fanhui = panduanfuhao(leftk, rightk);

                  if (fanhui == 0)
                  {

                      advanced_regression(1, min - 4, min - 2, max, max + 2);

                      float leftparameterB = parameterB;
                      if (FMy_Abs(leftparameterB, 0) <= 2.3)
                      {
                          if (min >= 12)
                              leftlinefuzhi(parameterA, parameterB, (char)(min), (char)(max));
                          if (min < 12) leftlinefuzhi(parameterA, parameterB, (char)(0), (char)(max));

                          if (ewai > 20) { advanced_regression(2, 5, 9, 14, 19); }
                          if (ewai > 10 && ewai <= 20)
                          { advanced_regression(2, 3, 5, 7, 9); }
                          if (ewai <= 10)
                          { advanced_regression(2, 1, 2, 3, 4); }

                          R_blackfuzhi(parameterA, parameterB, 0, (int)(45));

                          zhongxianfuzhi(0, 45);
                          secondmid = 1;
                          leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0;
                          shizimax = max;
                      }
                  }
                              if (fanhui == 1) { leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0; };
                          }
                          if (secondmid == 0 && leftdownflag == 1 && rightdownflag == 1 && leftupflag == 0 && rightupflag == 1
                               && right_turn_down[0] < right_turn_up[0]
                              && ( tiaobianright == 1)

                               )//如果发现两个下拐点加右上拐点  右上右下 左下
                          {

                              min = right_turn_down[0];
                              max = right_turn_up[0];
                              ewai = left_turn_down[0];
                              int zuixiao = 0;
                              if (right_turn_down[0] < left_turn_down[0]) zuixiao = right_turn_down[0];
                              if (right_turn_down[0] > left_turn_down[0]) zuixiao = left_turn_down[0];
                              if (ewai > 20) { advanced_regression(1, 5, 9, 14, 19); }
                              if (ewai > 10 && ewai <= 20)
                              { advanced_regression(1, 3, 5, 7, 9); }
                              if (ewai <= 10)
                              { advanced_regression(1, 1, 2, 3, 4); }
                              leftk = parameterB;
                              advanced_regression(2, min - 4, min - 2, max, max + 2);

                  rightk = parameterB;
                              fanhui = panduanfuhao(leftk, rightk);

                              if (fanhui == 0)
                              {
                      advanced_regression(2, min - 4, min - 2, max, max + 2);

                      float rightparameterB = parameterB;
                      if (FMy_Abs(rightparameterB, 0) <= 2.3)
                      {

                          if (ewai > 20) { advanced_regression(1, 5, 9, 14, 19); }
                          if (ewai > 10 && ewai <= 20)
                          { advanced_regression(1, 3, 5, 7, 9); }
                          if (ewai <= 10)
                          { advanced_regression(1, 1, 2, 3, 4); }
                          leftlinefuzhi(parameterA, parameterB, (int)(0), (int)(45));
                          advanced_regression(2, min - 4, min - 2, max, max + 2);
                          if (min >= 12)
                              R_blackfuzhi(parameterA, parameterB,(char) min, (char)(max));
                          if (min < 12) R_blackfuzhi(parameterA, parameterB, 0, (char)(max));
                          zhongxianfuzhi(0, 45);
                          secondmid = 1;
                          leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0;
                          shizimax = max;
                      }
                              }
                              if (fanhui == 1) { leftdownflag = 0; rightdownflag = 0; leftupflag = 0; rightupflag = 0; };
                          }



                buzhongxianflag = 0;
                leftdownflag = 0;
                leftupflag = 0;
                rightdownflag = 0;
                rightupflag = 0;
                flaghang = 0;
                min = 0;
                max = 0;
                leftk = 0;
                rightk = 0;
                ewai = 0;
                zuida = 0;

        }
    }

    双十字初步处理开始
    //if (secondmid == 1)
    //{
    //    SetText("进入双子子判断");
    //    int seconmidleft = 0;
    //    int secondmidright =0;
    //    int sebeginright=0;
    //    int sebeginleft=0;
    //    for (i = 0; i < 10; i++)
    //    {
    //        if (R_black[i] <=4) secondmidright = secondmidright + 1;
    //        if (L_black[i] >=182) seconmidleft = seconmidleft + 1;
    //    }
    //    if (secondmidright >= 6)
    //    {
    //        SetText("进入双子子判断1");
    //        for (i = 3; i < 40; i++)
    //        {
    //            if (R_black[i] == 0 && R_black[i + 1] != 0 && R_black[i + 2] != 0)
    //            {
    //                sebeginright = i + 1;
    //                SetText("sebeginright" + sebeginright);
    //                break;
    //            }
    //        }
    //        if (i + 1 < 35)
    //        {
    //            advanced_regression(2, i + 3, i + 5, i + 7, i + 9);
    //            R_blackfuzhi(parameterA, parameterB, 0, i + 3);
    //            zhongxianfuzhi(0, i + 5);
    //            SetText("双十字补右线");
    //        }
    //    }
    //    if (seconmidleft >= 6)
    //    {
    //        SetText("进入双子子判断2");
    //        for (i = 3; i < 40; i++)
    //        {
    //            if (L_black[i] == 186 && L_black[i + 1] != 186 && L_black[i + 2] != 186)
    //            {
    //                sebeginleft = i + 1;
    //                SetText("sebeginleft" + sebeginleft);
    //                break;
    //            }
    //        }
    //        if (i + 1 < 35)
    //        {
    //            advanced_regression(1, i + 3, i + 5, i + 7, i + 9);
    //            leftlinefuzhi(parameterA, parameterB, 0, i + 3);
    //            zhongxianfuzhi(0, i + 5);
    //            SetText("双十字补左线");
    //        }
    //    }
    //}

    双十字初步处理结束
    //十字防断处理开始

    //if (firstmid == 1 || secondmid == 1)
    //{
    //    countbannothing = 1;  //三帧之内不能掉线
    //    for (i = 0; i < 69; i++)
    //    {
    //        LCenterrecord[i] = LCenter[i];
    //        leftrecord[i] = L_black[i];
    //        rightrecord[i] = R_black[i];
    //    }
    //}
    //if (countbannothing != 0) countbannothing = countbannothing + 1;
    //if (countbannothing == 5) countbannothing = 0;
    //if (countbannothing != 0) bannothing = 1;
    //if (countbannothing == 0) bannothing = 0;

    //if (bannothing == 1)
    //{
    //    if (R_black[11] < 5 && R_black[12] < 5 && R_black[13] < 5 && L_black[11] > 181 && L_black[12] > 181 && L_black[13] > 181)  //掉线了
    //    {
    //        for (i = 0; i < 69; i++)
    //        {
    //            LCenter[i] = (byte)LCenterrecord[i];
    //            L_black[i] = (byte)leftrecord[i];
    //            R_black[i] = (byte)rightrecord[i];
    //        }
    //        SetText("十字掉线补线第一种");
    //    }
    //    if (R_black[16] < 5 && R_black[17] < 5 && R_black[18] < 5 && L_black[16] > 181 && L_black[17] > 181 && L_black[18] > 181)  //掉线了
    //    {
    //        for (i = 0; i < 69; i++)
    //        {
    //            LCenter[i] = (byte)LCenterrecord[i];
    //            L_black[i] = (byte)leftrecord[i];
    //            R_black[i] = (byte)rightrecord[i];
    //        }
    //        SetText("十字掉线补线第二种");
    //    }
    //    if (R_black[1] < 1 && R_black[2] < 1 && R_black[3] < 1 && L_black[1] > 185 && L_black[2] > 185 && L_black[3] > 185)  //掉线了
    //    {
    //        for (i = 0; i < 69; i++)
    //        {
    //            LCenter[i] = (byte)LCenterrecord[i];
    //            L_black[i] = (byte)leftrecord[i];
    //            R_black[i] = (byte)rightrecord[i];
    //        }
    //        SetText("十字掉线补线第三种");
    //    }
    //}


    十字防断处理结束
}
//正入十字补线结束

二、辅助函数

void find_leftdown_point(int start_point, int end_point, int RoadName)
{
    int j;
    if (RoadName == CROSSROAD)
    {
        for (j = start_point; j <= end_point; j++)
        {


            if ((L_black[j]-L_black[j - 2]) <=0 && (L_black[j]-L_black[j - 1]) <= 0 && (L_black[j + 1] - L_black[j]) >=0&& (L_black[j ] - L_black[j-1]) >= -3 && (L_black[j] - L_black[j-2]) >= -5 && ((L_black[j + 2] - L_black[j]) >= 1|| (L_black[j + 3] - L_black[j]) >= 1|| (L_black[j + 4] - L_black[j]) >= 1)
            && leftflag[j - 2] == 1 && leftflag[j - 1] == 1 && leftflag[j] == 1) //找到突变点
            {
                int record = 0;
                int i;
                record = j;
                for (i = j; i >= 1; i--)
                {
                    if (L_black[i] == L_black[i - 1]) record = i - 1;
                    if (L_black[i] != L_black[i - 1]) break;
                }

                left_turn_down[0] = (int)(record);//数组里面没有第0行
                left_turn_down[1] = L_black[record]; //记住行与列
                leftdownflag = 1;

                 break; //找到突变点之后就跳出
            }

        }
    }
}
void find_rightdown_point(int start_point, int end_point, int RoadName)//得到拐点的行与列 int
{
    int j;
    if (RoadName == CROSSROAD)   //代表十字
    {
        for (j = start_point; j <= end_point; j++)
        {

            if (R_black[j - 2]- R_black[j] >=- 3 && R_black[j-1]- R_black[j ]>=- 3 && R_black[j - 2] - R_black[j] <=1 && R_black[j-1] - R_black[j ] <=1 && R_black[j]- R_black[j+1]>=0&&(R_black[j] - R_black[j + 2] >= 1|| R_black[j] - R_black[j + 3] >= 1)
            && rightflag[j - 2] == 1 && rightflag[j - 1] == 1 && rightflag[j] == 1) //样本为4行,图变差据
            {
                int record = 0;
                int i;
                record = j;
                for (i = j; i >= 1; i--)
                {
                    if (R_black[i] == R_black[i - 1]) record = i - 1;
                    if (R_black[i] != R_black[i - 1]) break;
                }
                right_turn_down[0] = (int)(record);//数组里面没有第0行
                right_turn_down[1] = R_black[record];
                    rightdownflag = 1;

                    break;                //记住具体的行与列

            }

        }
    }
}
void find_leftup_point(int start_point, int end_point, int RoadName)
{
    int j;
    if (RoadName == CROSSROAD)
    {
        if (end_point >= 45) end_point =45;


        // for (j = start_point; j <=end_point; j++)
        for (j = end_point; j >= start_point; j--)
        {
            if (L_black[j] > 0)
            {
                if (
                    (((L_black[j - 2] - L_black[j - 1] >= 4)&&j>=28)||(((L_black[j - 2] - L_black[j - 1] >= 4||((L_black[j - 2] - L_black[j - 1] >= 3)&& L_black[j - 2]==186)) &&( j>18&&j < 28))) || (((L_black[j - 2] - L_black[j - 1] >= 6) && j <= 18)) || (((L_black[j - 2] - L_black[j - 1] >= 3) && L_black[j - 2]==186))) &&
                    My_Abs(L_black[j], L_black[j - 1]) <= 4 && My_Abs(L_black[j + 1], L_black[j]) <= 4&&
                    ((L_black[j - 2] > 150 || L_black[j - 3] > 150 || L_black[j - 4] > 150 || L_black[j - 5] > 150 || L_black[j - 6] > 150)||(j-1)>=34||( (L_black[j - 2] > 120 || L_black[j - 3] > 120 || L_black[j - 4] > 120 || L_black[j - 5] > 120 || L_black[j - 6] > 120)&&j-1>=25))
                 && leftflag[j - 1] == 1 && leftflag[j] == 1 && leftflag[j + 1] == 1


                 )
                {
                    int record;
                    record = L_black[j - 1];
                    if ((j - 1)>15&&record <186)
                    {
                        left_turn_up[0] = (int)(j - 1);//数组里面没有第0行
                        left_turn_up[1] = L_black[j - 1];
                        if (left_turn_up[1] < 186)
                        {
                            leftupflag = 1;

                            break;
                        }
                    }
                    if((j-1)<=15)
                    {
                        left_turn_up[0] = (int)(j - 1);//数组里面没有第0行
                        left_turn_up[1] = L_black[j - 1];
                        if (left_turn_up[1] < 186)
                        {
                           leftupflag = 1;

                            break;
                        }
                    }
                }
            }
            //第二类
            if (L_black[j] > 0&&j>=35)
            {
                if (L_black[j - 2] - L_black[j - 1] >= 30&& My_Abs(L_black[j], L_black[j - 1]) <= 10 && My_Abs(L_black[j + 1], L_black[j]) <=10 && ( L_black[j - 2] > 150 || L_black[j - 3] > 150||L_black[j - 4] > 150 || L_black[j - 5] > 150 || L_black[j - 6] > 150|| L_black[j - 3] - L_black[j - 1] >= 30)
                 && leftflag[j - 1] == 1 && leftflag[j] == 1 && leftflag[j + 1] == 1)
                {
                    int record;
                    record = L_black[j - 1];
                    if ((j - 1) > 15 && record < 180)
                    {
                        left_turn_up[0] = (int)(j - 1);//数组里面没有第0行
                        left_turn_up[1] = L_black[j - 1];
                        if (left_turn_up[1] < 182)
                        {
                            leftupflag = 1;

                            break;
                        }
                    }
                    if ((j - 1) <= 15)
                    {
                        left_turn_up[0] = (int)(j - 1);//数组里面没有第0行
                        left_turn_up[1] = L_black[j - 1];
                        if (left_turn_up[1] < 182)
                        {
                           leftupflag = 1;

                            break;
                        }
                    }
                }
            }

        }
    }
}
void find_rightup_point(int start_point, int end_point, int RoadName)
{
    int j;
    if (RoadName == CROSSROAD)
    {
        //if (end_point >= break_hangshu && break_hangshu >= 2) end_point = break_hangshu - 2;
        if (end_point >= 45) end_point = 45;
        // for (j = start_point; j <=end_point; j++)


        for (j = end_point; j >= start_point; j--)
        {
            if (R_black[j] < 161)
            {
                if ((((R_black[j - 1] - R_black[j - 2]) >=5&&j-1<=12)|| ((R_black[j - 1] - R_black[j - 2]) >= 4 && j - 1 >=28) || ((R_black[j - 1] - R_black[j - 2]) >= 4 && j - 1 >12 && j - 1 <28))
                    && My_Abs(R_black[j], R_black[j - 1]) <= 3 && My_Abs(R_black[j + 1], R_black[j]) <= 3 && ((R_black[j - 2] <40 || R_black[j - 3] < 40 || R_black[j - 4] < 40 || R_black[j - 5] < 40 || R_black[j - 6] < 40)||(j-1)>=34)
               && rightflag[j - 1] == 1 && rightflag[j] == 1 && rightflag[j + 1] == 1)
                {
                    int record;
                    int recordhang;

                    record = R_black[j - 1];
                    recordhang = j - 1;

                        if ((j - 1) > 5 && record >4)
                        {
                            right_turn_up[0] = (int)(j - 1);
                            right_turn_up[1] = R_black[j - 1];
                            rightupflag = 1;

                            break;
                        }
                        if ((j - 1) <= 15)
                        {
                            right_turn_up[0] = (int)(j - 1);
                            right_turn_up[1] = R_black[j - 1];
                            rightupflag = 1;

                            break;
                        }

                }
            }
            //第二类
            if (R_black[j] < 161)
            {
                if ((R_black[j - 1] - R_black[j - 2]) >= 10 && My_Abs(R_black[j], R_black[j - 1]) <= 8 && My_Abs(R_black[j + 1], R_black[j]) <= 8&&(R_black[j - 2] < 40 || R_black[j - 3] < 40 || R_black[j - 4] < 40 || R_black[j - 5] < 40 || R_black[j - 6] < 40)
               && rightflag[j - 1] == 1 && rightflag[j] == 1 && rightflag[j + 1] == 1&&j>=20)
                {
                    int record;
                    record = R_black[j - 1];

                    if ((j - 1) > 15 && record > 4)
                    {
                        right_turn_up[0] = (int)(j - 1);
                        right_turn_up[1] = R_black[j - 1];
                        rightupflag = 1;

                        break;
                    }
                    if ((j - 1) <= 15)
                    {
                        right_turn_up[0] = (int)(j - 1);
                        right_turn_up[1] = R_black[j - 1];
                        rightupflag = 1;

                        break;
                    }
                }
            }
        }
    }
}

//求两数之差绝对值开始
int My_Abs(int a, int b)
{

            if ((a - b) > 0)
                return ((int)(a - b));
            else return ((int)(b - a));
}
//求两数之差绝对值结束

void advanced_regression(int type, int startline1, int endline1, int startline2, int endline2)
{
    int i = 0;
    int sumlines1 = endline1 - startline1;
    int sumlines2 = endline2 - startline2;
    int sumX = 0;
    int sumY = 0;
    float averageX = 0;
    float averageY = 0;
    float sumUp = 0;
    float sumDown = 0;
    if (type == 0)  //拟合中线
    {
        /**计算sumX sumY**/
        for (i = startline1; i < endline1; i++)
        {
            sumX += i;
            sumY += LCenter[i];
        }
        for (i = startline2; i < endline2; i++)
        {
            sumX += i;
            sumY += LCenter[i];
        }
        averageX =(float)( sumX / (sumlines1 + sumlines2));     //x的平均值
        averageY = (float)(sumY / (sumlines1 + sumlines2));     //y的平均值
        for (i = startline1; i < endline1; i++)
        {
            sumUp += (LCenter[i] - averageY) * (i - averageX);
            sumDown += (i - averageX) * (i - averageX);
        }
        for (i = startline2; i < endline2; i++)
        {
            sumUp += (LCenter[i] - averageY) * (i - averageX);
            sumDown += (i - averageX) * (i - averageX);
        }
        if (sumDown == 0) parameterB = 0;
        else parameterB = sumUp / sumDown;
        parameterA = averageY - parameterB * averageX;

    }
    else if (type == 1)     //拟合左线
    {
        /**计算sumX sumY**/
        for (i = startline1; i < endline1; i++)
        {
            sumX += i;
            sumY += L_black[i];
        }
        for (i = startline2; i < endline2; i++)
        {
            sumX += i;
            sumY += L_black[i];
        }
        averageX =(float)( sumX / (sumlines1 + sumlines2));     //x的平均值
        averageY = (float)(sumY / (sumlines1 + sumlines2));     //y的平均值
        for (i = startline1; i < endline1; i++)
        {
            sumUp += (L_black[i] - averageY) * (i - averageX);
            sumDown += (i - averageX) * (i - averageX);
        }
        for (i = startline2; i < endline2; i++)
        {
            sumUp += (L_black[i] - averageY) * (i - averageX);
            sumDown += (i - averageX) * (i - averageX);
        }
        if (sumDown == 0) parameterB = 0;
        else parameterB = sumUp / sumDown;
        parameterA = averageY - parameterB * averageX;
    }
    else if (type == 2)         //拟合右线
    {
        /**计算sumX sumY**/
        for (i = startline1; i < endline1; i++)
        {
            sumX += i;
            sumY += R_black[i];
        }
        for (i = startline2; i < endline2; i++)
        {
            sumX += i;
            sumY += R_black[i];
        }
        averageX =(float)( sumX / (sumlines1 + sumlines2));     //x的平均值
        averageY = (float)(sumY / (sumlines1 + sumlines2));     //y的平均值
        for (i = startline1; i < endline1; i++)
        {
            sumUp += (R_black[i] - averageY) * (i - averageX);
            sumDown += (i - averageX) * (i - averageX);
        }
        for (i = startline2; i < endline2; i++)
        {
            sumUp += (R_black[i] - averageY) * (i - averageX);
            sumDown += (i - averageX) * (i - averageX);
        }
        if (sumDown == 0) parameterB = 0;
        else parameterB = sumUp / sumDown;
        parameterA = averageY - parameterB * averageX;
    }

}

void leftlinefuzhi(float A, float B, int8 start_point, int8 end_point)
{
     int8 m;
     for (m = start_point; m <= end_point; m++)
     {
         if((B * m + A)>=255) L_black[m]=255;
         if((B * m + A)<=0) L_black[m]=0;
         else if(0<(B * m + A)&&(B * m + A)<255)L_black[m] = (int8)(B * m + A);
     }
}
 void R_blackfuzhi(float A, float B, int8 start_point, int8 end_point)
{
     int8 m;
     for (m = start_point; m <= end_point; m++)
     {
         if ((B * m + A) < 0) {R_black[m] = 0; }
       else if((B * m + A) >=0&&(B * m + A)<=255) R_black[m] = (int8)(B * m + A);
         if((B * m + A)>=255) R_black[m]=255;
     }
}
 void zhongxianfuzhi( int8 start_point, int8 end_point)
 {
             int8 m;
             for (m = start_point; m <= end_point; m++)
             {
                 LCenter[m] = (int8)(L_black[m]/2 + R_black[m]/ 2);
             }
 }
int panduanfuhao(float x,float y)
{
    int a;
    a=0;
    if(x<0&&y<0) a=1;
    if(x>=0&&y>=0) a=1;
    return a;
}

总结

1.配合上辅助函数就是完整代码,如有不懂
请留言哦。

你可能感兴趣的:(智能车图像处理,图像处理,c++,人工智能)