onPictureTaken()中的错误,问题是不能将RGB转换为GRAY,程序停止从cvtcolor()函数运行



嘿,伙计们,我在这个函数上遇到了麻烦,这个函数是在照片捕获后调用的,现在我想使用图像的数据进行距离变换。并计算对象的确切数量,我知道问题在哪里,但我无法解决它,我认为问题是在Imgproc。cvtColor(m, mGray, Imgproc.COLOR_BGRA2GRAY)当我调试它的程序停止运行在这一点上,所以请回答我,如果你发现了什么;提前谢谢你

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.i(TAG, "Divyesh Saving a bitmap to file");
        // The camera preview was automatically stopped. Start it again.
        mCamera.startPreview();
        Log.i(TAG, "Divyesh Start preview");
        mCamera.setPreviewCallback(this);
        Log.i(TAG, "Divyesh Start preview callback");
        // Write the image in a file (in jpeg format)
        try {
            FileOutputStream fos = new FileOutputStream(mPictureFileName);
            Log.i(TAG, "Divyesh FOS open");
            fos.write(data);
            Log.i(TAG, "Divyesh FOS write");
            Mat photo = new Mat();
            Log.i(TAG, "Divyesh Mat photo = new Mat()");
            photo.put(0, 0, data); 
            Log.i(TAG, "Divyesh photo.put");
            m = new Mat(photo.height(), photo.width(), CvType.CV_8U,new Scalar(4));
            Log.i(TAG, "Divyesh Mat value of M and height width");
            //Bitmap myBitmap32 = photo.copy(Bitmap.Config.ARGB_8888, true);
            //Utils.bitmapToMat(myBitmap32, m);

                    Imgproc.cvtColor(m, mGray, Imgproc.COLOR_BGRA2GRAY);
                    Log.i(TAG, "Divyesh CVTCOLOR");
                    Imgproc.distanceTransform(mGray, mBin, Imgproc.DIST_LABEL_PIXEL, 3);
                    Core.normalize(mBin, mBin, 0, 1., Core.NORM_MINMAX);
                    Imgproc.threshold(mBin, mBin, .5, 1., Imgproc.THRESH_BINARY);
                    mBin.convertTo(dist_8u, CvType.CV_8U);
                    hierarchy = new Mat();
                    temp = dist_8u;
                    Imgproc.findContours(dist_8u, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
                    fu = contours.size();
                    Log.i(TAG, fu + "finally got no of objects");

            fos.close();
        } catch (java.io.IOException e) {
            Log.e("PictureDemo", "Exception in photoCallback", e);
        }

    }
}

和LOGCAT

03-14 11:11:39.089: D/CameraBridge(2350): mStretch value: 1.0
03-14 11:11:39.109: D/JavaCameraView(2350): Preview Frame received. Frame size: 115200
03-14 11:11:39.149: D/CameraBridge(2350): mStretch value: 1.0
03-14 11:11:39.149: I/OCVSample::Activity(2350): onTouch event
03-14 11:11:39.159: I/Sample::Tutorial3View(2350): Taking picture
03-14 11:11:39.969: I/Sample::Tutorial3View(2350): Divyesh Saving a bitmap to file
03-14 11:11:40.209: I/Sample::Tutorial3View(2350): Divyesh Start preview
03-14 11:11:40.209: I/Sample::Tutorial3View(2350): Divyesh Start preview callback
03-14 11:11:40.219: I/Sample::Tutorial3View(2350): Divyesh FOS open
03-14 11:11:40.229: I/Sample::Tutorial3View(2350): Divyesh FOS write
03-14 11:11:40.229: I/Sample::Tutorial3View(2350): Divyesh Mat photo = new Mat()
03-14 11:11:40.229: I/Sample::Tutorial3View(2350): Divyesh photo.put
03-14 11:11:40.229: I/Sample::Tutorial3View(2350): Divyesh Mat value of M and height width
03-14 11:11:40.229: D/AndroidRuntime(2350): Shutting down VM

你没有提到捕获大小,但从Frame size日志我猜是320x240。在Android中,捕获通常不是RGBA,而是YV12;因此是size=width x height x bits_per_pixel = 320 x 240 x 1.5 = 115200。在这种情况下,你使用错误的转换格式,你应该使用1通道输入Mat(因为YV12被解释,错误地,平面由OpenCV),然后(这段代码是c++,但你得到的想法):

cv::Mat yv12_image(height, width, CV_8UC1, input_buffer);
cv::Mat rgb_image;
cv::cvtColor(yv12_image, rgb_image, CV_YUV2RGB_YV12);

(注意RGB和BGR)。我想我记得在cvtColor中没有从YUV到灰色的直接转换,在这种情况下,您可以遍行输入缓冲区并通过周围的任何公式转换它,这将是缓慢的,或者运行上面的代码(翻译成Java),然后从RGB到灰色做另一个cvtColor

相关内容

  • 没有找到相关文章

最新更新