我有一张图像具有高强度区域,我想放大这些强度。我在 Matlab
中通过将 (0,255)
中的整数数组转换为 (0,1)
中的 float ,然后对每个值进行平方和最后乘以 255
并转换回整数。
如何在 openCV
中完成这样的事情?有没有办法逐个访问元素?即便如此,我想这将是低效的,并且想知道是否有 openCV
方法经过矢量化或其他优化来实现这一点。
最佳答案
给定输入灰度图像:
你的算法的结果是:
您可以:
- 使用
convertTo
进行转换和缩放。 - 使用元素级乘法
mul
对每个像素求平方,或使用pow
计算任意数。
这是简单的代码:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("path_to_image", IMREAD_GRAYSCALE);
imshow("Original", img);
// converting to float in (0,1)
img.convertTo(img, CV_32F, 1.0 / 255.0);
// power with an arbitrary number. Use 2 to square
pow(img, 2, img);
// multiplying by 255 and back to integer
img.convertTo(img, CV_8U, 255.0);
imshow("Result", img);
waitKey();
return 0;
}
关于c++ - 使用 openCV 放大高强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35075415/