0

我试图从我在图片上运行的直方图中获取最大值。我的直方图代码如下所示:(感谢堆栈溢出用户)

  public static CvHistogram getHueHistogram(IplImage image){

    if(image==null || image.nChannels()<3) new Exception("Error!");

    // Split the 3 channels into 3 images
    IplImageArray hsvChannels = splitChannels(image);

    //bins and value-range
    int numberOfBins=255;
    float minRange= 0f;
    float maxRange= 180f;

    // 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};
   int uniform = 1;
   CvHistogram hist = cvCreateHist(dims, sizes, histType, ranges, uniform);

   // Compute histogram
   int accumulate = 1;

   @SuppressWarnings("unused")
   IplImage mask = null;
   cvCalcHist(hsvChannels.position(0),hist, accumulate, null);
   return hist;
}

private static 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);
}

现在这是我的 get max 方法:

public static float getHistMaxValue (CvHistogram hist){

    float[] minValue = {0};// these exist from when I was testing the function, ignore them
    float[] maxValue = {0};
    int[] minId = {0}; // these exist from when I was testing the function, ignore them
    int[] maxId = {0};  
    cvGetMinMaxHistValue( hist, null, maxValue, null, maxId);

    System.out.printf("%2f", maxValue[0]);


    return maxValue[0];
}

现在在大多数情况下这工作正常,我的问题是每次运行程序时我的最大值从 1532、298027 和 298038 变化,即使直方图的图像保持不变。在获得直方图之前,我确实对图像进行了大量处理,但这些功能似乎工作正常。关于为什么这些结果往往会改变的任何想法?

4

0 回答 0