代码之家  ›  专栏  ›  技术社区  ›  harshit

用rgb图像创建hsv直方图

  •  0
  • harshit  · 技术社区  · 16 年前

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

    我试着探索jai,但它为rgb图像创建了直方图。 谢谢 哈希特

    5 回复  |  直到 11 年前
        1
  •  1
  •   Philippe Beaudoin    16 年前

    这是一个简单的rgb到hsv转换器的伪代码。它会发出 UNDEFINED 如果颜色为灰色,则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
  •  2
  •   Yogi    16 年前

    fir使用cv::cvtcolor将rgb转换为hsv 然后使用cv::calchist计算直方图

        3
  •  0
  •   Shervin Emami    16 年前

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

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

    然后可以使用我的rgb到hsv的代码,这比opencv的cvconvert函数要好: http://www.shervinemami.co.cc/colorConversion.html

    干杯,

    谢尔文·埃马米。

        4
  •  0
  •   sudhanshu    11 年前

    以下是执行此操作的代码:

          // 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
  •  0
  •   Achint    11 年前

    首先,必须使用cv::cvtcolor将图像转换为hsv图像才能将rgb图像转换为hsv图像,然后,可以使用cv::calchist计算hsv直方图。