如何使用OpenCV的直方图?

时间:2015-10-04 03:26:38

标签: c++ opencv opencv3.0

我很难在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不应该是0,但我不明白最新情况。

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

1 个答案:

答案 0 :(得分:2)

看起来需要包装。

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);