3

我正在尝试对我在 android 中拥有的图像(位图)使用自适应阈值处理,这需要将其更改为 Mat 然后将其转换为灰度。

下面是我从创建位图开始的代码。它们被命名为裁剪,因为我刚刚裁剪了这张照片。

            Bitmap bmCrop = BitmapFactory.decodeStream(iStream);
            Bitmap bmThreshed = null;
            /*
             * Initialize the Mats
             */
            Mat threshed = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1, new Scalar(4));//, new Scalar(4)
            //Mat crop = new Mat();
            Mat crop = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1,new Scalar(4));//, new Scalar(4)
            /*
             * Convert the Mats to Grayscale
             */
            if(!threshed.empty())
                Imgproc.cvtColor(threshed, threshed, Imgproc.COLOR_RGB2GRAY,1);// CURRENTLY BREAKING HERE
            if(!crop.empty())
                Imgproc.cvtColor(crop, crop, Imgproc.COLOR_BGR2GRAY,1);         


            Utils.bitmapToMat(bmCrop, crop);

            // Mat src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C
            Imgproc.adaptiveThreshold(crop, threshed, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 8);

            Utils.matToBitmap(threshed, bmThreshed);
            bmThreshed = bmCrop;

它目前正在“Imgproc.cvtColor(threshed, threshed, Imgproc.COLOR_RGB2GRAY,1)”行中断,我尝试在 Mat 上进行 cvtColor。

以下是 LogCat 的输出:

05-28 11:25:21.172: E/cv::error()(32505): OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3414
05-28 11:25:25.697: D/AndroidRuntime(32505): Shutting down VM
05-28 11:25:25.707: W/dalvikvm(32505): threadid=1: thread exiting with uncaught exception (group=0x414b1930)
05-28 11:25:25.747: E/AndroidRuntime(32505): FATAL EXCEPTION: main
05-28 11:25:25.747: E/AndroidRuntime(32505): CvException [org.opencv.core.CvException: /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/color.cpp:3414: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
05-28 11:25:25.747: E/AndroidRuntime(32505): ]
05-28 11:25:25.747: E/AndroidRuntime(32505):    at org.opencv.imgproc.Imgproc.cvtColor_0(Native Method)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4017)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at com.activity.IMGP_Camera$1.onManagerConnected(IMGP_Camera.java:263)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at org.opencv.android.AsyncServiceHelper$1.onServiceConnected(AsyncServiceHelper.java:318)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1101)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1118)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at android.os.Handler.handleCallback(Handler.java:725)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at android.os.Looper.loop(Looper.java:137)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at android.app.ActivityThread.main(ActivityThread.java:5226)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at java.lang.reflect.Method.invokeNative(Native Method)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at java.lang.reflect.Method.invoke(Method.java:511)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-28 11:25:25.747: E/AndroidRuntime(32505):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

5

断言失败表明输入图像cvtColor()既没有 3 通道也没有 4 通道。问题是您threshed使用类型CV_8UC1(单通道灰度)进行初始化,然后使用代码调用cvtColor(),该代码RGB2GRAY需要 3 通道 RGB 图像。

您根本不需要调用cvtColor()threshed因为它已经处于灰度状态。而且,假设您的目标是将裁剪后的图像转换为灰度,您将需要在填充数据Mat进行颜色转换。因此,您的代码的相关部分可能如下所示:

        Mat threshed = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1, new Scalar(4));//, new Scalar(4)
        //Mat crop = new Mat();
        Mat crop = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1,new Scalar(4));//, new Scalar(4)

        Utils.bitmapToMat(bmCrop, crop);
        if(!crop.empty())
            Imgproc.cvtColor(crop, crop, Imgproc.COLOR_BGR2GRAY,1);

免责声明:我不熟悉 Java 或 OpenCV Java API,因此此代码可能需要调整。

于 2013-05-28T20:37:14.807 回答
4

这是我现在工作的代码。我遇到的问题是我没有在初始化垫之后和执行 cvtColor() 之前立即执行 Utils.bitmapToMat()。

            /*
             * Initialize the bitmaps
             */
            Bitmap bmCrop = BitmapFactory.decodeStream(iStream);
            Bitmap bmThreshed = bmCrop;
            /*
             * Initialize the Mats
             */
            Mat threshed = new Mat(bmCrop.getWidth(),bmCrop.getHeight(), CvType.CV_8UC1);
            Utils.bitmapToMat(bmCrop, threshed);

            Mat crop = new Mat(bmCrop.getWidth(),bmCrop.getHeight(), CvType.CV_8UC1);
            Utils.bitmapToMat(bmCrop, crop);
            /*
             * Convert the Mats to Grayscale
             */
            if(!threshed.empty())
                Imgproc.cvtColor(threshed, threshed, Imgproc.COLOR_RGB2GRAY);
            if(!crop.empty())
                Imgproc.cvtColor(crop, crop, Imgproc.COLOR_BGR2GRAY);           

            /*
             * Use Adaptive Thresholding on the grayscaled Mats
             * crop -> threshed
             * Mat src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C
             */
            Imgproc.adaptiveThreshold(crop, threshed, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 8);//15, 8 were original tests. Casey was 75,10

            Utils.matToBitmap(threshed, bmThreshed);
            bmThreshed = bmCrop;
于 2013-05-29T13:26:19.607 回答