bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.5k stars 1.58k forks source link

how to stitch images exactly? #682

Closed huahua713 closed 7 years ago

huahua713 commented 7 years ago

Hi, I'm totally new to opencv. been searching online for more than 2 weeks but still no luck...

Can someone please tell me exactly how to stitch image using javacv?

I have added the following to my app level gradle compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.3.2'

and below is in onCreate

    MatVector imgs = new MatVector();
    String pathToFolder = Environment.getExternalStorageDirectory() + File.separator + "test" + File.separator;

    Mat mat1 = imread(pathToFolder + "img1.png");
    Mat mat2 = imread(pathToFolder + "img2.png");

    Mat pano = new Mat();
    Stitcher stitcher = Stitcher.createDefault(false);

    imgs.put(mat1, mat2);
    int status = stitcher.stitch(imgs, pano);

    if (status != Stitcher.OK) {
        Log.e(TAG, "Can't stitch images, error code = " + status);
    } else {
        imwrite(pathToFolder + "result.png", pano);
    }

I keep getting error code = 1 or OOM exception. any help is super appreciated. please!

img1.png and img2.png are downloaded from here under input images https://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/

saudet commented 7 years ago

There's a port of that sample here: https://github.com/bytedeco/sample-projects/tree/master/opencv-stitching

huahua713 commented 7 years ago

@saudet thank you so much for taking your time to help me!

yes, I basically copy the code from that example and put it in android.

for poc, I hardcoded most of the args passed from command line and hopefully I didn't miss any.

I don't understand why I'm getting Insufficient memory (Failed to allocate 1101915588 bytes) in void* cv::OutOfMemoryError(size_t), file /home/saudet/projects/bytedeco/javacpp-presets/opencv/cppbuild/android-arm/opencv-3.2.0/modules/core/src/alloc.cpp, line 52

both images are about 500-700k only. Do you have a working example for android?

huahua713 commented 7 years ago

@saudet app crashes on int status = stitcher.stitch(imgs, pano);

huahua713 commented 7 years ago

@saudet can you reopen it? I still cant get it to work... am I adding the correct gradle? what did I do wrong? I also do get other error like java.lang.RuntimeException: /home/saudet/projects/bytedeco/javacpp-presets/opencv/cppbuild/android-arm/opencv-3.2.0/modules/core/src/umatrix.cpp:380: error: (-215) u != 0 in function void cv::UMat::create(int, const int*, int, cv::UMatUsageFlags)

huahua713 commented 7 years ago

@saudet I know this isnt an issue/bug/improvement, but the question should be closed when the answer really solves my problem. I'm still struggling...help please

huahua713 commented 7 years ago

especially this has been a major headache for me in the past 2 weeks... :(

saudet commented 7 years ago

You just need more memory. Your device doesn't have enough memory.

huahua713 commented 7 years ago

@saudet thank you! but I feel I did something wrong. this is the pixel from google. if it crashes due to insufficient memory, i believe majority of android device won't work.

Am I referencing the right lib? compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.3.2'

I noticed the pom.xml in example has opencv-stitching but I can't manage to reference it or translate to gradle.

Many thanks!

saudet commented 7 years ago

Yes, this won't work for most devices. You'll need to use smaller images or some other algorithm that uses less memory.

huahua713 commented 7 years ago

@saudet I see....Thank you. Do you happen to have or know any java wrapper that does image stitching?