如何使用OpenCV的直方图?

How to use OpenCV's histogram?

我正在努力在 OpenCV 中获取浮点数据的直方图:

cv::ocl::setUseOpenCL(true);
auto rows = 2048;
auto cols = 2064;
auto input_d = cv::UMat(rows, cols, CV_32F, cv::USAGE_ALLOCATE_DEVICE_MEMORY);
cv::UMat hist_d;
cv::randn(input_d, 0, 0.5);
std::vector<int> channels = { 0 };
std::vector<int> histSize = { 256 };
std::vector<float> ranges = { 0, 1 };//run the histogram to track values 0 to 1
cv::calcHist(input_d, channels, cv::UMat(), hist_d, histSize, ranges, false);

我收到如下错误:

OpenCV Error: Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in cv::Mat::Mat, file src\matrix.cpp, line 452

有人知道这个功能怎么用吗?

以下代码有效,但计算不会在 GPU 上进行

auto rows = 2048;
auto cols = 2064;
auto input_d = cv::Mat(rows, cols, CV_32F);
cv::MatND hist_d;
cv::randn(input_d, 0, 0.5);
int histSize[1] = { 256 };
float hranges[2] = { 0.0, 256.0 };
const float*  range[1] = { hranges };
int channels[1] = { 0 };
cv::calcHist(&input_d, 1, channels, cv::Mat(), hist_d, 1, histSize, range);

我怀疑 foo 的大小不应该为零,但我不明白这是怎么回事。

cv::InputArray& foo = input_d;
cv::calcHist(foo, channels, cv::UMat(), hist_d, histSize, ranges, false);

看来需要包装一下

std::vector<cv::UMat> foo = { input_d };// should ref count, and avoid a deep copy?
cv::calcHist(foo, channels, cv::UMat(), hist_d, histSize, ranges, false);

以下是我使用 cv::UMat 实现的方法:

std::vector<int> channels = { 0 };
std::vector<int> histSize = { 256 };
std::vector<float> range = { 0, 256 };
std::vector<cv::UMat> foo = { oInputMat };
cv::calcHist(foo, channels, cv::UMat(), oHistogram, histSize, range, false);