在onPictureTaken(),问题是不能转换成RGB到GRAY错误,程序停止cvtcolor()函数运行? [英] Error in onPictureTaken(), issue is cant converting RGB to GRAY, program stop running from cvtcolor() function.?

查看:2223
本文介绍了在onPictureTaken(),问题是不能转换成RGB到GRAY错误,程序停止cvtcolor()函数运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨I M越来越麻烦这个功能,这个功能是捕捉照片后调用,现在我想用图像的数据为距离变换。和计算对象的确切数字,我知道在哪里的问题,但我不能解决这个问题,我认为问题出在Imgproc.cvtColor(男,mGray,Imgproc.COLOR_BGRA2GRAY)当我调试它程序停止在这一点上运行,所以请你回答我,如果你发现了什么;
谢谢你在前进

  @覆盖
    公共无效onPictureTaken(字节[]数据,相机摄像头){
        Log.i(TAGDivyesh保存到文件的位图);
        //摄像机preVIEW自动停止。再次启动它。
        mCamera.start preVIEW();
        Log.i(TAGDivyesh开始preVIEW);
        mCamera.set previewCallback(本);
        Log.i(TAGDivyesh开始preVIEW回调);
        //写在文件中的图像(JPEG格式)
        尝试{
            FOS的FileOutputStream =新的FileOutputStream(mPictureFileName);
            Log.i(TAG,Divyesh FOS开);            fos.write(数据);
            Log.i(TAGDivyesh FOS写);
            垫照片=新垫();
            Log.i(TAGDivyesh垫照片=新垫());
            photo.put(0,0,数据);
            Log.i(TAGDivyesh photo.put);            M =新垫(photo.height(),photo.width(),CvType.CV_8U,新标(4));
            Log.i(TAG,M和高度宽度Divyesh垫值);
            //位图myBitmap32 = photo.copy(Bitmap.Config.ARGB_8888,真);
            //Utils.bitmapToMat(myBitmap32,米);
                    Imgproc.cvtColor(男,mGray,Imgproc.COLOR_BGRA2GRAY);
                    Log.i(TAGDivyesh 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);
                    等级=新垫();
                    TEMP = dist_8u;
                    Imgproc.findContours(dist_8u,轮廓,层次,Imgproc.RETR_EXTERNAL,Imgproc.CHAIN​​_APPROX_SIMPLE);
                    福= contours.size();
                    Log.i(TAG,福+终于得到了没有对象);            fos.close();        }赶上(java.io.IOException异常五){
            Log.e(PictureDemo,异常在photoCallback,E);
        }
    }}

和logcat的

  11月3日至14日:11:39.089:D / CameraBridge(2350):mStr​​etch值:1.0
11月3日至一十四日:11:39.109:D / JavaCameraView(2350):preVIEW帧接收。帧大小:115200
11月3日至一十四日:11:39.149:D / CameraBridge(2350):mStr​​etch值:1.0
11月3日至一十四日:11:39.149:I / OCVSample ::活动(2350):onTouch事件
11月3日至一十四日:11:39.159:I /样品:: Tutorial3View(2350):拍照
11月3日至一十四日:11:39.969:I /样品:: Tutorial3View(2350):Divyesh保存一个位图到文件
11月3日至一十四日:11:40.209:I /样品:: Tutorial3View(2350):Divyesh开始preVIEW
11月3日至一十四日:11:40.209:I /样品:: Tutorial3View(2350):Divyesh开始preVIEW回调
11月3日至一十四日:11:40.219:I /样品:: Tutorial3View(2350):Divyesh FOS开放
11月3日至一十四日:11:40.229:I /样品:: Tutorial3View(2350):Divyesh FOS写
11月3日至一十四日:11:40.229:I /样品:: Tutorial3View(2350):Divyesh垫照片=新垫()
11月3日至一十四日:11:40.229:I /样品:: Tutorial3View(2350):Divyesh photo.put
11月3日至一十四日:11:40.229:I /样品:: Tutorial3View(2350):M和高度宽度Divyesh垫价值
11月3日至一十四日:11:40.229:D / AndroidRuntime(2350):关闭VM


解决方案

您不提捕捉大小,但是从帧大小登录我的猜测是320×240。在Android中捕获通常不RGBA但YV12;因此,尺寸=宽x高x bits_per_pixel = 320×240×1.5 = 115200 。在这种情况下,你用错了转换格式,你应该使用一个1路输入(因为YV12 PTED是错误地为平面间$ P $通过OpenCV的),然后(这code是C ++,但你的想法):

CV ::垫yv12_image(高度,宽度,CV_8UC1,input_buffer);
CV ::垫rgb_image;
CV :: cvtColor(yv12_image,rgb_image,CV_YUV2RGB_YV12);

(小心使用RGB和BGR的BTW)。我想,我记得有没有 cvtColor 从YUV格雷直接转换,在这种情况下,你可以通过任何公式行走输入缓冲区,并将其转换身边,这将是缓慢的,或者运行code以上(翻译成Java),然后做另一个 cvtColor 从RGB到灰色。

Hey guys i m getting trouble with this function, this function is called after photo capturing and now i want to use data of image for distance transform. and count the exact number of objects i know where is problem but i cant solve it, I think problem is at Imgproc.cvtColor(m, mGray, Imgproc.COLOR_BGRA2GRAY) when i debug it program stop running at this point so please answer me if you found something; Thank you in advance

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


    }

}

and 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

解决方案

You don't mention the capture size, but from the Frame size Log I guess is 320x240. In Android captures are typically not in RGBA but YV12; hence size=width x height x bits_per_pixel = 320 x 240 x 1.5 = 115200. In that case, you're using the wrong conversion formats, you should use a 1-channel input Mat (because a YV12 is interpreted, wrongly, as planar by OpenCV) and then (this code is C++ but you get the idea):

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

(Careful with RGB vs BGR btw). I think I recall there isn't direct conversion from YUV to Gray in cvtColor, in that case you can either walk the input buffer and convert it via any formula around, which would be slow, or run the code above (translated to Java) and then do another cvtColor from RGB to Gray.

这篇关于在onPictureTaken(),问题是不能转换成RGB到GRAY错误,程序停止cvtcolor()函数运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆