Ask Your Question

Revision history [back]

warpAffine produces erroneous output on large images

I am using getRotationMatrix2D to rotate an image, passing the rotation matrix into warpAffine. On small images (e.g. 4465x1745 or smaller) the output is what I expect. Larger images (e.g. 14882x5815) give weird results. The same image scaled down gives expected results. Interestingly, reading the input image as grayscale allows warpAffine to process the image, even with large dimensions.

Code:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

#define READ_COLOR  1
#define READ_BW     0

int main(int argc, char *argv[])
{
    Mat mat, out;
    int len;

    if (argc != 3) {
        cerr << "Usage: " << *argv << " input-img output-img" << endl;
        return -1;
    }

    mat = imread(argv[1], READ_COLOR);
    if (!mat.data)
        return -1;

    Point2i pt(0, 0);
    Mat r = getRotationMatrix2D(pt, 10, 1);
    if (!r.data)
        return -1;
    warpAffine(mat, out, r, mat.size());

    if (!imwrite(argv[2], out)) {
        cerr << "!! error writing " << argv[2] << endl;
        return -1;
    }

    return 0;
}

Image as input (download Original Size):

http://www.flickr.com/photos/fourcornersschool/10124677703/sizes/l/

When loaded with color in a large size I get the following result (post-processed with Graphics Magick to 30% scale due to upload size limitations):

image description

Zoomed into somewhere near the center, for clarity, the image looks like this:

image description

Telling imread to load as grayscale, the following image is produced (scaled as before with GM to 25%):

image description

I built OpenCV with the following:

cmake28 -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D WITH_CUDA=ON -D WITH_OPENCL=OFF -D WITH_TBB=ON ../opencv.git/

Build information:

General configuration for OpenCV 2.4.8.2 =====================================
  Version control:               2.4.8.2

  Platform:
    Host:                        Linux 2.6.32-431.3.1.el6.x86_64 x86_64
    CMake:                       2.8.11.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/gmake
    Configuration:               RELEASE

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /usr/bin/c++  (ver 4.4.7)
    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 -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 -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 -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 -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:         YES

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

  GUI:
    QT:                          NO
    GTK+ 2.x:                    YES (ver 2.20.1)
    GThread :                    YES (ver 2.26.1)
    GtkGlExt:                    NO
    OpenGL support:              NO

  Media I/O: 
    ZLib:                        /usr/lib64/libz.so (ver 1.2.3)
    JPEG:                        /usr/lib64/libjpeg.so (ver )
    PNG:                         /usr/lib64/libpng.so (ver 1.2.49)
    TIFF:                        /usr/lib64/libtiff.so (ver 42 - 3.9.4)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     /usr/lib64/libImath.so /usr/lib64/libIlmImf.so /usr/lib64/libIex.so /usr/lib64/libHalf.so /usr/lib64/libIlmThread.so (ver 1.6.1)

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    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:                    YES/YES
    XIMEA:                       NO
    Xine:                        NO

  Other third-party libraries:
    Use IPP:                     NO
    Use Eigen:                   YES (ver 3.0.6)
    Use TBB:                     YES (ver 2.2 interface 4001)
    Use OpenMP:                  NO
    Use GCD                      NO
    Use Concurrency              NO
    Use C=:                      NO
    Use Cuda:                    YES (ver 5.5)
    Use OpenCL:                  NO

  NVIDIA CUDA
    Use CUFFT:                   YES
    Use CUBLAS:                  NO
    USE NVCUVID:                 NO
    NVIDIA GPU arch:             11 12 13 20 21 30 35
    NVIDIA PTX archs:            30
    Use fast math:               NO

  Python:
    Interpreter:                 /usr/local/bin/python2 (ver 2.7.3)

  Java:
    ant:                         NO
    JNI:                         /usr/lib/jvm/java/include /usr/lib/jvm/java/include/linux /usr/lib/jvm/java/include
    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:              /usr/local/src/opencv/opencv-build-2.4.8.2
-----------------------------------------------------------------

Any thoughts? Thanks!