2012-09-28 46 views
2

我在Android中使用JavaCV。
在我的代碼,我已經創建了一個對象進行比較的圖像對象,並使用(OpenCV的食譜
http://code.google.com/p/javacv/source/browse/OpenCV2_Cookbook/src/opencv2_cookbook/chapter04/ImageComparator.scala?repo=examples
http://code.google.com/p/javacv/wiki/OpenCV2_Cookbook_Examples_Chapter_4類)一ImageComparator。如果我使用SD卡上的文件,比較器正在工作。Android JavaCV從相機創建IplImage以使用ColorHistogram

File referenceImageFile = new File(absPath1); // Read an image.  
IplImage reference = Util.loadOrExit(referenceImageFile,CV_LOAD_IMAGE_COLOR);  
comparator = new ImageComparator(reference);  
comparator = new ImageComparator(reference);  

但是從Camera Preview中,當我創建IplImage時它不起作用。在比較「分數」計算過程中,我得到以下異常。

score = referenceComparator.compare(grayImage)/ imageSize; java.lang.RuntimeException:/home/saudet/android/OpenCV-2.4.2/modules/core/src/convert.cpp:1196:error:(-215)i < src.channels()in function void cvSplit()對於CameraPreview我使用FacePreview中的代碼來創建IplImage.But它在grayScale中創建圖像。

int f = SUBSAMPLING_FACTOR; 
     if (grayImage == null || grayImage.width() != width/f 
       || grayImage.height() != height/f) { 
      grayImage = IplImage.create(width/f, height/f, IPL_DEPTH_8U, 1); 
     } 
     int imageWidth = grayImage.width(); 
     int imageHeight = grayImage.height(); 
     int dataStride = f * width; 
     int imageStride = grayImage.widthStep(); 
     ByteBuffer imageBuffer = grayImage.getByteBuffer(); 
     for (int y = 0; y < imageHeight; y++) { 
      int dataLine = y * dataStride; 
      int imageLine = y * imageStride; 
      for (int x = 0; x < imageWidth; x++) { 
       imageBuffer.put(imageLine + x, data[dataLine + f * x]); 
      } 
     } 

如何從相機創建Color IplImage以與ImageComparator一起使用?

回答

0

下面的代碼似乎工作正常。

public void onPreviewFrame(final byte[] data, final Camera camera) { 
      try { 
       Camera.Size size = camera.getParameters().getPreviewSize(); 
       processImage(data, size.width, size.height); 
       camera.addCallbackBuffer(data); 
      } catch (RuntimeException e) { 
       // The camera has probably just been released, ignore. 
       Log.d("Exception", " " + e); 
      } 
     } 
protected void processImage(byte[] data, int width, int height) { 
     score.clear(); 

     // First, downsample our image 
     int f = SUBSAMPLING_FACTOR; 
     IplImage _4image = IplImage.create(width, height, IPL_DEPTH_8U, f); 

     int[] _temp = new int[width * height]; 

     if (_4image != null) { 
      decodeYUV420SP(_temp, data, width, height); 
      _4image.getIntBuffer().put(_temp); 
     } 


     //bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     //bitmap.copyPixelsFromBuffer(_4image.getByteBuffer()); 


     Log.d("CompareAndroid", "processImage"); 
     int imageSize = _4image.width() * _4image.height(); 
     Iterator<ImageComparator> iterator = reference_List.iterator(); 
     // Compute histogram match and normalize by image size. 
     // 1 means perfect match. 

    while(iterator.hasNext()){ 
     score.add(((ImageComparator) iterator.next()).compare(_4image)/imageSize); 
    } 
     Log.d("CompareImages", "Score Size "+score.size()); 
     postInvalidate(); 
    } 

This code seems to be working fine. 

    private void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, 
      int height) { 
     int frameSize = width * height; 
     for (int j = 0, yp = 0; j < height; j++) { 
      int uvp = frameSize + (j >> 1) * width, u = 0, v = 0; 
      for (int i = 0; i < width; i++, yp++) { 
       int y = (0xff & ((int) yuv420sp[yp])) - 16; 
       if (y < 0) 
        y = 0; 
       if ((i & 1) == 0) { 
        v = (0xff & yuv420sp[uvp++]) - 128; 
        u = (0xff & yuv420sp[uvp++]) - 128; 
       } 
       int y1192 = 1192 * y; 

       int r = (y1192 + 1634 * v); 
       int g = (y1192 - 833 * v - 400 * u); 
       int b = (y1192 + 2066 * u); 

       if (r < 0) 
        r = 0; 
       else if (r > 262143) 
        r = 262143; 
       if (g < 0) 
        g = 0; 
       else if (g > 262143) 
        g = 262143; 
       if (b < 0) 
        b = 0; 
       else if (b > 262143) 
        b = 262143; 

     rgb[yp] = 0xff000000 | ((b << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((r >> 10) & 0xff); 
      } 
     } 
    } 
0

我沒有測試過,但這樣的事情應該工作:

IplImage yuvimage = IplImage.create(width, height * 3/2, IPL_DEPTH_8U, 2); 
IplImage rgbimage = IplImage.create(width, height, IPL_DEPTH_8U, 3); 
cvCvtColor(yuvimage, rgbimage, CV_YUV2BGR_NV21);