2010-04-10 27 views
0

在Java或OpenCv中有沒有辦法;最好是Java,我可以有一個HSV直方圖給RGB圖像。使用RGB圖像創建HSV直方圖

我試着探索JAI,但它創建RGB圖像的直方圖。 謝謝 Harshit

回答

1

這裏是一個簡單的RGB到HSV轉換器的僞代碼。它會給UNDEFINED一個h如果顏色是灰色的陰影,否則H爲6介於0和

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直方圖的每個bin轉換爲HSV bin。

2

冷杉使用CV :: cvtColor將RGB轉換爲HSV 然後用CV :: calcHist計算直方圖

0

這裏是做這樣的代碼:

 // 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); 
0

首先,你必須使用CV :: cvtColor爲RGB圖像轉換成HSV圖像,然後將圖像轉換爲HSV,您可以使用CV :: calcHist來計算HSV直方圖。