读取直方图后出现未处理的异常(使用 calcHist 创建)

发布于 2024-12-26 11:01:28 字数 1704 浏览 1 评论 0原文

我试图从 OpenCV 中的彩色(3 通道)图像中获取直方图,但每次我执行这样的 calcHist 直方图:

//int histSize[3];
//float hranges[2];
//const float* ranges[3];
//int channels[3];

ColorHistogram::ColorHistogram() 
{
    // Prepare arguments for a color histogram
    histSize[0]= histSize[1]= histSize[2]= 256;
    hranges[0]= 0.0; // BRG range
    hranges[1]= 255.0;
    ranges[0]= hranges; // all channels have the same range
    ranges[1]= hranges;
    ranges[2]= hranges;
    channels[0]= 0; // the three channels
    channels[1]= 1;
    channels[2]= 2;
}

cv::MatND ColorHistogram::getHistogram(const cv::Mat &image)
{
    cv::MatND hist;
    // Compute histogram
    cv::calcHist(&image,
        1, // histogram of 1 image only
        channels, // the channel used
        cv::Mat(), // no mask is used
        hist, // the resulting histogram
        3, // it is a 3D histogram
        histSize, // number of bins
        ranges // pixel value range
        );
    return hist;
}

在此处输入图像描述

当我尝试将结果提供给例如 cv::minMaxLoc 时,我收到未处理的异常。

cv::Mat ColorHistogram::getHistogramImage(const cv::Mat &image){
    // Compute histogram first
    cv::MatND hist = getHistogram(image);
    // Get min and max bin values
    double maxVal=0;
    double minVal=0;
    cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0);
//....
}

编辑

我不知道这是否重要,但我在控制台中收到此错误:

OpenCV 错误:未知函数中断言失败 (img.dims <= 2), 文件 C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\stat.cpp, 第788行

和我的 imagedims = 2

I am trying to get histogram from my color (3-channel) image in OpenCV but every time I do the calcHist histogram like this :

//int histSize[3];
//float hranges[2];
//const float* ranges[3];
//int channels[3];

ColorHistogram::ColorHistogram() 
{
    // Prepare arguments for a color histogram
    histSize[0]= histSize[1]= histSize[2]= 256;
    hranges[0]= 0.0; // BRG range
    hranges[1]= 255.0;
    ranges[0]= hranges; // all channels have the same range
    ranges[1]= hranges;
    ranges[2]= hranges;
    channels[0]= 0; // the three channels
    channels[1]= 1;
    channels[2]= 2;
}

cv::MatND ColorHistogram::getHistogram(const cv::Mat &image)
{
    cv::MatND hist;
    // Compute histogram
    cv::calcHist(&image,
        1, // histogram of 1 image only
        channels, // the channel used
        cv::Mat(), // no mask is used
        hist, // the resulting histogram
        3, // it is a 3D histogram
        histSize, // number of bins
        ranges // pixel value range
        );
    return hist;
}

enter image description here

When I try to fed the result to for instance cv::minMaxLoc I get an unhandled exception.

cv::Mat ColorHistogram::getHistogramImage(const cv::Mat &image){
    // Compute histogram first
    cv::MatND hist = getHistogram(image);
    // Get min and max bin values
    double maxVal=0;
    double minVal=0;
    cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0);
//....
}

EDIT

I don't know if this is important but I get this error in console:

OpenCV Error: Assertion failed (img.dims <= 2) in unknown function,
file
C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\stat.cpp,
line 788

and my image's dims = 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-01-02 11:01:28

遗憾的是,您无法使用 3D 直方图调用 minMaxLoc(即 hist.dims == 3 为 true)。以下是 minMaxLoc 的代码:

void cv::minMaxLoc( InputArray _img, double* minVal, double* maxVal,
                Point* minLoc, Point* maxLoc, InputArray mask )
{
    Mat img = _img.getMat();
    CV_Assert(img.dims <= 2); // <-- This is the line that is asserting for you...

    minMaxIdx(_img, minVal, maxVal, (int*)minLoc, (int*)maxLoc, mask);
    if( minLoc )
        std::swap(minLoc->x, minLoc->y);
    if( maxLoc )
        std::swap(maxLoc->x, maxLoc->y);
}

您必须手动搜索 3D 直方图中的最小值和最大值。您也许可以使用 NAryMatIterator以帮助简化搜索。文档中有一个如何使用它的示例。另外,您可以找到我的相关答案 在这里

You can't call minMaxLoc with a 3D histogram (i.e., hist.dims == 3 is true) unfortunately. Below is the code for minMaxLoc:

void cv::minMaxLoc( InputArray _img, double* minVal, double* maxVal,
                Point* minLoc, Point* maxLoc, InputArray mask )
{
    Mat img = _img.getMat();
    CV_Assert(img.dims <= 2); // <-- This is the line that is asserting for you...

    minMaxIdx(_img, minVal, maxVal, (int*)minLoc, (int*)maxLoc, mask);
    if( minLoc )
        std::swap(minLoc->x, minLoc->y);
    if( maxLoc )
        std::swap(maxLoc->x, maxLoc->y);
}

You'll have to search for the min and max values in your 3D histogram manually. You may be able to use the NAryMatIterator to help ease the search. There is an example of how to use this with the documentation. Also, you can find my related answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文