如何使用JavaCV创建HSV直方图

时间:2012-06-23 23:31:09

标签: scala opencv histogram javacv

我是JavaCV的新手,想要从图像中创建直方图。

我尝试将一些C ++代码翻译成Java,但JavaCV没有像cvCVtPixToPlane这样的方法。

有人可以帮我创建直方图吗?

1 个答案:

答案 0 :(得分:3)

必须运行OpenCV-Wiki中的翻译OpenCV-Code。

我给你的代码snipet来创建一个来自h通道的1D-Diagram:

private CvHistogram getHueHistogram(IplImage image){
    if(image==null || image.nChannels()<3) new Exception("Error!");
    IplImage hsvImage= cvCreateImage(image.cvSize(), image.depth(), 3);
    cvCvtColor(image, hsvImage, CV_BGR2HSV);
    // Split the 3 channels into 3 images
    IplImageArray hsvChannels = splitChannels(hsvImage);
    //bins and value-range
    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;
    IplImage mask = null;
    cvCalcHist(hsvChannels.position(0),hist, accumulate, null);
    return hist;
}

我的splitChannels-Method,我在这个snipet中使用过:

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

如果要从直方图中绘制图片,可以迭代这些图像。使用cvQueryHistValue_1D(),您可以从bin_i获得像素总和