Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected behaviour of Mat::copyTo? #10064

Closed
liquidmetal opened this issue Nov 10, 2017 · 7 comments · Fixed by #10294
Closed

Expected behaviour of Mat::copyTo? #10064

liquidmetal opened this issue Nov 10, 2017 · 7 comments · Fixed by #10294

Comments

@liquidmetal
Copy link
Contributor

liquidmetal commented Nov 10, 2017

System information (version)

  • OpenCV => 3.3.0
  • Operating System / Platform => Ubuntu 17.10, intel i7
  • Compiler => g++6.3.0 (c++14 mode)

Detailed description

Copying a small Mat to a larger Mat produces artifacts because of image reshaping.

Expected output (120 rows, 160 cols)
expected

Actual image saved using imwrite (119 rows, 159 cols)
bug

Steps to reproduce
    Mat output_with_content(120, 160, CV_8UC3, CV_RGB(255, 0, 0));
    for(int y=0;y<120;y++) {
        auto col = ((double)rand()/RAND_MAX)*255.0;
        Vec3b color(col, col, col);
        for(int x=0;x<160;x++) {
            output_with_content.at<Vec3b>(y, x) = color;
        }
    }

    imwrite("../expected.png", output_with_content);

    Mat to_paste(119, 159, CV_8UC3, CV_RGB(0, 0, 0));
    for(int y=0;y<119;y++) {
        auto col = ((double)rand()/RAND_MAX)*255.0;
        Vec3b color(col, 0, 0);
        for(int x=0;x<159;x++) {
            to_paste.at<Vec3b>(y, x) = color;
        }
    }
    Mat mask(119, 159, CV_8UC1, Scalar(0));

    // The bug
    to_paste.copyTo(output_with_content, mask);
    imwrite("../bug.png", output_with_content);
Potential issue
  • The issues seems like some kind of a memcpy that isn't aligned correct. This is also likely to be some specialized CPU instruction.
  • I expected the original (output_with_content, larger) image to stay intact and the smaller image (to_paste) to be pasted in the top-left corner of the image. Is this the expected behavior?
Potential solutions

This can be solved in one of the following ways:

  • Enforce the two Mats to be the same dimension and error out if this isn't true
  • Paste the smaller mat on the top-left corner (and update the docs)

I'd be happy to contribute this fix (if this is something that needs fixing that is)

@liquidmetal liquidmetal changed the title Expected behaviour of Mat::copyTo Expected behaviour of Mat::copyTo? Nov 10, 2017
@alalek
Copy link
Member

alalek commented Nov 10, 2017

Could you try to check with reverted patch #9551 ?

@liquidmetal
Copy link
Contributor Author

It seems like #9551 isn't related to this bug. I found the bug in OpenCV as far back as 2.4.9 - however it would be great if someone else could double-check this.

I used find_package(OpenCV 2.4.9 REQUIRED) in my cmake file. I also ensured that the library symlinks on my machine pointed to libopencv_*.so.2.4.9. Is there anything else i should check to ensure I'm really using v2.4.9?

@alalek
Copy link
Member

alalek commented Nov 12, 2017

Check cv::getBuildInformation() string.

@liquidmetal
Copy link
Contributor Author

The build information looks great! I am indeed using v2.4.9.

OpenCV Build: 
General configuration for OpenCV 2.4.9 =====================================
  Version control:               2.4.9

  Platform:
    Host:                        Linux 4.10.0-38-generic x86_64
    CMake:                       3.7.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /usr/bin/c++  (ver 6.3.0)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -msse3 -ffunction-sections -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -msse3 -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -msse3 -ffunction-sections -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -msse3 -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):
    Linker flags (Debug):
    Precompiled headers:         NO

  OpenCV modules:
    To be built:                 core flann imgproc highgui features2d calib3d ml objdetect photo stitching ts
    Disabled:                    nonfree video videostab world
    Disabled by dependency:      contrib(deps: video legacy gpu ocl nonfree) gpu(deps: video legacy) legacy(deps: video) ocl(deps: video) python(deps: video legacy gpu ocl nonfree contrib) superres(deps: video legacy gpu ocl)
    Unavailable:                 androidcamera dynamicuda java viz

  GUI: 
    QT:                          NO
    GTK+ 2.x:                    YES (ver 2.24.31)
    GThread :                    YES (ver 2.52.0)
    GtkGlExt:                    NO
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O: 
    ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
    JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
    PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.28)
    TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.7)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  YES (ver 2.2.5)
    FFMPEG:                      NO
      codec:                     NO
      format:                    NO
      util:                      NO
      swscale:                   NO
      gentoo-style:              NO
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    NO/YES
    XIMEA:                       NO
    Xine:                        NO

  Other third-party libraries:
    Use IPP:                     NO
    Use Eigen:                   YES (ver 3.3.2)
    Use TBB:                     NO
    Use OpenMP:                  NO
    Use GCD                      NO
    Use Concurrency              NO
    Use C=:                      NO
    Use Cuda:                    NO
    Use OpenCL:                  YES

  OpenCL:
    Version:                     dynamic
    Include path:                /work/software/opencv/3rdparty/include/opencl/1.2
    Use AMD FFT:                 NO
    Use AMD BLAS:                NO

  Python:
    Interpreter:                 /usr/bin/python2 (ver 2.7.13)
    Libraries:                   NO
    numpy:                       /usr/local/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.0)
    packages path:               lib/python2.7/dist-packages

  Java:
    ant:                         NO
    JNI:                         NO
    Java tests:                  NO

  Documentation:
    Build Documentation:         NO
    Sphinx:                      NO
    PdfLaTeX compiler:           /usr/bin/pdflatex

  Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    C/C++ Examples:              NO

  Install path:                  /usr/local

  cvconfig.h is in:              /work/software/opencv/build-2.4.9

@sturkmen72
Copy link
Contributor

sturkmen72 commented Nov 12, 2017

@liquidmetal can i ask a favour unrelated to this issue.
your OpenCV have libpng.so (ver 1.6.28)
could you try to open the png image at pnggroup/libpng#183

@alalek
Copy link
Member

alalek commented Dec 12, 2017

Mat mask(119, 159, CV_8UC1, Scalar(0));

Expected output is black image 159 x 119 pixels.

Destination image is re-allocated, but occasionally with the same memory location, so this code doesn't work properly:

    if( dst.data != data0 ) // do not leave dst uninitialized
        dst = Scalar(0);

@liquidmetal
Copy link
Contributor Author

This is great - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants