【问题标题】:JavaCV creating and drawing grayscale one channel histogramJavaCV创建和绘制灰度单通道直方图
【发布时间】:2013-06-27 21:34:43
【问题描述】:

我是这个网站的新手,如果我的帖子有任何错误,请告诉我。 我有一些关于在 javacv 中计算和绘制直方图的问题。以下是我根据搜索到的一些信息编写的代码:

我得到以下错误:OpenCV 错误:未知函数中的参数值之一超出范围(索引超出范围),文件......\src\opencv\modules \core\src\array.cpp,第 1691 行

private CvHistogram getHistogram(IplImage image) {//get histogram data, input has been converted to grayscale beforehand 


    IplImage[] hsvImage1 = {image};
    //bins and value-range
    int numberOfBins = 256;
    float minRange = 0.0f;
    float maxRange = 255.0f;
    // Allocate histogram object
    int dims = 1;
    int[] sizes = new int[]{numberOfBins};
    int histType = CV_HIST_ARRAY;
    float[] minMax = new float[]{minRange, maxRange};
    float[][] ranges = new float[][]{minMax};
    CvHistogram hist = cvCreateHist(dims, sizes, histType, ranges, 1); 
    cvCalcHist(hsvImage1, hist, 0, null);
    return hist;
}

private IplImage DrawHistogram(CvHistogram hist, IplImage image) {//draw histogram
    int scaleX = 1;
    int scaleY = 1;
    int i;
    float[] max_value = {0};
    int[] int_value = {0};
    cvGetMinMaxHistValue(hist, max_value, max_value, int_value, int_value);//get min and max value for histogram

    IplImage imgHist = cvCreateImage(cvSize(256, image.height() ),IPL_DEPTH_8U,1);//create image to store histogram
    cvZero(imgHist);
    CvPoint pts = new CvPoint(5);

    for (i = 0; i < 256; i++) {//draw the histogram 
        float value = opencv_legacy.cvQueryHistValue_1D(hist, i);
        float nextValue = opencv_legacy.cvQueryHistValue_1D(hist, i + 1);

        pts.position(0).x(i * scaleX).y(image.height() * scaleY);
        pts.position(1).x(i * scaleX + scaleX).y(image.height() * scaleY);
        pts.position(2).x(i * scaleX + scaleX).y((int)((image.height() - nextValue * image.height() /max_value[0]) * scaleY));
        pts.position(3).x(i * scaleX).y((int)((image.height() - value * image.height() / max_value[0]) * scaleY));
        pts.position(4).x(i * scaleX).y(image.height() * scaleY);
        cvFillConvexPoly(imgHist, pts.position(0), 5, CvScalar.RED, CV_AA, 0);
    }
    return imgHist;
}

我尝试搜索底部提供的几个链接,但是,每个链接都使用不同的语言,因此我不确定我是否已将它们正确转换为 java。老实说,我怀疑的事情很少,如果可以提供任何建议,我会很高兴,例如:

  1. float[] 最大值 = {0}; // 我参考了互联网,它帮助我解决了 cvGetMinMaxHistValue() 中的语法错误,不确定是否会导致逻辑错误

  2. pts.position(3).x(i * scaleX).y((int)((image.height() - value * image.height() / max_value[0]) * scaleY)); // 我把 int 向下转换为 pts 会识别的类型,还有一件事是 max_value[0] 为 0,想知道它是否会因为除法而导致逻辑错误

使用的链接:

【问题讨论】:

    标签: opencv histogram javacv grayscale


    【解决方案1】:

    //使用这个 public CvHistogram getHistogram(IplImage image) {//获取直方图数据,输入已经预先转为灰度

    IplImageArray hsvImage1 = splitChannels(image);
    //bins and value-range
    int numberOfBins = 256;
    float minRange = 0.0f;
    float maxRange = 255.0f;
    // Allocate histogram object
    int dims = 1;
    int[] sizes = new int[]{numberOfBins};
    int histType = CV_HIST_ARRAY;
    float[] minMax = new float[]{minRange, maxRange};
    float[][] ranges = new float[][]{minMax};
    
    CvHistogram hist = cvCreateHist(dims, sizes, histType, ranges, 1); 
    cvCalcHist(hsvImage1, hist, 0, null);
    return hist;
    

    }

    private IplImageArray splitChannels(IplImage hsvImage) {
        CvSize size = hsvImage.cvSize();
        int depth = hsvImage.depth();
        IplImage channel0 = cvCreateImage(size, depth, 1);
        IplImage channel1 = cvCreateImage(size, depth, 1);
        IplImage channel2 = cvCreateImage(size, depth, 1);
        cvSplit(hsvImage, channel0, channel1, channel2, null);
        return new IplImageArray(channel0, channel1, channel2);
    }
    

    【讨论】:

      【解决方案2】:

      你的错误在这部分:

      for (i = 0; i < 256; i++) {//draw the histogram 
          float value = opencv_legacy.cvQueryHistValue_1D(hist, i);
          float nextValue = opencv_legacy.cvQueryHistValue_1D(hist, i + 1);
      

      您使用i+1 并导致错误超出范围,您可以使用for 直到255 更正它。

      希望对你有所帮助。总帐

      【讨论】:

        猜你喜欢
        • 2020-04-23
        • 2019-06-01
        • 2012-06-25
        • 2020-04-15
        • 2014-07-28
        • 1970-01-01
        • 2013-10-12
        • 2014-08-20
        • 1970-01-01
        相关资源
        最近更新 更多