Repository: opencv
Issue: Parallelization: convert to grey leads to 100% processor load
Labels: category: core,category: 3rdparty

##### System information (version)
```
General configuration for OpenCV 3.2.0-dev =====================================
  Version control:               3.0.0-4749-gdcd8589

  Extra modules:
    Location (extra):            C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/contrib/modules
    Version control (extra):     3.0.0-beta-1572-g560e782

  Platform:
    Timestamp:                   2017-05-11T14:41:49Z
    Host:                        Windows 6.3.9600 AMD64
    CMake:                       3.5.1
    CMake generator:             Visual Studio 12 2013 Win64
    CMake build tool:            C:/Program Files (x86)/MSBuild/12.0/bin/MSBuild.exe
    MSVC:                        1800

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3 SSSE3
      requested:                 SSSE3
    Dispatched code generation:  SSE4_1 FP16 AVX AVX2
      requested:                 SSE4_1 AVX FP16 AVX2
      SSE4_1 (0 files):          + SSE4_1
      FP16 (0 files):            + SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (1 files):             + SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (1 files):            + SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe  (ver 18.0.40629.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi       /wd4127 /wd4251 /wd4324 /wd4275 /wd4589 /MP12  /openmp /MD /O2 /Ob2 /D NDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi       /wd4127 /wd4251 /wd4324 /wd4275 /wd4589 /MP12  /openmp /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi         /MP12  /openmp /MD /O2 /Ob2 /D NDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi         /MP12  /openmp /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 vfw32 vtkRenderingOpenGL2 vtkCommonCore vtksys vtkCommonDataModel vtkCommonMath vtkCommonMisc vtkCommonSystem vtkCommonTransforms vtkCommonExecutionModel vtkIOImage vtkDICOMParser vtkmetaio vtkzlib vtkjpeg vtkpng vtktiff vtkImagingCore vtkRenderingCore vtkCommonColor vtkCommonComputationalGeometry vtkFiltersCore vtkFiltersGeneral vtkFiltersGeometry vtkFiltersSources vtkglew vtkInteractionStyle vtkFiltersExtraction vtkFiltersStatistics vtkImagingFourier vtkalglib vtkRenderingLOD vtkFiltersModeling vtkIOPLY vtkIOCore vtkFiltersTexture vtkRenderingFreeType vtkfreetype vtkIOExport vtkRenderingGL2PSOpenGL2 vtkgl2ps vtkIOGeometry vtkIOLegacy glu32 opengl32 cudart nppc nppi npps cufft -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 cudev core cudaarithm flann imgproc ml objdetect phase_unwrapping plot reg surface_matching video viz xphoto bgsegm cudabgsegm cudafilters cudaimgproc cudawarping dnn face fuzzy imgcodecs photo shape videoio xobjdetect cudacodec highgui bioinspired dpm features2d line_descriptor saliency text calib3d ccalib cudafeatures2d cudalegacy cudaobjdetect cudaoptflow cudastereo datasets rgbd stereo structured_light superres tracking videostab xfeatures2d ximgproc aruco optflow sfm stitching
    Disabled:                    world contrib_world
    Disabled by dependency:      -
    Unavailable:                 java python2 python3 ts cnn_3dobj cvv dnn_modern freetype hdf matlab

  Windows RT support:            NO

  GUI:
    QT:                          NO
    Win32 UI:                    YES
    OpenGL support:              YES (glu32 opengl32)
    VTK support:                 YES (ver 7.1.1)

  Media I/O:
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.24)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    Video for Windows:           YES
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (ver )
      avformat:                  YES (ver )
      avutil:                    YES (ver )
      swscale:                   YES (ver )
      avresample:                YES (ver )
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    Media Foundation:            NO
    XIMEA:                       NO
    Intel PerC:                  NO

  Parallel framework:            OpenMP

  Other third-party libraries:
    Use Intel IPP:               2017.0.2 [2017.0.2]
               at:               C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/build/3rdparty/ippicv/ippicv_win
    Use Intel IPP IW:            prebuilt binaries (2017.0.2)
    Use Intel IPP Async:         NO
    Use Lapack:                  NO
    Use Eigen:                   YES (ver 3.3.2)
    Use Cuda:                    YES (ver 8.0)
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  NVIDIA CUDA
    Use CUFFT:                   YES
    Use CUBLAS:                  NO
    USE NVCUVID:                 NO
    NVIDIA GPU arch:             30 35 37 50 52 60 61
    NVIDIA PTX archs:
    Use fast math:               NO

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 NO

  Python 3:
    Interpreter:                 C:/Program Files (x86)/Python3.4.3/python.exe (ver 3.4.3)

  Python (for build):            C:/Program Files (x86)/Python3.4.3/python.exe

  Java:
    ant:                         NO
    JNI:                         C:/Program Files/Java/jdk1.8.0_40/include C:/Program Files/Java/jdk1.8.0_40/include/win32 C:/Program Files/Java/jdk1.8.0_40/include
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Tests and samples:
    Tests:                       NO
    Performance tests:           NO
    C/C++ Examples:              NO

  Install path:                  C:/DEVELOPMENT/opencv.install

  cvconfig.h is in:              C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/build
-----------------------------------------------------------------
```

##### Detailed description
Ever since I upgraded from OpenCV 3.1. (I built commit 49b4af1e5c675ea8152c7413e7a7ca0def41c7e8 ) to OpenCV 3.2+X (I built commit dcd8589b678824beeba4f5741c975ec78f943c8a) my OpenCV pipeline leads to 100% processor load on my eight core system on occasions where it previously was around 25%. This makes my pipeline unusable. I managed to write a minimal example for this unusual high processor load which you see below:

##### Steps to reproduce

```
#include <iostream>
#include <opencv/highgui.h>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

int main()
{
    //cv::setNumThreads(0);
    std::cout << cv::getBuildInformation() << std::endl;
    while (true) {
        cv::Mat img(1000, 1000, CV_8UC3);
        cv::Mat grey;
        cv::cvtColor(img, grey, img.channels() == 4 ? cv::COLOR_BGRA2GRAY : cv::COLOR_BGR2GRAY);
        cv::imshow("Grey", grey);
        std::cerr << ".";
        if (cv::waitKey(100) == 'q') {
            break;
        }
    }
}
```
Leads to a 100% processor load on my 8 core system - even though I have a very long waiting period of 100 ms.
If I uncomment the first line ``//cv::setNumThreads(0);``, the load reported by the Windows Taskmanager for my process is basically zero while the console prints the dots in roughly the same frequency (poor man's profiling :-) ). I can also reduce ``cv::waitKey(1)`` so it runs as fast as possible while still having near-zero load.

### Comparison with old OpenCV version
("old" means 49b4af1e5c675ea8152c7413e7a7ca0def41c7e8 and "new" means dcd8589b678824beeba4f5741c975ec78f943c8a) If I run above example with my old OpenCV build, the load is basically zero no matter if i set the num threads or not. When looking at the build information for the old build (see below) I noticed that the

``Parallel framework: Concurrency``

in the old build changed to

``Parallel framework: OpenMP``

and also in the old build the OpenMP Cpp flags where missing while in the new build they are here. Stepping trough the calls the old version indeed executes [Concurrency::parallel_for](https://github.com/opencv/opencv/blob/649bb7ac04bf54ffce2cdecb42a64b3c019fbe94/modules/core/src/parallel.cpp#L363) in the old build but [omp parallel for](https://github.com/opencv/opencv/blob/649bb7ac04bf54ffce2cdecb42a64b3c019fbe94/modules/core/src/parallel.cpp#L353) in the new build

**Bug1:** Why is the load so tremendously high with OpenMP? How can I go back to the Concurrency framework?
**Bug2:** In both builds I explicitly enabled ``WITH_OPENMP`` in cmake. Was it not used in the old build and shouldn't it have been?

```
General configuration for OpenCV 3.1.0-dev =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2016-04-25T10:00:36Z
    Host:                        Windows 6.3.9600 AMD64
    CMake:                       3.5.1
    CMake generator:             Visual Studio 12 Win64
    CMake build tool:            C:/Program Files (x86)/MSBuild/12.0/bin/MSBuild.exe
    MSVC:                        1800

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe  (ver 18.0.40629.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /MD /O2 /Ob2 /D NDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi    /MP8  /MD /O2 /Ob2 /D NDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi    /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
    Precompiled headers:         YES
    Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 vfw32 cudart nppc nppi npps cufft -LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v7.0/lib/x64
    3rdparty dependencies:       zlib libjpeg libwebp libpng libtiff libjasper IlmImf libprotobuf

  OpenCV modules:
    To be built:                 cudev core cudaarithm flann imgproc ml reg surface_matching video cudabgsegm cudafilters cudaimgproc cudawarping dnn fuzzy imgcodecs photo shape videoio cudacodec highgui objdetect plot ts xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib cudafeatures2d cudalegacy cudaobjdetect cudaoptflow cudastereo datasets rgbd stereo structured_light superres tracking videostab xfeatures2d ximgproc aruco optflow stitching
    Disabled:                    world contrib_world
    Disabled by dependency:      -
    Unavailable:                 java python2 python3 viz cvv hdf matlab sfm

  Windows RT support:            NO

  GUI:
    QT:                          NO
    Win32 UI:                    YES
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver 0.3.1)
    PNG:                         build (ver 1.6.19)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO

  Video I/O:
    Video for Windows:           YES
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES (prebuilt binaries)
      codec:                     YES (ver 56.41.100)
      format:                    YES (ver 56.36.101)
      util:                      YES (ver 54.27.100)
      swscale:                   YES (ver 3.1.101)
      resample:                  NO
      gentoo-style:              YES
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    Media Foundation:            NO
    XIMEA:                       NO
    Intel PerC:                  NO

  Parallel framework:            Concurrency

  Other third-party libraries:
    Use IPP:                     9.0.1 [9.0.1]
         at:                     C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/3rdparty/ippicv/unpack/ippicv_win
    Use IPP Async:               NO
    Use Eigen:                   YES (ver 3.1.0)
    Use Cuda:                    YES (ver 7.0)
    Use OpenCL:                  YES
    Use custom HAL:              NO

  NVIDIA CUDA
    Use CUFFT:                   YES
    Use CUBLAS:                  NO
    USE NVCUVID:                 NO
    NVIDIA GPU arch:             20 21 30 35 50 52
    NVIDIA PTX archs:            30
    Use fast math:               NO

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 C:/Users/MyUser/AppData/Local/Programs/Python/Python35/python.exe (ver 3.5.1)

  Python 3:
    Interpreter:                 C:/Users/MyUser/AppData/Local/Programs/Python/Python35/python.exe (ver 3.5.1)

  Python (for build):            C:/Users/MyUser/AppData/Local/Programs/Python/Python35/python.exe

  Java:
    ant:                         NO
    JNI:                         C:/Program Files/Java/jdk1.8.0_40/include C:/Program Files/Java/jdk1.8.0_40/include/win32 C:/Program Files/Java/jdk1.8.0_40/include
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Documentation:
    Doxygen:                     NO
    PlantUML:                    NO

  Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    C/C++ Examples:              NO

  Install path:                  C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/build/install

  cvconfig.h is in:              C:/Program Files (x86)/Jenkins/jobs/OpenCV/workspace/build
-----------------------------------------------------------------

```