Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cv / cvcanny.cpp
diff --git a/src/cv/cvcanny.cpp b/src/cv/cvcanny.cpp
new file mode 100644 (file)
index 0000000..da45034
--- /dev/null
@@ -0,0 +1,365 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                        Intel License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000, Intel Corporation, all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of Intel Corporation may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "_cv.h"\r
+\r
+CV_IMPL void\r
+cvCanny( const void* srcarr, void* dstarr,\r
+         double low_thresh, double high_thresh, int aperture_size )\r
+{\r
+    CvMat *dx = 0, *dy = 0;\r
+    void *buffer = 0;\r
+    uchar **stack_top, **stack_bottom = 0;\r
+\r
+    CV_FUNCNAME( "cvCanny" );\r
+\r
+    __BEGIN__;\r
+\r
+    CvMat srcstub, *src = (CvMat*)srcarr;\r
+    CvMat dststub, *dst = (CvMat*)dstarr;\r
+    CvSize size;\r
+    int flags = aperture_size;\r
+    int low, high;\r
+    int* mag_buf[3];\r
+    uchar* map;\r
+    int mapstep, maxsize;\r
+    int i, j;\r
+    CvMat mag_row;\r
+\r
+    CV_CALL( src = cvGetMat( src, &srcstub ));\r
+    CV_CALL( dst = cvGetMat( dst, &dststub ));\r
+\r
+    if( CV_MAT_TYPE( src->type ) != CV_8UC1 ||\r
+        CV_MAT_TYPE( dst->type ) != CV_8UC1 )\r
+        CV_ERROR( CV_StsUnsupportedFormat, "" );\r
+\r
+    if( !CV_ARE_SIZES_EQ( src, dst ))\r
+        CV_ERROR( CV_StsUnmatchedSizes, "" );\r
+\r
+    if( low_thresh > high_thresh )\r
+    {\r
+        double t;\r
+        CV_SWAP( low_thresh, high_thresh, t );\r
+    }\r
+\r
+    aperture_size &= INT_MAX;\r
+    if( (aperture_size & 1) == 0 || aperture_size < 3 || aperture_size > 7 )\r
+        CV_ERROR( CV_StsBadFlag, "" );\r
+\r
+    size = cvGetMatSize( src );\r
+\r
+    dx = cvCreateMat( size.height, size.width, CV_16SC1 );\r
+    dy = cvCreateMat( size.height, size.width, CV_16SC1 );\r
+    cvSobel( src, dx, 1, 0, aperture_size );\r
+    cvSobel( src, dy, 0, 1, aperture_size );\r
+\r
+    /*if( icvCannyGetSize_p && icvCanny_16s8u_C1R_p && !(flags & CV_CANNY_L2_GRADIENT) )\r
+    {\r
+        int buf_size=  0;\r
+        IPPI_CALL( icvCannyGetSize_p( size, &buf_size ));\r
+        CV_CALL( buffer = cvAlloc( buf_size ));\r
+        IPPI_CALL( icvCanny_16s8u_C1R_p( (short*)dx->data.ptr, dx->step,\r
+                                     (short*)dy->data.ptr, dy->step,\r
+                                     dst->data.ptr, dst->step,\r
+                                     size, (float)low_thresh,\r
+                                     (float)high_thresh, buffer ));\r
+        EXIT;\r
+    }*/\r
+\r
+    if( flags & CV_CANNY_L2_GRADIENT )\r
+    {\r
+        Cv32suf ul, uh;\r
+        ul.f = (float)low_thresh;\r
+        uh.f = (float)high_thresh;\r
+\r
+        low = ul.i;\r
+        high = uh.i;\r
+    }\r
+    else\r
+    {\r
+        low = cvFloor( low_thresh );\r
+        high = cvFloor( high_thresh );\r
+    }\r
+\r
+    CV_CALL( buffer = cvAlloc( (size.width+2)*(size.height+2) +\r
+                                (size.width+2)*3*sizeof(int)) );\r
+\r
+    mag_buf[0] = (int*)buffer;\r
+    mag_buf[1] = mag_buf[0] + size.width + 2;\r
+    mag_buf[2] = mag_buf[1] + size.width + 2;\r
+    map = (uchar*)(mag_buf[2] + size.width + 2);\r
+    mapstep = size.width + 2;\r
+\r
+    maxsize = MAX( 1 << 10, size.width*size.height/10 );\r
+    CV_CALL( stack_top = stack_bottom = (uchar**)cvAlloc( maxsize*sizeof(stack_top[0]) ));\r
+\r
+    memset( mag_buf[0], 0, (size.width+2)*sizeof(int) );\r
+    memset( map, 1, mapstep );\r
+    memset( map + mapstep*(size.height + 1), 1, mapstep );\r
+\r
+    /* sector numbers \r
+       (Top-Left Origin)\r
+\r
+        1   2   3\r
+         *  *  * \r
+          * * *  \r
+        0*******0\r
+          * * *  \r
+         *  *  * \r
+        3   2   1\r
+    */\r
+\r
+    #define CANNY_PUSH(d)    *(d) = (uchar)2, *stack_top++ = (d)\r
+    #define CANNY_POP(d)     (d) = *--stack_top\r
+\r
+    mag_row = cvMat( 1, size.width, CV_32F );\r
+\r
+    // calculate magnitude and angle of gradient, perform non-maxima supression.\r
+    // fill the map with one of the following values:\r
+    //   0 - the pixel might belong to an edge\r
+    //   1 - the pixel can not belong to an edge\r
+    //   2 - the pixel does belong to an edge\r
+    for( i = 0; i <= size.height; i++ )\r
+    {\r
+        int* _mag = mag_buf[(i > 0) + 1] + 1;\r
+        float* _magf = (float*)_mag;\r
+        const short* _dx = (short*)(dx->data.ptr + dx->step*i);\r
+        const short* _dy = (short*)(dy->data.ptr + dy->step*i);\r
+        uchar* _map;\r
+        int x, y;\r
+        int magstep1, magstep2;\r
+        int prev_flag = 0;\r
+\r
+        if( i < size.height )\r
+        {\r
+            _mag[-1] = _mag[size.width] = 0;\r
+\r
+            if( !(flags & CV_CANNY_L2_GRADIENT) )\r
+                for( j = 0; j < size.width; j++ )\r
+                    _mag[j] = abs(_dx[j]) + abs(_dy[j]);\r
+            /*else if( icvFilterSobelVert_8u16s_C1R_p != 0 ) // check for IPP\r
+            {\r
+                // use vectorized sqrt\r
+                mag_row.data.fl = _magf;\r
+                for( j = 0; j < size.width; j++ )\r
+                {\r
+                    x = _dx[j]; y = _dy[j];\r
+                    _magf[j] = (float)((double)x*x + (double)y*y);\r
+                }\r
+                cvPow( &mag_row, &mag_row, 0.5 );\r
+            }*/\r
+            else\r
+            {\r
+                for( j = 0; j < size.width; j++ )\r
+                {\r
+                    x = _dx[j]; y = _dy[j];\r
+                    _magf[j] = (float)std::sqrt((double)x*x + (double)y*y);\r
+                }\r
+            }\r
+        }\r
+        else\r
+            memset( _mag-1, 0, (size.width + 2)*sizeof(int) );\r
+\r
+        // at the very beginning we do not have a complete ring\r
+        // buffer of 3 magnitude rows for non-maxima suppression\r
+        if( i == 0 )\r
+            continue;\r
+\r
+        _map = map + mapstep*i + 1;\r
+        _map[-1] = _map[size.width] = 1;\r
+        \r
+        _mag = mag_buf[1] + 1; // take the central row\r
+        _dx = (short*)(dx->data.ptr + dx->step*(i-1));\r
+        _dy = (short*)(dy->data.ptr + dy->step*(i-1));\r
+        \r
+        magstep1 = (int)(mag_buf[2] - mag_buf[1]);\r
+        magstep2 = (int)(mag_buf[0] - mag_buf[1]);\r
+\r
+        if( (stack_top - stack_bottom) + size.width > maxsize )\r
+        {\r
+            uchar** new_stack_bottom;\r
+            maxsize = MAX( maxsize * 3/2, maxsize + size.width );\r
+            CV_CALL( new_stack_bottom = (uchar**)cvAlloc( maxsize * sizeof(stack_top[0])) );\r
+            memcpy( new_stack_bottom, stack_bottom, (stack_top - stack_bottom)*sizeof(stack_top[0]) );\r
+            stack_top = new_stack_bottom + (stack_top - stack_bottom);\r
+            cvFree( &stack_bottom );\r
+            stack_bottom = new_stack_bottom;\r
+        }\r
+\r
+        for( j = 0; j < size.width; j++ )\r
+        {\r
+            #define CANNY_SHIFT 15\r
+            #define TG22  (int)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5)\r
+\r
+            x = _dx[j];\r
+            y = _dy[j];\r
+            int s = x ^ y;\r
+            int m = _mag[j];\r
+\r
+            x = abs(x);\r
+            y = abs(y);\r
+            if( m > low )\r
+            {\r
+                int tg22x = x * TG22;\r
+                int tg67x = tg22x + ((x + x) << CANNY_SHIFT);\r
+\r
+                y <<= CANNY_SHIFT;\r
+\r
+                if( y < tg22x )\r
+                {\r
+                    if( m > _mag[j-1] && m >= _mag[j+1] )\r
+                    {\r
+                        if( m > high && !prev_flag && _map[j-mapstep] != 2 )\r
+                        {\r
+                            CANNY_PUSH( _map + j );\r
+                            prev_flag = 1;\r
+                        }\r
+                        else\r
+                            _map[j] = (uchar)0;\r
+                        continue;\r
+                    }\r
+                }\r
+                else if( y > tg67x )\r
+                {\r
+                    if( m > _mag[j+magstep2] && m >= _mag[j+magstep1] )\r
+                    {\r
+                        if( m > high && !prev_flag && _map[j-mapstep] != 2 )\r
+                        {\r
+                            CANNY_PUSH( _map + j );\r
+                            prev_flag = 1;\r
+                        }\r
+                        else\r
+                            _map[j] = (uchar)0;\r
+                        continue;\r
+                    }\r
+                }\r
+                else\r
+                {\r
+                    s = s < 0 ? -1 : 1;\r
+                    if( m > _mag[j+magstep2-s] && m > _mag[j+magstep1+s] )\r
+                    {\r
+                        if( m > high && !prev_flag && _map[j-mapstep] != 2 )\r
+                        {\r
+                            CANNY_PUSH( _map + j );\r
+                            prev_flag = 1;\r
+                        }\r
+                        else\r
+                            _map[j] = (uchar)0;\r
+                        continue;\r
+                    }\r
+                }\r
+            }\r
+            prev_flag = 0;\r
+            _map[j] = (uchar)1;\r
+        }\r
+\r
+        // scroll the ring buffer\r
+        _mag = mag_buf[0];\r
+        mag_buf[0] = mag_buf[1];\r
+        mag_buf[1] = mag_buf[2];\r
+        mag_buf[2] = _mag;\r
+    }\r
+\r
+    // now track the edges (hysteresis thresholding)\r
+    while( stack_top > stack_bottom )\r
+    {\r
+        uchar* m;\r
+        if( (stack_top - stack_bottom) + 8 > maxsize )\r
+        {\r
+            uchar** new_stack_bottom;\r
+            maxsize = MAX( maxsize * 3/2, maxsize + 8 );\r
+            CV_CALL( new_stack_bottom = (uchar**)cvAlloc( maxsize * sizeof(stack_top[0])) );\r
+            memcpy( new_stack_bottom, stack_bottom, (stack_top - stack_bottom)*sizeof(stack_top[0]) );\r
+            stack_top = new_stack_bottom + (stack_top - stack_bottom);\r
+            cvFree( &stack_bottom );\r
+            stack_bottom = new_stack_bottom;\r
+        }\r
+\r
+        CANNY_POP(m);\r
+    \r
+        if( !m[-1] )\r
+            CANNY_PUSH( m - 1 );\r
+        if( !m[1] )\r
+            CANNY_PUSH( m + 1 );\r
+        if( !m[-mapstep-1] )\r
+            CANNY_PUSH( m - mapstep - 1 );\r
+        if( !m[-mapstep] )\r
+            CANNY_PUSH( m - mapstep );\r
+        if( !m[-mapstep+1] )\r
+            CANNY_PUSH( m - mapstep + 1 );\r
+        if( !m[mapstep-1] )\r
+            CANNY_PUSH( m + mapstep - 1 );\r
+        if( !m[mapstep] )\r
+            CANNY_PUSH( m + mapstep );\r
+        if( !m[mapstep+1] )\r
+            CANNY_PUSH( m + mapstep + 1 );\r
+    }\r
+\r
+    // the final pass, form the final image\r
+    for( i = 0; i < size.height; i++ )\r
+    {\r
+        const uchar* _map = map + mapstep*(i+1) + 1;\r
+        uchar* _dst = dst->data.ptr + dst->step*i;\r
+        \r
+        for( j = 0; j < size.width; j++ )\r
+            _dst[j] = (uchar)-(_map[j] >> 1);\r
+    }\r
+\r
+    __END__;\r
+\r
+    cvReleaseMat( &dx );\r
+    cvReleaseMat( &dy );\r
+    cvFree( &buffer );\r
+    cvFree( &stack_bottom );\r
+}\r
+\r
+void cv::Canny( const Mat& image, Mat& edges,\r
+                double threshold1, double threshold2,\r
+                int apertureSize, bool L2gradient )\r
+{\r
+    Mat src = image;\r
+    edges.create(src.size(), CV_8U);\r
+    CvMat _src = src, _dst = edges;\r
+    cvCanny( &_src, &_dst, threshold1, threshold2,\r
+        apertureSize + (L2gradient ? CV_CANNY_L2_GRADIENT : 0));\r
+}\r
+\r
+/* End of file. */\r