3

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

我试图将一些 C++ 代码翻译成 Java,但 JavaCV 没有像cvCVtPixToPlane.

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

4

1 回答 1

3

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

我给你我的代码片段以从 h 通道创建一维图:

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,我在这个片段中使用:

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

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

于 2012-06-28T16:38:40.067 回答