2013-02-26 31 views
0

我是这个网站的新手,请让我知道如果我在我的帖子上犯了什么错误。 我有一些关于在javacv中计算和绘制直方图的问题。以下是我根据我搜索的一些信息编写的代码:JavaCV创建和绘制灰度单通道直方图

存在此错误,我得到:OpenCV错误:其中一个参数值超出范围(索引超出范围)未知功能,文件...... \ SRC \ OpenCV的\模块\核心的\ 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. 浮子[] MAX_VALUE = {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,不知道是否会引起逻辑错误,由于分割用

链接:

回答

0

//使用此 公共CvHistogram getHistogram(IplImage结构图像){//获得的直方图数据,输入已被转换成灰度预先

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); 
} 
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纠正。

我希望我帮你。 GL