Skip to content

New computeECC function, and updated findTransformECC function to make gaussian filtering optional - #13837

Merged
alalek merged 5 commits into
opencv:3.4from
amithjkamath:test
Feb 22, 2019
Merged

alalek merged 5 commits into
opencv:3.4from
amithjkamath:test

Conversation

@amithjkamath

@amithjkamath amithjkamath commented Feb 15, 2019 •

Copy link
Copy Markdown
Contributor

resolves #12432

This pullrequest changes

The issue tracked in #12432 is resolved with the changes here, by adding a new function called computeECC as suggested by the issue author, and, by adding another optional argument to findTransformECC to control the size of the Gaussian filter (set to default of 5, to maintain backward compatibility; set to 1 to do no filtering, like the issue author would like). Tests and documentation strings are included as well.

This is my first pull request, and so pardon my tardiness.

@amithk-uc

Copy link
Copy Markdown

@alalek : Apologies if you're not the person to get in touch about this, but the error here: https://pullrequest.opencv.org/buildbot/builders/precommit_linux64/builds/20152/steps/Compare%20ABI%20dumps/logs/report-html seems to be that the abi has changed. This is intentional, as I added a new optional argument to the function. I didn't find anything specific about handling such changes in the contribute page: could you please help and let me know if there's any other change I need to make to make this test pass? Local build passes (Ubuntu 18.04.2 LTS) and the tests pass too (build/opencv_test_video). Thanks!

InputOutputArray warpMatrix, int motionType = MOTION_AFFINE,
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 50, 0.001),
InputArray inputMask = noArray());
InputArray inputMask = noArray(), int gaussFiltSize = 5);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to keep original overload too.
Try this:

CV_EXPORTS_W
double findTransformECC(InputArray templateImage, InputArray inputImage,
    InputOutputArray warpMatrix, int motionType,
    TermCriteria criteria,
    InputArray inputMask, int gaussFiltSize);

/** @overload */
CV_EXPORTS
double findTransformECC(InputArray templateImage, InputArray inputImage,
    InputOutputArray warpMatrix, int motionType = MOTION_AFFINE,
    TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 50, 0.001),
    InputArray inputMask = noArray());

... .cpp ...

double findTransformECC(InputArray templateImage, InputArray inputImage,
    InputOutputArray warpMatrix, int motionType,
    TermCriteria criteria,
    InputArray inputMask)
{
    return findTransformECC(..., inputMask, 5)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findTransformECC
*/

CV_EXPORTS_W double computeECC(const Mat& templateImage, const Mat& inputImage, const Mat& inputMask);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use InputArray in public API instead of direct Mat.

  2. inputMask
    An optional mask

    inputMask = noArray()

  3. which should be warped with the final warpMatrix in
    order to provide

    What?
    Re-check parameters documentation and their limitations (CV_8U/32F is not correct).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed #1 and #2. Not sure I understand #3. I used the same description for the parameters as the existing findTransformECC function. As long as both images are of the same type, and are single channel, we should be good, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter description uses undefined "warpMatrix" (not used in this function at all).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah: thank you again. Here's the latest version:

@PARAM inputImage single-channel input image to be warped to provide an image similar to
templateImage, same type as templateImage.

Comment thread modules/video/src/ecc.cpp Outdated
{
Scalar meanTemplate, sdTemplate;

meanStdDev(templateImage, meanTemplate, sdTemplate, inputMask);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is 4 spaces.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread modules/video/test/test_ecc.cpp Outdated
}

bool CV_ECC_Test_Compute::testCompute(int)
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets avoid using of legacy testing framework.

Just use Google Test:

TEST(Video_ECC_Test_Compute, accuracy)
{
    Mat testImg = (Mat_<float>(3, 3) << 1, 0, 0, 1, 0, 0, 1, 0, 0);
    Mat warpedImage = (Mat_<float>(3, 3) << 0, 1, 0, 0, 1, 0, 0, 1, 0);
    Mat_<unsigned char> mask = Mat_<unsigned char>::ones(testImg.rows, testImg.cols);
    double ecc = computeECC(warpedImage, testImg, mask);

    EXPECT_NEAR(ecc, -0.5f, 1e-5f);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: Thanks for writing out the Google Test version.

Comment thread modules/video/src/ecc.cpp Outdated
double cv::computeECC(InputArray templateImage, InputArray inputImage, InputArray inputMask)
{
Mat src = templateImage.getMat();
Mat dst = inputImage.getMat();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dst = inputImage

This doesn't look consistent.

It is better to declare parameters with underscore _ suffix (in .cpp file only, keep .hpp without this hack). For "Mat" use original names.

double cv::computeECC(InputArray templateImage_, InputArray inputImage_, InputArray inputMask)
{
    Mat templateImage = templateImage_.getMat();
    Mat inputImage = inputImage_.getMat();

Anyway, you don't need to extract Mat from InputArray. Just use "InputArray" directly:

  • .cols -> .cols()
  • .rows -> .rows()
  • or Mat::ones(src.rows, src.cols, ...) -> Mat::ones(templateImage.size(), ...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patient guidance. Done.

Comment thread modules/video/src/ecc.cpp Outdated
CV_Error( Error::StsUnmatchedFormats, "Both input images must have the same data type" );

if(inputMask.empty())
inputMaskMat = Mat::ones(src.rows, src.cols, CV_8U);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this.
Keep inputMask = noArray() - it is more faster.

Also cache countNonZero(inputMask) value (with override for the case of "no mask"):
int active_pixels = inputMask.empty() ? templateImage.size().area() : countNonZero(inputMask);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me. Thank you once again.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that python wrapper requires inputMask to be explicitly passed?

    (cc, warp_matrix) = cv2.findTransformECC(im1_gray,im2_gray,warp_matrix, warp_mode, criteria)
TypeError: Required argument 'inputMask' (pos 6) not found

@alalek

alalek commented Feb 19, 2019

Copy link
Copy Markdown
Member

Thank you for update!

It is better to put this patch into 3.4 branch (there are no strong dependencies on "master" features, also it makes support easier).
We will merge changes from 3.4 into master regularly (weekly/bi-weekly).

Please squash and rebase your commits:

  • change "base" branch of this PR: master => 3.4 (use "Edit" button near PR title)
  • rebase your commits from master onto 3.4 branch. For example:
    git rebase -i --onto upstream/3.4 upstream/master
    (check list of your commits, save and quit (Esc + "wq" + Enter)
    where upstream is configured by following this GitHub guide and fetched (git fetch upstream).
  • push rebased commits into source branch of your fork (with --force option)

Note: no needs to re-open PR, apply changes "inplace".
If you have any issues, let us know.

@amithjkamath
amithjkamath changed the base branch from master to 3.4 February 19, 2019 17:03
@amithjkamath

Copy link
Copy Markdown
Contributor Author

@alalek Don't know if I did it right, but I followed your instructions and I suppose the branch is set to 3.4 and the checks have passed. Please do let me know if there's anything else I can do. Thank you very much for your patience and reviews!

@alalek
alalek merged commit 4c94804 into opencv:3.4 Feb 22, 2019
@alalek alalek mentioned this pull request Feb 26, 2019
InputArray inputMask, int gaussFiltSize);

/** @overload */
CV_EXPORTS

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we need to use CV_EXPORTS_W here.

/cc @mixmixmix

SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
New computeECC function, and updated findTransformECC function to make gaussian filtering optional (opencv#13837)

* fix for opencv#12432 with doc and tests

* Added doc string for new parameter.

* Fixes suggested by Alalek for getting around ABI incompatibility.

* Update to docstring, to remove parameter that isn't relevant.

* More updates based on Alalek's usggestions.
@amithjkamath
amithjkamath deleted the test branch October 6, 2026 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants