Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV, how can we normalize a Mat min to max and max to min?

I want to no normalize a Mat to the min value goes to 255 and max goes to 0 (normalize the Mat between 0~255).

For example, if we have an array like [0.02, 0.002, 0.0002] after normalization I want to get a result like this: [3, 26, 255], but now when I am using NORM_MINMAX I got [255, 26, 3].

But I did not find any function to do the inverted operation of the NORM_MINMAX.

OpenCV, how can we normalize a Mat min to max and max to min?

I want to no normalize a Mat to the min value goes to 255 and max goes to 0 (normalize the Mat between 0~255).

For example, if we have an array like [0.02, 0.002, 0.0002] after normalization I want to get a result like this: [3, 26, 255], but now when I am using NORM_MINMAX I got [255, 26, 3].

But I did not find any function to do the inverted operation of the NORM_MINMAX.

OpenCV, how can we normalize a Mat min to max and max to min?

I want to normalize a Mat to the min value goes to 255 and max goes to 0 (normalize the Mat between 0~255).

For example, if we have an array like [0.02, 0.002, 0.0002] after normalization I want to get a result like this: [3, 26, 255], but now when I am using NORM_MINMAX I got [255, 26, 3].

But I did not find any function to do the inverted operation of the NORM_MINMAX.

Code used:

cv::Mat mat(10, 10, CV_64F);
mat.setTo(0);
mat.row(0) = 0.02;
mat.row(1) = 0.002;
mat.row(2) = 0.0002;
cv::normalize(mat, mat, 255, 0, cv::NORM_MINMAX);
mat.convertTo(mat, CV_8UC1);
std::cout << mat << std::endl;

Result is:

[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
  26,  26,  26,  26,  26,  26,  26,  26,  26,  26;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]

But I want the inverse of the above result.

When I subtract 255 from the mat like:

cv::subtract(255, mat, mat, mat); // the last mat acts as mask
std::cout << mat << std::endl;
Result is:

[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
 229, 229, 229, 229, 229, 229, 229, 229, 229, 229;
 252, 252, 252, 252, 252, 252, 252, 252, 252, 252;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]

OpenCV, how can we normalize a Mat min to max and max to min?

I want to normalize a Mat to the min value goes to 255 and max goes to 0 (normalize the Mat between 0~255).

For example, if we have an array like [0.02, 0.002, 0.0002] after normalization I want to get a result like this: [3, 26, 255], but now when I am using NORM_MINMAX I got [255, 26, 3].

But I did not find any function to do the inverted operation of the NORM_MINMAX.

Code used:

cv::Mat mat(10, 10, CV_64F);
mat.setTo(0);
mat.row(0) = 0.02;
mat.row(1) = 0.002;
mat.row(2) = 0.0002;
cv::normalize(mat, mat, 255, 0, cv::NORM_MINMAX);
mat.convertTo(mat, CV_8UC1);
std::cout << mat << std::endl;

Result is:

[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
  26,  26,  26,  26,  26,  26,  26,  26,  26,  26;
   3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]

But I want the inverse of the above result.

When I subtract 255 from the mat like:

cv::subtract(255, mat, mat, mat); // the last mat acts as mask
std::cout << mat << std::endl;

Result is: is:

[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
 229, 229, 229, 229, 229, 229, 229, 229, 229, 229;
 252, 252, 252, 252, 252, 252, 252, 252, 252, 252;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]