我想用c++在OpenCV中用我的自定义最大值和自定义最小值规范化0到1之间的矩阵。这是我的代码:
#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';
}
#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;
}
你可以这样做(使用lambda函数):
dst = src.clone();
std::for_each(dst.begin<float>(), dst.end<float>()
, [](float& ele) { ele = (10+ele)/20; });