opencv - OpenCV,我们如何将Mat min最小化为max并将max最小化为min?

标签 opencv

我想将Mat规范化为最小值,最大为255,最大为0(规范化Mat 0〜255 之间)。

例如,如果规范化后我们有一个类似于[0.02, 0.002, 0.0002]的数组,我想得到这样的结果:[3, 26, 255],但是现在当我使用NORM_MINMAX时,我得到了[255, 26, 3]

但是我没有找到任何执行NORM_MINMAX的反向操作的函数。

使用的代码:

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;

结果是:
[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]

但是我想要上述结果的反面。

更新:当我从垫子上减去255时:
cv::subtract(255, mat, mat, mat); // the last mat acts as mask
std::cout << mat << std::endl;

结果是:
[  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]

最佳答案

我终于找到了计算方法,步骤如下:

通过使用反比例公式,我们可以轻松计算NORM_MINMAX的反比例

x = a*b/c

其中 a = mat元素的最小值, b = 255(最大值), c =我们要计算的元素。
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;
std::cout << mat<< std::endl;

// craete a mask
cv::Mat mask(mat.size(), CV_8U);
mask.setTo(0);
mask.row(0) = 255;
mask.row(1) = 255;
mask.row(2) = 255;

// find the min value
double min;
cv::minMaxLoc(mat, &min, nullptr, nullptr, nullptr, mask);
std::cout << "min=" << min << std::endl;

// unfortunately opencv divide operation does not support mask, so we need some extra steps to perform.
cv::Mat result, maskNeg;
cv::divide(min*255, mat, result); // this is the magic line
cv::bitwise_not(mask, maskNeg);
mat.copyTo(result, maskNeg);
std::cout << result << std::endl;

// convert to 8bit
result .convertTo(result , CV_8UC1);
std::cout << "the final result:" << std::endl;
std::cout << temp << std::endl;

和输出:
original mat
[0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02;
 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002;
 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002;
 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]

min=0.0002

the calculated min-max
[2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55;
 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
 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]

 the final result:
[  3,   3,   3,   3,   3,   3,   3,   3,   3,   3;
  26,  26,  26,  26,  26,  26,  26,  26,  26,  26;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
   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 - OpenCV,我们如何将Mat min最小化为max并将max最小化为min?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663432/

相关文章:

matlab - OpenCV函数类似于matlab的 "find"

c++ - OpenCV imread 默默失败的原因?

python - Keras CNN 自动编码器输入形状错误

java - 将 csv 数据作为 zipentry 对象移动到 zip 存档 - 构造函数 ZipEntry(Path) 未定义

python - 克服 python 中的 opencv CV_IO_MAX_IMAGE_PIXELS 限制

windows - 如何使用 mingw 启用 tbb 安装 opencv

c++ - 在opencv中访问多 channel 数组中的整个 channel

python - 将 h264 RTSP 流读入 p​​ython 和 opencv

python-3.x - 如何使用opencv多次更改图像中4个像素的颜色?

运行 cv2.Canny() 时 Emacs 中的 python opencv 错误