0

我是这个网站的新手,如果我的帖子有任何错误,请告诉我。我有一些关于在 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. 浮动[] 最大值 = {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,想知道会不会因为除法而导致逻辑错误

使用的链接:

4

2 回答 2

0

//使用这个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);
}
于 2013-03-22T19:43:59.460 回答
0

您的错误在这部分:

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来纠正它。

我希望我对你有所帮助。总帐

于 2013-06-27T13:34:43.700 回答