fnhdx
fnhdx

Reputation: 321

Opencv - polynomial function fitting

In opencv (or other c++ lib), is there a similar function like matlab fit which can do 3d polynomial surface fitting (i.e. f(x,y)= p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2). Thanks

Upvotes: 1

Views: 12656

Answers (2)

YScharf
YScharf

Reputation: 2037

There is an undocumented function in openCV (contrib.hpp) called cv::polyfit(). It takes as input a Mat of x coordinates and another Mat of y coordinates. Not very easy to use Mats for this but you can build a wrapper for sending a vector of cv::Point points.

vector <float> fitPoly(const vector <Point> &src, int order){
    Mat src_x = Mat(src.size(), 1, CV_32F);
    Mat src_y = Mat(src.size(), 1, CV_32F);
    for (int i = 0; i < src.size(); i++){
        src_x.at<float>(i, 0) = (float)src[i].x;
        src_y.at<float>(i, 0) = (float)src[i].y;
    }

    return cv::polyfit(src_x, src_y, order);
}

Upvotes: -1

LBerger
LBerger

Reputation: 623

I don't think there is a lib in opencv but you can do like that :

int main( int argc, char** argv )
{
Mat z    = imread("1449862093156643.jpg",CV_LOAD_IMAGE_GRAYSCALE);

Mat M = Mat_<double>(z.rows*z.cols,6);
Mat I=Mat_<double>(z.rows*z.cols,1);
for (int i=0;i<z.rows;i++)
    for (int j = 0; j < z.cols; j++)
    {
        double x=(j - z.cols / 2) / double(z.cols),y= (i - z.rows / 2) / double(z.rows);
        M.at<double>(i*z.cols+j, 0) = x*x;
        M.at<double>(i*z.cols+j, 1) = y*y;
        M.at<double>(i*z.cols+j, 2) = x*y;
        M.at<double>(i*z.cols+j, 3) = x;
        M.at<double>(i*z.cols+j, 4) = y;
        M.at<double>(i*z.cols+j, 5) = 1;
        I.at<double>(i*z.cols+j, 0) = z.at<uchar>(i,j);
    }
SVD s(M);
Mat q;
s.backSubst(I,q);
cout<<q;
imshow("Orignal",z);
cout<<q.at<double>(2,0);
Mat background(z.rows,z.cols,CV_8UC1);
for (int i=0;i<z.rows;i++)
    for (int j = 0; j < z.cols; j++)
    {
        double x=(j - z.cols / 2) / double(z.cols),y= (i - z.rows / 2) / double(z.rows);
        double quad=q.at<double>(0,0)*x*x+q.at<double>(1,0)*y*y+q.at<double>(2,0)*x*y;
        quad+=q.at<double>(3,0)*x+q.at<double>(4,0)*y+q.at<double>(5,0);
        background.at<uchar>(i,j) = saturate_cast<uchar>(quad);
    }
imshow("Simulated background",background);
waitKey();
return 0;
}

Original post is here

Upvotes: 4

Related Questions