2

我在 Android 中使用 JavaCV。
在我的代码中,我创建了一个 ImageComparator(OpenCV CookBook 类
http://code.google.com/p/javacv/source/browse/OpenCV2_Cookbook/src/opencv2_cookbook/chapter04/ImageComparator.scala?repo=examples 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)对象并使用该对象来比较图像。如果我使用 SD 卡中的文件,则比较器正在工作。

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

但是从相机预览中,当我创建 IplImage 时它不起作用。在比较“分数”计算期间,我收到以下异常。

score = referenceComparator.compare(grayImage) / imageSize; java.lang.RuntimeException: /home/saudet/android/OpenCV-2.4.2/modules/core/src/convert.cpp:1196: 错误: (-215) i < src.channels() in function void cvSplit(const无效*,无效*,无效*,无效*,无效*)

对于 CameraPreview,我使用 FacePreview 中的代码来创建 IplImage。但它以灰度创建图像。

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 一起使用?

4

2 回答 2

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);
于 2012-10-13T06:23:31.897 回答
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);
            }
        }
    }
于 2012-10-05T10:31:52.953 回答