Skip to content

Change article for fitEllipseDirect function#26729

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
MaximSmolskiy:change-article-for-fitEllipseDirect-function
Jan 8, 2025
Merged

Change article for fitEllipseDirect function#26729
asmorkalov merged 1 commit intoopencv:4.xfrom
MaximSmolskiy:change-article-for-fitEllipseDirect-function

Conversation

@MaximSmolskiy
Copy link
Copy Markdown
Contributor

Pull Request Readiness Checklist

I believe that implementation of fitEllipseDirect function does not correspond to fitzgibbon1999 but to its improvement oy1998NumericallySD

Several arguments in favor of this:

  • fitzgibbon1999 suggests to calculate eigenvalues and eigenvectors for 6x6 matrix, but oy1998NumericallySD - for 3x3 matrix M. In implementation we calculate eigenvalues and eigenvectors for 3x3 matrix M
    M(0,0) = (DM(2,0) + (DM(2,3)*TM(0,0) + DM(2,4)*TM(1,0) + DM(2,5)*TM(2,0))/Ts)/2.;
    M(0,1) = (DM(2,1) + (DM(2,3)*TM(0,1) + DM(2,4)*TM(1,1) + DM(2,5)*TM(2,1))/Ts)/2.;
    M(0,2) = (DM(2,2) + (DM(2,3)*TM(0,2) + DM(2,4)*TM(1,2) + DM(2,5)*TM(2,2))/Ts)/2.;
    M(1,0) = -DM(1,0) - (DM(1,3)*TM(0,0) + DM(1,4)*TM(1,0) + DM(1,5)*TM(2,0))/Ts;
    M(1,1) = -DM(1,1) - (DM(1,3)*TM(0,1) + DM(1,4)*TM(1,1) + DM(1,5)*TM(2,1))/Ts;
    M(1,2) = -DM(1,2) - (DM(1,3)*TM(0,2) + DM(1,4)*TM(1,2) + DM(1,5)*TM(2,2))/Ts;
    M(2,0) = (DM(0,0) + (DM(0,3)*TM(0,0) + DM(0,4)*TM(1,0) + DM(0,5)*TM(2,0))/Ts)/2.;
    M(2,1) = (DM(0,1) + (DM(0,3)*TM(0,1) + DM(0,4)*TM(1,1) + DM(0,5)*TM(2,1))/Ts)/2.;
    M(2,2) = (DM(0,2) + (DM(0,3)*TM(0,2) + DM(0,4)*TM(1,2) + DM(0,5)*TM(2,2))/Ts)/2.;
    double det = fabs(cv::determinant(M));
    if (fabs(det) > 1.0e-10)
    break;
    eps = (float)(s/(n*2)*1e-2);
    }
    if( iter < 2 ) {
    Mat eVal, eVec;
    eigenNonSymmetric(M, eVal, eVec);
  • fitzgibbon1999 suggests to take eigenvector for positive eigenvalue, but oy1998NumericallySD - for positive cond = 4 * evec(1, :) .* evec(3, :) - evec(2, :) .ˆ 2. In implementation we calculate this cond and take eigenvector accordingly
    // Select the eigen vector {a,b,c} which satisfies 4ac-b^2 > 0
    double cond[3];
    cond[0]=(4.0 * eVec.at<double>(0,0) * eVec.at<double>(0,2) - eVec.at<double>(0,1) * eVec.at<double>(0,1));
    cond[1]=(4.0 * eVec.at<double>(1,0) * eVec.at<double>(1,2) - eVec.at<double>(1,1) * eVec.at<double>(1,1));
    cond[2]=(4.0 * eVec.at<double>(2,0) * eVec.at<double>(2,2) - eVec.at<double>(2,1) * eVec.at<double>(2,1));
    if (cond[0]<cond[1]) {
    i = (cond[1]<cond[2]) ? 2 : 1;
    } else {
    i = (cond[0]<cond[2]) ? 2 : 0;
    }
    double norm = std::sqrt(eVec.at<double>(i,0)*eVec.at<double>(i,0) + eVec.at<double>(i,1)*eVec.at<double>(i,1) + eVec.at<double>(i,2)*eVec.at<double>(i,2));
    if (((eVec.at<double>(i,0)<0.0 ? -1 : 1) * (eVec.at<double>(i,1)<0.0 ? -1 : 1) * (eVec.at<double>(i,2)<0.0 ? -1 : 1)) <= 0.0) {
    norm=-1.0*norm;
    }
    pVec(0) =eVec.at<double>(i,0)/norm; pVec(1) =eVec.at<double>(i,1)/norm;pVec(2) =eVec.at<double>(i,2)/norm;

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov asmorkalov self-requested a review January 8, 2025 09:14
@asmorkalov asmorkalov self-assigned this Jan 8, 2025
@asmorkalov asmorkalov added this to the 4.11.0 milestone Jan 8, 2025
@asmorkalov asmorkalov merged commit 38b8659 into opencv:4.x Jan 8, 2025
@MaximSmolskiy MaximSmolskiy deleted the change-article-for-fitEllipseDirect-function branch January 8, 2025 09:24
@asmorkalov asmorkalov mentioned this pull request Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants