Skip to content

cvtColorTwoPlane throwing Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR) error #14477

Closed

Description

@kafan1986
  • OpenCV => 4.1.0
  • Operating System / Platform => Android arm
  • Compiler => .so library
Detailed description

Similar to JavaCamera2View, I am trying to convert the YUV_420_888 image to BGR Mat. I have modified the JavaCamera2View method (bgra) to following below. Imgproc.cvtColorTwoPlane(y_mat, uv_mat, mBgr, Imgproc.COLOR_YUV2BGR_NV21) is throwing error on my uvc camera based single board computer running android. It works fine on my android based mobile phone. Error logs are provided below.

Steps to reproduce
public Mat bgr() {
        Image.Plane[] planes = mImage.getPlanes();
        int w = mImage.getWidth();
        int h = mImage.getHeight();
        int chromaPixelStride = planes[1].getPixelStride();

        if (chromaPixelStride == 2) { // Chroma channels are interleaved
            ByteBuffer y_plane = planes[0].getBuffer();
            ByteBuffer uv_plane = planes[1].getBuffer();
            Mat y_mat = new Mat(h, w, CvType.CV_8UC1, y_plane);
            Mat uv_mat = new Mat(h / 2, w / 2, CvType.CV_8UC2, uv_plane);
            
Imgproc.cvtColorTwoPlane(y_mat, uv_mat, mBgr, Imgproc.COLOR_YUV2BGR_NV21); // ********* Crashes here ******

            return mBgr;
        } else { // Chroma channels are not interleaved
            byte[] yuv_bytes = new byte[w * (h + h / 2)];
            ByteBuffer y_plane = planes[0].getBuffer();
            ByteBuffer u_plane = planes[1].getBuffer();
            ByteBuffer v_plane = planes[2].getBuffer();

            y_plane.get(yuv_bytes, 0, w * h);

            int chromaRowStride = planes[1].getRowStride();
            int chromaRowPadding = chromaRowStride - w / 2;

            int offset = w * h;
            if (chromaRowPadding == 0) {
                // When the row stride of the chroma channels equals their width, we can copy
                // the entire channels in one go
                u_plane.get(yuv_bytes, offset, w * h / 4);
                offset += w * h / 4;
                v_plane.get(yuv_bytes, offset, w * h / 4);
            } else {
                // When not equal, we need to copy the channels row by row
                for (int i = 0; i < h / 2; i++) {
                    u_plane.get(yuv_bytes, offset, w / 2);
                    offset += w / 2;
                    if (i < h / 2 - 1) {
                        u_plane.position(u_plane.position() + chromaRowPadding);
                    }
                }
                for (int i = 0; i < h / 2; i++) {
                    v_plane.get(yuv_bytes, offset, w / 2);
                    offset += w / 2;
                    if (i < h / 2 - 1) {
                        v_plane.position(v_plane.position() + chromaRowPadding);
                    }
                }
            }

            Mat yuv_mat = new Mat(h + h / 2, w, CvType.CV_8UC1);
            yuv_mat.put(0, 0, yuv_bytes);
            Imgproc.cvtColor(yuv_mat, mBgr, Imgproc.COLOR_YUV2BGR_I420, 3);

            return mBgr;
            
        }
    }

Error Message:

Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xb3678000 in tid 13437

2019-04-22 02:31:20.866 13449-13449/? A/DEBUG: ABI: 'arm'
2019-04-22 02:31:20.866 13449-13449/? A/DEBUG: pid: 12779, tid: 13437, name: Camera2HandlerT  >>> com.ss.retailapp <<<
2019-04-22 02:31:20.866 13449-13449/? A/DEBUG: signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xb3678000
2019-04-22 02:31:20.866 13449-13449/? A/DEBUG:     r0  b3677fe1  r1  b51fc540  r2  b51fc550  r3  b51fc660
2019-04-22 02:31:20.866 13449-13449/? A/DEBUG:     r4  b51fc660  r5  b83fdb30  r6  b51fc6a0  r7  b51fc788
2019-04-22 02:31:20.866 13449-13449/? A/DEBUG:     r8  b51fc620  r9  000009e0  r10 00000040  r11 b51fc6e0
2019-04-22 02:31:20.866 13449-13449/? A/DEBUG:     ip  f45add8c  sp  b51fc4d0  lr  b51fc590  pc  d30021ba
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG: backtrace:
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #00 pc 002e61ba  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #01 pc 0026d1f5  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #02 pc 0026d6fb  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #03 pc 0026d58b  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #04 pc 00787263  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so (tbb::internal::custom_scheduler<tbb::internal::IntelSchedulerTraits>::local_wait_for_all(tbb::task&, tbb::task*)+266)
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #05 pc 00780695  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so (tbb::internal::arena::process(tbb::internal::generic_scheduler&)+68)
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #06 pc 007840f1  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so (tbb::internal::market::process(rml::job&)+32)
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #07 pc 00784d31  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so (tbb::internal::rml::private_worker::run()+68)
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #08 pc 00784ce5  /data/app/com.ss.retailapp-3L43PIzkF5FQYmOhkCNBJg==/lib/arm/libopencv_java4.so (tbb::internal::rml::private_worker::thread_routine(void*)+4)
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #09 pc 00063c35  /system/lib/libc.so (__pthread_start(void*)+22)
2019-04-22 02:31:20.869 13449-13449/? A/DEBUG:     #10 pc 0001e085  /system/lib/libc.so (__start_thread+22)

Edit: Image plane information updated below.

ImageImage object values: getFormat(): 35 , getWidth(): 2560 , getHeight(): 960
planes[0] : getPixelStride(): 1 , getRowStride(): 2560 , getBuffer().isDirect(): true
planes[1] : getPixelStride(): 2 , getRowStride(): 2560 , getBuffer().isDirect(): true
planes[2] : getPixelStride(): 2 , getRowStride(): 2560 , getBuffer().isDirect(): true

Activity

  1. alalek commented on May 4, 2019

    @alalek
    Member

    Please collect this information from your case:

    • Image object values: .getFormat(), .getWidth(), .getHeight()
    • from attached Image.Plane objects (for each plane): .getPixelStride(), .getRowStride(), .getBuffer().isDirect()

    relates #14342
    /cc @Victorlouisdg @floe

  2. kafan1986 commented on May 4, 2019

    @kafan1986
    Author

    Please collect this information from your case:

    • Image object values: .getFormat(), .getWidth(), .getHeight()
    • from attached Image.Plane objects (for each plane): .getPixelStride(), .getRowStride(), .getBuffer().isDirect()

    relates #14342
    /cc @Victorlouisdg @floe

    Here are the values that was asked:

    ImageImage object values: getFormat(): 35 , getWidth(): 2560 , getHeight(): 960
    planes[0] : getPixelStride(): 1 , getRowStride(): 2560 , getBuffer().isDirect(): true
    planes[1] : getPixelStride(): 2 , getRowStride(): 2560 , getBuffer().isDirect(): true
    planes[2] : getPixelStride(): 2 , getRowStride(): 2560 , getBuffer().isDirect(): true
    
  3. Victorlouisdg commented on May 4, 2019

    @Victorlouisdg
    Contributor

    @kafan1986 can you confirm the regular rgba() method works correctly?

    How did you initialize the mBgr Mat ?

  4. kafan1986 commented on May 4, 2019

    @kafan1986
    Author

    mBgr

    mBgr = new Mat();

  5. alalek commented on May 4, 2019

    @alalek
    Member

    35

    It is YUV_420_888. I don't see any suspicious values of these fields.

    Did this work?

    • get RGBA
    • cvtColor(mRGBa, mRGB, COLOR_RGBA2RGB);

    If yes, then there is some bug in OpenCV. if there is problem on the first step, then the bug is likely in Android camera module.

    Next level of investigation is debugger:

    • build OpenCV binaries with debug information: build_sdk.py --debug)
    • disable threading: setNumThreads(1);
    • hopefully Android Studio can help with debugging of native C++ code.
  6. kafan1986 commented on May 4, 2019

    @kafan1986
    Author

    35

    It is YUV_420_888. I don't see any suspicious values of these fields.

    Did this work?

    • get RGBA
    • cvtColor(mRGBa, mRGB, COLOR_RGBA2RGB);

    If yes, then there is some bug in OpenCV. if there is problem on the first step, then the bug is likely in Android camera module.

    Next level of investigation is debugger:

    • build OpenCV binaries with debug information: build_sdk.py --debug)
    • disable threading: setNumThreads(1);
    • hopefully Android Studio can help with debugging of native C++ code.

    RGBA throws similar error too. The grayscale image (Y channel only) works fine though. Should I write and share ByteBuffer data of all 3 channels for you take a look at?

  7. alalek commented on May 4, 2019

    @alalek
    Member
     Mat uv_mat = new Mat(h / 2, w / 2, CvType.CV_8UC2, uv_plane);
    +Mat uv_mat_clone = uv_mat.clone();        // <---- just check, does this crash?
  8. kafan1986 commented on May 4, 2019

    @kafan1986
    Author
    Mat uv_mat_clone = uv_mat.clone(); 

    Yes, it does crash at this line Mat uv_mat_clone = uv_mat.clone();

    New error log is as below:

    Details
    2019-04-21 21:44:37.103 6453-6453/? A/DEBUG: ABI: 'arm'
    2019-04-21 21:44:37.103 6453-6453/? A/DEBUG: pid: 6067, tid: 6439, name: Camera2HandlerT  >>> com.synapticsense.retailapp <<<
    2019-04-21 21:44:37.103 6453-6453/? A/DEBUG: signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xce985000
    2019-04-21 21:44:37.103 6453-6453/? A/DEBUG:     r0  cf12bfc0  r1  ce984fe1  r2  00000000  r3  00000000
    2019-04-21 21:44:37.103 6453-6453/? A/DEBUG:     r4  ffffffff  r5  0012c000  r6  e8cb4d80  r7  ceef7d70
    2019-04-21 21:44:37.103 6453-6453/? A/DEBUG:     r8  ceef7ca8  r9  ce859001  r10 ceef7ea0  r11 b257aa00
    2019-04-21 21:44:37.103 6453-6453/? A/DEBUG:     ip  c91bacc0  sp  ceef7c90  lr  c88adadd  pc  e9aa6e38
    2019-04-21 21:44:37.120 2815-3000/? I/[Gralloc]: framebuffer hal alread move to hwcomposer
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG: backtrace:
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #00 pc 00019e38  /system/lib/libc.so (__memcpy_base+112)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #01 pc 001e7ad9  /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/lib/arm/libopencv_java3.so (cv::Mat::copyTo(cv::_OutputArray const&) const+432)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #02 pc 000d0dd9  /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/lib/arm/libopencv_java3.so (Java_org_opencv_core_Mat_n_1clone+72)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #03 pc 0041c279  /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #04 pc 00417d75  /system/lib/libart.so (art_quick_invoke_stub_internal+68)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #05 pc 003f13eb  /system/lib/libart.so (art_quick_invoke_static_stub+222)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #06 pc 000a1043  /system/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+154)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #07 pc 001e8835  /system/lib/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+232)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #08 pc 001e3511  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+776)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #09 pc 003eceeb  /system/lib/libart.so (MterpInvokeStatic+130)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #10 pc 0040ab94  /system/lib/libart.so (ExecuteMterpImpl+14612)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #11 pc 00018850  /dev/ashmem/dalvik-classes3.dex extracted in memory from /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/base.apk!classes3.dex (deleted) (org.opencv.core.Mat.clone+8)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #12 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #13 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #14 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #15 pc 003ebf0f  /system/lib/libart.so (MterpInvokeVirtual+442)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #16 pc 0040aa14  /system/lib/libart.so (ExecuteMterpImpl+14228)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #17 pc 0003eaaa  /dev/ashmem/dalvik-classes2.dex extracted in memory from /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/base.apk!classes2.dex (deleted) (com.synapticsense.retailapp.VisionClasses.CameraImageFrame.bgr+678)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #18 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #19 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #20 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.188 6453-6453/? A/DEBUG:     #21 pc 003ebf0f  /system/lib/libart.so (MterpInvokeVirtual+442)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #22 pc 0040aa14  /system/lib/libart.so (ExecuteMterpImpl+14228)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #23 pc 0003ef5e  /dev/ashmem/dalvik-classes2.dex extracted in memory from /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/base.apk!classes2.dex (deleted) (com.synapticsense.retailapp.VisionClasses.CameraImageFrame.newgray+14)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #24 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #25 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #26 pc 001e4377  /system/lib/libart.so (bool art::interpreter::DoCall<true, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+674)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #27 pc 003ed149  /system/lib/libart.so (MterpInvokeVirtualRange+424)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #28 pc 0040ad14  /system/lib/libart.so (ExecuteMterpImpl+14996)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #29 pc 0003dca0  /dev/ashmem/dalvik-classes2.dex extracted in memory from /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/base.apk!classes2.dex (deleted) (com.synapticsense.retailapp.VisionClasses.CameraClass.processImageMat+1264)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #30 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #31 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #32 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #33 pc 003ecda1  /system/lib/libart.so (MterpInvokeDirect+196)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #34 pc 0040ab14  /system/lib/libart.so (ExecuteMterpImpl+14484)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #35 pc 0003d4b8  /dev/ashmem/dalvik-classes2.dex extracted in memory from /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/base.apk!classes2.dex (deleted) (com.synapticsense.retailapp.VisionClasses.CameraClass.access$400)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #36 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #37 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #38 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #39 pc 003eceeb  /system/lib/libart.so (MterpInvokeStatic+130)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #40 pc 0040ab94  /system/lib/libart.so (ExecuteMterpImpl+14612)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #41 pc 0003b600  /dev/ashmem/dalvik-classes2.dex extracted in memory from /data/app/com.synapticsense.retailapp-YTeLr0R846yqViejeyyrDQ==/base.apk!classes2.dex (deleted) (com.synapticsense.retailapp.VisionClasses.CameraClass$2.onImageAvailable+40)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #42 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #43 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #44 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #45 pc 003ecadd  /system/lib/libart.so (MterpInvokeInterface+1020)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #46 pc 0040ac14  /system/lib/libart.so (ExecuteMterpImpl+14740)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #47 pc 0060690a  /system/framework/boot-framework.vdex (android.media.ImageReader$ListenerHandler.handleMessage+72)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #48 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #49 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #50 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #51 pc 003ebf0f  /system/lib/libart.so (MterpInvokeVirtual+442)
    2019-04-21 21:44:37.189 6453-6453/? A/DEBUG:     #52 pc 0040aa14  /system/lib/libart.so (ExecuteMterpImpl+14228)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #53 pc 00ae2e6a  /system/framework/boot-framework.vdex (android.os.Handler.dispatchMessage+42)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #54 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #55 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #56 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #57 pc 003ebf0f  /system/lib/libart.so (MterpInvokeVirtual+442)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #58 pc 0040aa14  /system/lib/libart.so (ExecuteMterpImpl+14228)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #59 pc 00ae9f58  /system/framework/boot-framework.vdex (android.os.Looper.loop+404)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #60 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #61 pc 001cc757  /system/lib/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+146)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #62 pc 001e34fb  /system/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+754)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #63 pc 003eceeb  /system/lib/libart.so (MterpInvokeStatic+130)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #64 pc 0040ab94  /system/lib/libart.so (ExecuteMterpImpl+14612)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #65 pc 00ae29ac  /system/framework/boot-framework.vdex (android.os.HandlerThread.run+56)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #66 pc 001c7e89  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.2193211614+352)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #67 pc 001cc6a3  /system/lib/libart.so (art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*)+82)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #68 pc 003df753  /system/lib/libart.so (artQuickToInterpreterBridge+890)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #69 pc 0041c2ff  /system/lib/libart.so (art_quick_to_interpreter_bridge+30)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #70 pc 00417d75  /system/lib/libart.so (art_quick_invoke_stub_internal+68)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #71 pc 003f12e7  /system/lib/libart.so (art_quick_invoke_stub+226)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #72 pc 000a1031  /system/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+136)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #73 pc 00350a6d  /system/lib/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char const*)+52)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #74 pc 003517f9  /system/lib/libart.so (art::InvokeVirtualOrInterfaceWithJValues(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, jvalue*)+316)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #75 pc 00373369  /system/lib/libart.so (art::Thread::CreateCallback(void*)+900)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #76 pc 00063c35  /system/lib/libc.so (__pthread_start(void*)+22)
    2019-04-21 21:44:37.190 6453-6453/? A/DEBUG:     #77 pc 0001e085  /system/lib/libc.so (__start_thread+22)
    
  9. alalek commented on May 4, 2019

    @alalek
    Member

    Probably buffer is not "direct" in this Android system. Or it is gone for some reason (lifetime issue).

    This Mat over ByteBuffers are just "views" (no data is stored, ByteBuffer is assumed to be "direct"): code.

    Also stacktrace looks strange. Did you try to use CvCameraViewFrame outside of callback?

  10. kafan1986 commented on May 4, 2019

    @kafan1986
    Author

    Probably buffer is not "direct" in this Android system. Or it is gone for some reason (lifetime issue).

    This Mat over ByteBuffers are just "views" (no data is stored, ByteBuffer is assumed to be "direct"): code.

    Also stacktrace looks strange. Did you try to use CvCameraViewFrame outside of callback?

    OK. Would investigate. Lifetime did not seem to be an issue for Y channel (Gray) buffer data though, But the preview looks OK too (colors), if fed directly to textureView. Because I am using camera in background I had to use ImageReader's surface.

    I am not using CvCameraViewFrame, I am using own code and quite similar to OpenCV for getting camera preview image using Android Camera API 2. I copied the code from JavaCamera2Frame and made minor changes for my app for dealing with creating Mat from Image data.

  11. Victorlouisdg commented on May 5, 2019

    @Victorlouisdg
    Contributor

    @kafan1986 can you try using this function for the conversion?

  12. kafan1986 commented on May 5, 2019

    @kafan1986
    Author

    @kafan1986 can you try using this function for the conversion?

    Some good news and bad news. Good news is it does not crash anymore. Bad news there is some alignment issue with the UV data.

    BTW, I changed your function to produce RGB Mat for this testing ( Imgproc.cvtColor(yuv_mat, mBgr, Imgproc.COLOR_YUV2RGB_NV21, 3); ) as I am using Bitmap.createBitmap later on with the map to display the image on ImageView.

    Regarding the alignment issue I am attaching the image screenshot.

    image

  13. alalek commented on May 5, 2019

    @alalek
    Member

    Please try to "capture" well-known image, like this (in case of issues with color) and this (in case of issues with geometry).

    Android canvas expects RGBA (not RGB) - problem may be there too.
    It is better to save image into PNG file via imwrite() (but it expects BGR on input, so R/G color will be swapped, but stride issue should be reproduced properly).

    Also it would be nice to dump these buffers into RAW files from well known image:

    • planes[i].getBuffer() (i=0..2)
  14. kafan1986 commented on May 5, 2019

    @kafan1986
    Author

    Please try to "capture" well-known image, like this (in case of issues with color) and this (in case of issues with geometry).

    Android canvas expects RGBA (not RGB) - problem may be there too.
    It is better to save image into PNG file via imwrite() (but it expects BGR on input, so R/G color will be swapped, but stride issue should be reproduced properly).

    Also it would be nice to dump these buffers into RAW files from well known image:

    • planes[i].getBuffer() (i=0..2)

    I have changed the Mat to RGBA before the Bitmap.createBitmap step.

    I have attached the screenshot below as well as the buffer data for all 3 planes. From the image it looks like each of the 2 channels (U and V), gets compressed into one half of the image, one covers the top half and other the bottom half, giving impression of 3x image.

    ImageTest.zip

    image

  15. alalek commented on May 5, 2019

    @alalek
    Member

    Based on these RAW files it is possible to restore captured frame (from stereo camera). So geometry is fine. Color format is good too.
    But last pixel from UV plane has been lost completely (corrupted 2x2 block in bottom-right corner - it is black). May be this is the problem.

    Question is that is going into Mat during ByteBuffer -> Mat conversion.

    Results: fromRAW.zip

    C++ code is below (from host x86 machine):

    Details
    std::string basepath("... path to files ...");
    Mat y(960, 2560, CV_8U, Scalar::all(0));
    {
        std::ifstream f(basepath + "plane_0.raw");
        CV_Assert(f.is_open());
        f.read((char*)y.data, y.total());
        std::cout << "Y bytes: " << f.tellg() << std::endl;
    }
    
    Mat uv(960/2, 2560/2, CV_8UC2, Scalar::all(0));
    {
        std::ifstream f(basepath + "plane_2.raw");
        CV_Assert(f.is_open());
        f.read((char*)uv.data, uv.total() * 2);
        std::cout << "UV bytes: " << f.tellg() << ", expected: " << uv.total() * 2 << std::endl;
    }
    
    { // verifying planes geometry
        std::vector<Mat> uv_channels;
        split(uv, uv_channels);
        imshow("Y", y); imwrite("plane_y.jpg", y);
        imshow("uv0", uv_channels[0]); imwrite("plane_uv0.jpg", uv_channels[0]);
        imshow("uv1", uv_channels[1]); imwrite("plane_uv1.jpg", uv_channels[1]);
    }
    
    Mat bgra;
    Mat bgr;
    cvtColorTwoPlane(y, uv, bgra, COLOR_YUV2BGRA_NV21);
    cvtColorTwoPlane(y, uv, bgr, COLOR_YUV2BGR_NV21);
    std::cout << typeToString(bgra.type()) << std::endl;
    std::cout << typeToString(bgr.type()) << std::endl;
    
    imshow("bgra", bgra); imwrite("bgra.png", bgra);
    imshow("bgr", bgr); imwrite("bgr.jpg", bgr);
    
    waitKey();
    

    Next experiments (code may be not accurate - not tested, translated from C++):

    import org.opencv.core.Rect;
    ...
    ByteBuffer y_plane = planes[0].getBuffer();
    ByteBuffer uv_plane = planes[1].getBuffer();
    
    // create "cut" views
    
    Mat uv_mat_cut = new Mat(uv_mat, new Rect(0, 0, uv_mat.cols(), uv_mat.rows() - 1)); 
    Mat uv_mat_clone = uv_mat_cut.clone();  // just check, hopefully without "last" pixel (row) it is will not be crashed
    
    // cut 2 last rows from Y plane
    Mat y_mat_cut = new Mat(y_mat, new Rect(0, 0, y_mat.cols(), y_mat.rows() - 2));
    
    Imgproc.cvtColorTwoPlane(y_mat_cut, uv_mat_cut, mBgr, Imgproc.COLOR_YUV2BGR_NV21);
    
    return mBgr; // should be 2560x958
    
    1. In original Java code (data from plane[2] start earlier):
    -ByteBuffer uv_plane = planes[1].getBuffer();
    +ByteBuffer uv_plane = planes[2].getBuffer();
    
    +Mat uv_mat_clone = uv_mat.clone();   // <=== check this again
    
    ... try _NV21 => _NV12 if colors are swapped
  16. kafan1986 commented on May 5, 2019

    @kafan1986
    Author

    I will test your suggestion, btw it stopped crashing previously with @Victorlouisdg 's last provided function.

  17. alalek commented on May 5, 2019

    @alalek
    Member

    last provided function

    This function expects planes[1].getPixelStride() == 1 - it is not your case.

  18. kafan1986 commented on May 6, 2019

    @kafan1986
    Author
    1. In original Java code (data from plane[2] start earlier):
    -ByteBuffer uv_plane = planes[1].getBuffer();
    +ByteBuffer uv_plane = planes[2].getBuffer();
    
    +Mat uv_mat_clone = uv_mat.clone();   // <=== check this again
    
    ... try _NV21 => _NV12 if colors are swapped

    The experiment 1 worked (no crashing) but colors were swapped. Worked fine after using it with NV12. Closing the issue. Thanks for all the help.

  19. txdat commented on Jan 18, 2021

    @txdat

    @kafan1986 I have the same issue like you, I can use cvtColorTwoPlane with 640x480 images but not for others (e.g 1024x768). How did you solve it? Thank you!

  20. kafan1986 commented on Jan 18, 2021

    @kafan1986
    Author

    @txdat You have to look at the frame data. I believe in my case, culprit was the camera hardware, as at certain resolution, it was giving incomplete data, so I had to ignore the last two pixel rows. The below code worked fine for my case.

    int rowCut = 2; // Define rowCut size according to your actual image data
    boolean flipImage = true;
    
    private void createBgr(){
            if (mImage != null) {
                Image.Plane[] planes = mImage.getPlanes();
    
                int w = mImage.getWidth();
                int h = mImage.getHeight();
                int chromaPixelStride = planes[1].getPixelStride();
    
                if (chromaPixelStride == 2) { // Chroma channels are interleaved
                    ByteBuffer y_plane = planes[0].getBuffer();
                    ByteBuffer uv_plane = planes[1].getBuffer();
                    Mat y_mat = new Mat(h, w, CvType.CV_8UC1, y_plane);
                    Mat uv_mat = new Mat(h / 2, w / 2, CvType.CV_8UC2, uv_plane);
                    // Changes for stereo camera and N2 //https://github.com/opencv/opencv/issues/14477
                    Mat y_mat_cut = new Mat(y_mat, new Rect(0, 0, y_mat.cols(), y_mat.rows() - rowCut)); // cut 2 last rows from Y plane
                    Mat uv_mat_cut = new Mat(uv_mat, new Rect(0, 0, uv_mat.cols(), uv_mat.rows() - rowCut / 2));
                    Imgproc.cvtColorTwoPlane(y_mat_cut, uv_mat_cut, mBgr, Imgproc.COLOR_YUV2BGR_NV12); // With NV21 colours are inverted
                    if (flipImage) {
                        Core.flip(mBgr, flipmBgrMat, 1);
                    }
                } else { // Chroma channels are not interleaved
                    byte[] yuv_bytes = new byte[w * (h + h / 2)];
                    ByteBuffer y_plane = planes[0].getBuffer();
                    ByteBuffer u_plane = planes[1].getBuffer();
                    ByteBuffer v_plane = planes[2].getBuffer();
    
                    y_plane.get(yuv_bytes, 0, w * h);
    
                    int chromaRowStride = planes[1].getRowStride();
                    int chromaRowPadding = chromaRowStride - w / 2;
    
                    int offset = w * h;
                    if (chromaRowPadding == 0) {
                        // When the row stride of the chroma channels equals their width, we can copy
                        // the entire channels in one go
                        u_plane.get(yuv_bytes, offset, w * h / 4);
                        offset += w * h / 4;
                        v_plane.get(yuv_bytes, offset, w * h / 4);
                    } else {
                        // When not equal, we need to copy the channels row by row
                        for (int i = 0; i < h / 2; i++) {
                            u_plane.get(yuv_bytes, offset, w / 2);
                            offset += w / 2;
                            if (i < h / 2 - 1) {
                                u_plane.position(u_plane.position() + chromaRowPadding);
                            }
                        }
                        for (int i = 0; i < h / 2; i++) {
                            v_plane.get(yuv_bytes, offset, w / 2);
                            offset += w / 2;
                            if (i < h / 2 - 1) {
                                v_plane.position(v_plane.position() + chromaRowPadding);
                            }
                        }
                    }
    
                    Mat yuv_mat = new Mat(h + h / 2, w, CvType.CV_8UC1);
                    yuv_mat.put(0, 0, yuv_bytes);
                    Imgproc.cvtColor(yuv_mat, mBgr, Imgproc.COLOR_YUV2BGR_I420, 3);
    
                    if (flipImage) {
                        Core.flip(mBgr, flipmBgrMat, 1);
                    }
                }
            }
        }
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcategory: videoio(camera)needs investigationplatform: androidpriority: low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions