Thibaud David Thibaud David - 10 days ago 5
C++ Question

Undo consecutives convertTo operations

When applying this to a

cv::Mat


original.convertTo(new_image, -1, 1.2, -10);


How can I implement the opposite effect, to get back my original matrix?

I've tried lowering the scale parameter below 1, and inverting the beta parameter without getting desired effect.
I think I am missing something about how they works, but can't find out the answer.

Answer

The convertTo functions computes:

 new_image = original * alpha + beta
 new_image = original * 1.2   + (-10)

To get the inverse operation, you need to:

 old_image = (new_image - beta) / alpha;
 old_image = (new_image - (-10)) / 1.2;

Please note that the results may be saturated_cast<>'ed to fit the matrix type. In this case the operation is not reversible and produces an image different from the original one. This is very likely to happen for CV_8U matrices, but not for CV_64F matrices.

This is a simple snippet to shows how it works:

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    Mat original = ...

    double alpha = 1.2;
    double beta = -10.0;

    Mat new_image;
    original.convertTo(new_image, -1, alpha, beta);

    // // Revert the 'convertTo'
    Mat old_image;
    old_image = (new_image - beta) / alpha;

    // Check the difference
    Mat diff;
    absdiff(original, old_image, diff);

    return 0;
}
Comments