c++ 椭圆拟合之最小二乘法(图像处理)

椭圆拟合之最小二乘法

原理:可能以后的篇幅再详细写了,

代码:

QImage inputImage : 输入图像;
float
ellipse : 输出椭圆的各个参数
bool Vertical : 是否垂直
bool total : 椭圆是否有上下部分,还是只是椭圆的一段曲线

void Ellipse(QImage *inputImage,float* ellipse,bool Vertical,bool total)
{
    vector> XX_mats;
   // double x[2];
    vector x_mats;
    vector y_mats;

    int Min_x = 100000,Max_x = 0;
    float b = 100000;
    float Max_a = 0;
    float a;
    int y_Min = 0, y_Max = 0;
    float X0,Y0;



   //初始值

    float sum_x = 0;
    float sum_y = 0;
    int width = inputImage->width();
    int height = inputImage->height();

    for(int i = 25; ipixel(j,i)).red() == 255)
            {
                if(j>Max_x)
                {
                    Max_x = j;
                    y_Max = i;
                }
                if(jMax_a)
        {
            Max_a = juli;
        }
    }


     //a=Max_a;

    float tem;
    if(Vertical)
    {
        tem = a;
        a = b;
        b = tem;
    }

    float A = a*a;
    float B = b*b;


    float sum_0 = 0;
    for(int j = 0; j= 11)
                {
                    break;
                }
            }
        }

        sum_1 = learn*s1*sum_1;
        sum_2 = learn*s1*sum_2;
        sum_3 = learn*s1*sum_3;
        sum_4 = learn*s1*sum_4;
        sum_5 = learn*s1*sum_5;

        // 更新 参数theta

        X0 = X0 - sum_1;
        Y0 = Y0 - sum_2;
        a = a - sum_3;
        b = b - sum_4;
        theta = theta + sum_5;



        cout<<"sum:"<height()/2;i++)
    {
        for(int j = 25; jwidth()-10; j++)
        {

            /*
            float  y = sqrtf(b*b - ((b*b)/(a*a))*(j-X0)*(j-X0)) + Y0;
            y1 = Y0 - sqrtf(b*b - ((b*b)/(a*a))*(j-X0)*(j-X0));

            sum_0 += sqrtf(powf((y_mats[j] - y),2.0));
            if(y>0&&y<890)
            {
                inputImage->setPixel(j,int(y+0.3),qRgb(0,255,0));

            }
            if(y1>0&&y1<900)
                inputImage->setPixel(j,int(y1),qRgb(0,255,0));

            */
            float cx = j - X0;
            float cy = i - Y0;
            float x = cx * cos(theta) - cy * sin(theta);
            float y = cx * sin(theta) + cy * cos(theta);
            x = (x * x) / powf(a,2);
            y = (y * y) / powf(b,2);


            float result = x + y;


            if(abs(result -1)<0.01)
            {
                inputImage->setPixel(j,i,qRgb(0,255,0));
            }


        }
    }


    Show_Image(*inputImage);
    /*
     */




    ellipse[0] = X0;
    ellipse[1] = Y0;
    ellipse[2] = a;
    ellipse[3] = b;
    ellipse[4] = theta;


}

效果图:
c++ 椭圆拟合之最小二乘法(图像处理)_第1张图片
误差值:
c++ 椭圆拟合之最小二乘法(图像处理)_第2张图片

你可能感兴趣的:(c++ 椭圆拟合之最小二乘法(图像处理))