【问题标题】:creating HSV histogram using RGB image使用 RGB 图像创建 HSV 直方图
【发布时间】:2011-02-06 10:27:26
【问题描述】:

在 Java 或 OpenCv 中有没有办法;最好是Java,我可以有一个HSV直方图给出RGB图像。

我尝试探索 JAI,但它会为 RGB 图像创建直方图。 谢谢 恶搞

【问题讨论】:

    标签: java image-processing opencv


    【解决方案1】:

    这是一个简单的 RGB 到 HSV 转换器的伪代码。如果颜色是灰色阴影,它将给出UNDEFINED 的 H,否则 H 介于 0 和 6 之间。

    x = min(R, G, B);
    V = max(R, G, B);
    if (V == x) {
      H = UNDEFINED
      S = 0
    }
    else {
      if( R == x ) {
        f = G - B;
        i = 3;
      } else if( G == x ) {
        f = B - R;
        i = 5;
      } else {
        f = R - G;
        i = 1;
      }
      H = i - f /(V - x);
      S = (V - x)/V;
    }
    

    现在您可以转换所有像素并将它们分箱以构建 HSV 直方图,或者您可以将 RGB 直方图的每个分箱转换为 HSV 分箱。

    【讨论】:

      【解决方案2】:

      首先使用 cv::cvtColor 将 RGB 转换为 HSV 然后使用 cv::calcHist 计算直方图

      【讨论】:

        【解决方案3】:

        您可以使用“JavaCV”库直接从 Java 访问 OpenCV 函数:

        http://code.google.com/p/javacv/

        然后你可以使用我的 RGB 到 HSV 的代码,它比 OpenCV 的 cvConvert 函数更好: http://www.shervinemami.co.cc/colorConversion.html

        干杯,

        舍文·艾美美。

        【讨论】:

          【解决方案4】:

          这是执行此操作的代码:

                // Assume SourceImage is a Bitmap ARGB_8888
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Bitmap refImage = BitmapFactory.decodeFile(mBaseDir + "some_reference.jpg", options);
          
                Mat hsvRef = new Mat();
                Mat hsvSource = new Mat();
          
                Mat srcRef = new Mat(refImage.getHeight(), refImage.getWidth(), CvType.CV_8U, new Scalar(4));
                Utils.bitmapToMat(refImage, srcRef);
          
          
                Mat srcSource = new Mat(SourceImage.getHeight(), SourceImage.getWidth(), CvType.CV_8U, new Scalar(4));
                Utils.bitmapToMat(SourceImage, srcSource);
          
                /// Convert to HSV
                Imgproc.cvtColor(srcRef, hsvRef, Imgproc.COLOR_BGR2HSV);
                Imgproc.cvtColor(srcSource, hsvSource, Imgproc.COLOR_BGR2HSV);
          
                /// Using 50 bins for hue and 60 for saturation
                int hBins = 50;
                int sBins = 60;
                MatOfInt histSize = new MatOfInt( hBins,  sBins);
          
                // hue varies from 0 to 179, saturation from 0 to 255
                MatOfFloat ranges =  new MatOfFloat( 0f,180f,0f,256f );
          
                // we compute the histogram from the 0-th and 1-st channels
                MatOfInt channels = new MatOfInt(0, 1);
          
          
          
                Mat histRef = new Mat();
                Mat histSource = new Mat();
          
                ArrayList<Mat> histImages=new ArrayList<Mat>();
                histImages.add(hsvRef);
                Imgproc.calcHist(histImages,
                        channels,
                        new Mat(),
                        histRef,
                        histSize,
                        ranges,
                        false);
                Core.normalize(histRef,
                        histRef,
                        0,
                        1,
                        Core.NORM_MINMAX,
                        -1,
                        new Mat());
          
                histImages=new ArrayList<Mat>();
                histImages.add(hsvSource);
                Imgproc.calcHist(histImages,
                        channels,
                        new Mat(),
                        histSource,
                        histSize,
                        ranges,
                        false);
                Core.normalize(histSource,
                        histSource,
                        0,
                        1,
                        Core.NORM_MINMAX,
                        -1,
                        new Mat());
          
                double resp1 = Imgproc.compareHist(histRef, histSource, 0);
                double resp2 = Imgproc.compareHist(histRef, histSource, 1);
                double resp3 = Imgproc.compareHist(histRef, histSource, 2);
                double resp4 = Imgproc.compareHist(histRef, histSource, 3);
          

          【讨论】:

            【解决方案5】:

            首先,您必须使用 cv::cvtColor 将图像转换为 HSV,将 RGB 图像转换为 HSV 图像,然后,您可以使用 cv::calcHist 计算 HSV 直方图。

            【讨论】:

              猜你喜欢
              • 2012-06-25
              • 1970-01-01
              • 2013-11-20
              • 2010-12-14
              • 1970-01-01
              • 1970-01-01
              • 2016-02-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多