2013-02-26 136 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