OpenCV Android无缝克隆错误:( - 1515)CV_MAT_TYPE(mtype)== m.type()

时间:2016-04-01 11:01:51

标签: java android opencv

我需要你的帮助。 我正在尝试使用OpenCV的seamlessClone()函数实现两个图像的简单混合。我得到一个例外,说:

  

错误:(-215)CV_MAT_TYPE(mtype)== m.type()

以下是它的源代码:

public class SeamlessClone extends AppCompatActivity {

    public static final String TAG = "Seamless Clone demo";

    static {
        if (!OpenCVLoader.initDebug()) {
            // Handle initialization error
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seamless_clone);

        Bitmap destinationBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lightning);
        Log.d(TAG, "bitmap: " + destinationBitmap.getWidth() + "x" + destinationBitmap.getHeight());


        Mat destination = new Mat(destinationBitmap.getHeight(),
            destinationBitmap.getWidth(), CvType.CV_8UC4);
        Utils.bitmapToMat(destinationBitmap, destination);
        Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.airplane);

        Log.d(TAG, "bitmap: " + sourceBitmap.getWidth() + "x" + sourceBitmap.getHeight());

        Mat source = new Mat(sourceBitmap.getHeight(),
            sourceBitmap.getWidth(), CvType.CV_8UC4);
        Utils.bitmapToMat(sourceBitmap, source);

        Mat mask = new Mat(source.rows(),source.cols(),source.depth(),Scalar.all(255));

        Point point = new Point(destination.cols()/2,destination.rows()/2);

        Mat result = new Mat(destinationBitmap.getHeight(),
            destinationBitmap.getWidth(), CvType.CV_8UC4);

        Photo.seamlessClone(source, destination, mask, point, result, Photo.NORMAL_CLONE);

        Utils.matToBitmap(result, destinationBitmap);

        ImageView imageView = (ImageView) findViewById(R.id.clone_result);

        imageView.setImageBitmap(destinationBitmap);
    }
}

以下是来自logcat的错误日志:

E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
E/cv::error(): OpenCV Error: Assertion failed (CV_MAT_TYPE(mtype) == m.type()) in void cv::_OutputArray::create(int, const int*, int, int, bool, int) const, file /Volumes/Linux/builds/master_pack-android/opencv/modules/core/src/matrix.cpp, line 2297
E/org.opencv.photo: photo::seamlessClone_10() caught cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/core/src/matrix.cpp:2297: error: (-215) CV_MAT_TYPE(mtype) == m.type() in function void cv::_OutputArray::create(int, const int*, int, int, bool, int) const
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hemisphere.opencvexample, PID: 1905
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hemisphere.opencvexample/com.example.hemisphere.opencvexample.SeamlessClone}:
CvException [org.opencv.core.CvException: cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/core/src/matrix.cpp:2297: error: (-215) CV_MAT_TYPE(mtype) == m.type() in function void cv::_OutputArray::create(int, const int*, int, int, bool, int) const
                                                                                ]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
    at android.app.ActivityThread.access$800(ActivityThread.java:155)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
    Caused by: CvException [org.opencv.core.CvException: cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/core/src/matrix.cpp:2297: error: (-215) CV_MAT_TYPE(mtype) == m.type() in function void cv::_OutputArray::create(int, const int*, int, int, bool, int) const
                                                                                ]
    at org.opencv.photo.Photo.seamlessClone_0(Native Method)
    at org.opencv.photo.Photo.seamlessClone(Photo.java:581)
    at com.example.hemisphere.opencvexample.SeamlessClone.onCreate(SeamlessClone.java:72)
    at android.app.Activity.performCreate(Activity.java:6010)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
    ... 10 more

我在代码中做错了吗?

提前致谢!

2 个答案:

答案 0 :(得分:0)

使用OpenCV和Objective-C ++我遇到了同样的错误。 我打开我的源,目的地和掩码图像为PNG。你需要乘坐Alpha设置。

为此,请在尝试执行克隆功能之前,使用OpenCV cvtColor函数(CV_RGBA2RGB代码)转换图像。

也许这也是你的问题?

答案 1 :(得分:0)

请尝试使用 3 个通道 (CvType.CV_8UC3) 而不是 4 个,希望它能够正常工作。