#include <iostream>
#include <opencv2/core/core.hpp>
int main()
{
double min = -10;
double max = 10;
cv::Mat src(cv::Size(2, 2), CV_32FC1, cv::Scalar::all(0));
cv::Mat dst;
src.at<float>(0,0) = -3;
src.at<float>(1,1) = 3;
std::cout << "src: " << src << '\n';
dst = (src - min) / (max - min);
std::cout << "dst: " << dst << '\n';
return 0;
}
Normalize a matrix between 0 and 1 with my custom max and my custom min in OpenCV with C++
-
13-06-2023 - |
Question
I'd like to normalize a matrix between 0 and 1 with my custom max and my custom min in OpenCV with C++. This is my code:
#include <iostream>
#include <opencv2/core/core.hpp>
int main()
{
cv::Mat src(cv::Size(2, 2), CV_32FC1, cv::Scalar::all(0));
cv::Mat dst;
src.at<float>(0,0) = -3;
src.at<float>(1,1) = 3;
std::cout << "src: " << src << '\n';
cv::normalize(src, dst, 0, 1, cv::NORM_MINMAX, CV_32FC1); // Not I would like!
// I'd like to normalize between 0 and 1 but my minimum is -10 and my maximum
// is 10 and not between [-3, 3]!
std::cout << "dst: " << dst << '\n';
}
Solution
OTHER TIPS
You can do like this (using lambda function):
dst = src.clone();
std::for_each(dst.begin<float>(), dst.end<float>()
, [](float& ele) { ele = (10+ele)/20; });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow