cvundistort.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 1,555 行 · 第 1/4 页

SVN-BASE
1,555
字号
                    int a3 = src[uv + 5];
                    int b1 = src[uvs];
                    int b2 = src[uvs + 1];
                    int b3 = src[uvs + 2];
                    int c1 = src[uvs + 3];
                    int c2 = src[uvs + 4];
                    int c3 = src[uvs + 5];
                    int d1, d2, d3;

                    a1 -= a01;
                    a2 -= a02;
                    a3 -= a03;
                    b1 -= a01;
                    b2 -= a02;
                    b3 -= a03;
                    c1 -= a01 + a1 + b1;
                    c2 -= a02 + a2 + b2;
                    c3 -= a03 + a3 + b3;
                    d1 = ((a1 * iu + b1 * iv + c1 * iuv) >> S) + a01;
                    d2 = ((a2 * iu + b2 * iv + c2 * iuv) >> S) + a02;
                    d3 = ((a3 * iu + b3 * iv + c3 * iuv) >> S) + a03;
                    dst[u3] = (uchar) d1;
                    dst[u3 + 1] = (uchar) d2;
                    dst[u3 + 2] = (uchar) d3;
                }
            }
        }
    }

    memcpy( (void*)src, buf, 3 );

    icvFree( (void **) &x1 );
    icvFree( (void **) &x2 );
    icvFree( (void **) &x3 );
    icvFree( (void **) &du );
    return CV_NO_ERR;
}
/*______________________________________________________________________________________*/


/*F//////////////////////////////////////////////////////////////////////////////////////
//    Name: cvUnDistortOnce
//    Purpose: The function corrects radial and tangential image distortion using known
//             matrix of the camera intrinsic parameters and distortion coefficients
//    Context:
//    Parameters:  srcImage    - source (distorted) image
//                 dstImage    - output (undistorted) image
//                 intrMatrix  - matrix of the camera intrinsic parameters
//                 distCoeffs  - vector of the distortion coefficients (k1, k2, p1 and p2)
//                 interpolate - interpolation toggle (optional parameter)
//
//    Notes:   1. If interpolate = 0, interpolation disabled;
//                else (default) bilinear interpolation is used.
//             2. If p1 = p2 = 0, function acts faster.
//F*/

typedef CvStatus (CV_STDCALL * CvUnDistortOnceFunc)( const void* src, int srcstep,
                                                     void* dst, int dststep, CvSize size,
                                                     const float* intrMatrix,
                                                     const float* distCoeffs,
                                                     int interToggle );


CV_IMPL void
cvUnDistortOnce( const void* srcImage, void* dstImage,
                 const float* intrMatrix, const float* distCoeffs,
                 int interpolate )
{
    static void* undist_tab[4];
    static int inittab = 0;
    
    CV_FUNCNAME( "cvUnDistortOnce" );

    __BEGIN__;

    int coi1 = 0, coi2 = 0;
    CvMat srcstub, *src = (CvMat*)srcImage;
    CvMat dststub, *dst = (CvMat*)dstImage;
    CvSize size;
    CvUnDistortOnceFunc  func = 0;

    if( !inittab )
    {
        undist_tab[0] = (void*)icvUnDistort1_8u_C1R;
        undist_tab[1] = (void*)icvUnDistortEx_8u_C1R;
        undist_tab[2] = (void*)icvUnDistort1_8u_C3R;
        undist_tab[3] = (void*)icvUnDistortEx_8u_C3R;
        inittab = 1;
    }

    CV_CALL( src = cvGetMat( src, &srcstub, &coi1 ));
    CV_CALL( dst = cvGetMat( dst, &dststub, &coi2 ));

    if( coi1 != 0 || coi2 != 0 )
        CV_ERROR( CV_BadCOI, "" );

    if( CV_MAT_TYPE(src->type) != CV_8UC1 &&
        CV_MAT_TYPE(src->type) != CV_8UC3 )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    if( !CV_ARE_TYPES_EQ( src, dst ))
        CV_ERROR( CV_StsUnmatchedFormats, "" );

    if( !CV_ARE_SIZES_EQ( src, dst ))
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    if( !intrMatrix || !distCoeffs )
        CV_ERROR( CV_StsNullPtr, "" );

    size = icvGetMatSize( src );

    func = (CvUnDistortOnceFunc)
        (undist_tab[(CV_MAT_CN(src->type)-1)+(distCoeffs[2] != 0 || distCoeffs[3] != 0)]);

    if( !func )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    IPPI_CALL( func( src->data.ptr, src->step, dst->data.ptr, dst->step, size,
                     intrMatrix, distCoeffs, interpolate ));
  
    __END__;
}


/*F//////////////////////////////////////////////////////////////////////////////////////
//    Name: cvUnDistortInit
//    Purpose: The function calculates arrays of distorted points indices and
//             interpolation coefficients for cvUnDistort function using known
//             matrix of the camera intrinsic parameters and distortion coefficients
//    Context:
//    Parameters:  srcImage    - source (distorted) image
//                 intrMatrix  - matrix of the camera intrinsic parameters
//                 distCoeffs  - vector of the distortion coefficients (k1, k2, p1 and p2)
//                 data        - distortion data array
//                 interpolate - interpolation toggle (optional parameter)
//
//    Notes:  1. If interpolate=0, interpolation disabled;
//               else (default) bilinear interpolation is used.
//            2. Array data must be allocated before. If interpolate = 0, its length
//               must be size.width*size.height elements; else 3*size.width*size.height.
//F*/
CV_IMPL void
cvUnDistortInit( const void* srcImage, void* undistMap,
                 const float* intrMatrix, const float* distCoeffs,
                 int  interpolate )
{
    CV_FUNCNAME( "cvUnDistortInit" );

    __BEGIN__;

    int coi1 = 0, coi2 = 0;
    CvMat srcstub, *src = (CvMat*)srcImage;
    CvMat mapstub, *map = (CvMat*)undistMap;
    CvSize size;

    CV_CALL( src = cvGetMat( src, &srcstub, &coi1 ));
    CV_CALL( map = cvGetMat( map, &mapstub, &coi2 ));

    if( coi1 != 0 || coi2 != 0 )
        CV_ERROR( CV_BadCOI, "" );

    if( CV_MAT_TYPE( map->type ) != CV_32SC1 && CV_MAT_TYPE( map->type ) != CV_32SC3 )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    if( !intrMatrix || !distCoeffs )
        CV_ERROR( CV_StsNullPtr, "" );

    if( src->height > map->height ||
        interpolate && src->width*3 > map->width*CV_MAT_CN( map->type ) ||
        !interpolate && src->width > map->width )
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    size = icvGetMatSize( src );

    IPPI_CALL( icvUnDistortInit( src->step, (int*)map->data.ptr, map->step,
                                 size, intrMatrix, distCoeffs,
                                 interpolate, icvPixSize[CV_MAT_TYPE(src->type)]));

    __END__;
}


/*F//////////////////////////////////////////////////////////////////////////////////////
//    Name: cvUnDistort
//    Purpose: The function corrects radial and tangential distortion in the frame
//             using previousely calculated arrays of distorted points indices and
//             undistortion coefficients
//    Context:
//    Parameters:  srcImage    - source (distorted) image
//                 dstImage    - output (undistorted) image
//                 data        - distortion data array
//                 interpolate - interpolation toggle (optional parameter)
//
//    Notes:  1. If interpolate=0, interpolation disabled;
//               else (default) bilinear interpolation is used.
//            2. Array data must be allocated and calculated by the cvUnDistortInit
//               function before. If interpolate = 0, its length must be
//               size.width*size.height elements; else 3*size.width*size.height.
//F*/
CV_IMPL void
cvUnDistort( const void* srcImage, void* dstImage,
             const void* undistMap, int interpolate )
{
    CV_FUNCNAME( "cvUnDistort" );

    __BEGIN__;

    int coi1 = 0, coi2 = 0, coi3 = 0;
    CvMat srcstub, *src = (CvMat*)srcImage;
    CvMat mapstub, *map = (CvMat*)undistMap;
    CvMat dststub, *dst = (CvMat*)dstImage;
    CvSize size;

    CV_CALL( src = cvGetMat( src, &srcstub, &coi1 ));
    CV_CALL( map = cvGetMat( map, &mapstub, &coi2 ));
    CV_CALL( dst = cvGetMat( dst, &dststub, &coi3 ));

    if( coi1 != 0 || coi2 != 0 || coi3 != 0 )
        CV_ERROR( CV_BadCOI, "" );

    if( CV_MAT_TYPE(src->type) != CV_8UC1 && CV_MAT_TYPE(src->type) != CV_8UC3 )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    if( !CV_ARE_TYPES_EQ( src, dst ))
        CV_ERROR( CV_StsUnmatchedFormats, "" );

    if( CV_MAT_TYPE( map->type ) != CV_32SC1 && CV_MAT_TYPE( map->type ) != CV_32SC3 )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    if( src->height > map->height ||
        interpolate && src->width*3 > map->width*CV_MAT_CN( map->type ) ||
        !interpolate && src->width > map->width )
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    if( !CV_ARE_SIZES_EQ( src, dst ))
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    size = icvGetMatSize( src );

    if( CV_MAT_TYPE( src->type ) == CV_8UC1 )
    {
        IPPI_CALL( icvUnDistort_8u_C1R( src->data.ptr, src->step,
                                        (int*)map->data.ptr, map->step,
                                        dst->data.ptr, dst->step,
                                        size, interpolate ));
    }
    else if( CV_MAT_TYPE( src->type ) == CV_8UC3 )
    {
        IPPI_CALL( icvUnDistort_8u_C3R( src->data.ptr, src->step,
                                        (int*)map->data.ptr, map->step, 
                                        dst->data.ptr, dst->step,
                                        size, interpolate ));
    }
    else
    {
        CV_ERROR( CV_StsUnsupportedFormat, "Undistorted image must have 8uC1 or 8uC3 format" );
    }

    __END__;
}


/*F//////////////////////////////////////////////////////////////////////////////////////
//    Name: cvConvertMap
//    Purpose: The function converts floating-point pixel coordinate map to
//             faster fixed-point map, used within cvUnDistort (cvRemap)
//    Context:
//    Parameters:  flUndistMap - source map: (width x height x 32fC2) or
//                                           (width*2 x height x 32fC1).
//                               each pair are pixel coordinates (x,y) in a source image.
//                 undistMap - resultant map: (width x height x 32sC3) or
//                                 (width*3 x height x 32sC1) if interpolation is enabled;
//                                 (width x height x 32sC1) if interpolation is disabled;
//                 pixelSize - bytes per pixel in images that will be transformed
//                             using the map. For byte-depth images it is just
//                             a number of channels.
//                 interpolate - interpolation flag (turned on by default)
//F*/
CV_IMPL void
cvConvertMap( const CvArr* srcImage, const CvArr* flUndistMap,
              CvArr* undistMap, int interpolate )
{
    CV_FUNCNAME( "cvConvertMap" );

    __BEGIN__;

    int coi1 = 0, coi2 = 0, coi3 = 0;
    int i, j;
    CvMat srcstub, *src = (CvMat*)srcImage;
    CvMat mapstub, *map = (CvMat*)undistMap;
    CvMat flmapstub, *flmap = (CvMat*)flUndistMap;
    CvSize size;
    CvPoint2D32f* flmapdata = 0;
    int srcStep, pixSize;

    CV_CALL( src = cvGetMat( src, &srcstub, &coi1 ));
    CV_CALL( map = cvGetMat( map, &mapstub, &coi2 ));
    CV_CALL( flmap = cvGetMat( flmap, &flmapstub, &coi3 ));

    if( coi1 != 0 || coi2 != 0 || coi3 != 0 )
        CV_ERROR( CV_BadCOI, "" );

    size = cvGetSize( src );

    if( CV_MAT_DEPTH( flmap->type ) != CV_32F )
        CV_ERROR( CV_StsUnsupportedFormat, "Source map should have 32f depth" );

    if( CV_MAT_CN( flmap->type ) > 2 ||
        size.width*2 != flmap->width * CV_MAT_CN( flmap->type ) ||
        size.height != flmap->height )
        CV_ERROR( CV_StsUnmatchedSizes, "Source map and source image have unmatched sizes");

    if( CV_MAT_TYPE( map->type ) != CV_32SC1 &&
        (CV_MAT_TYPE( map->type ) != CV_32SC3 || !interpolate))
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    if( map->height != size.height ||
        interpolate && size.width*3 != map->width*CV_MAT_CN( map->type ) ||
        !interpolate && size.width != map->width )
        CV_ERROR( CV_StsUnmatchedSizes, "" );

    flmapdata = (CvPoint2D32f*)flmap->data.ptr;
    pixSize = icvPixSize[CV_MAT_TYPE(src->type)];
    srcStep = src->step;

    if( !interpolate )
    {
        int* mapdata = (int*)map->data.ptr;

        for( i = 0; i < size.height; i++, (char*&)flmapdata += flmap->step,
                                          (char*&)mapdata += map->step )
        {
            for( j = 0; j < size.width; j++ )
            {
                int ix = cvRound(flmapdata[j].x);
                int iy = cvRound(flmapdata[j].y);

                if( (unsigned)ix < (unsigned)size.width &&
                    (unsigned)iy < (unsigned)size.height )
                    mapdata[j] = iy*srcStep + ix*pixSize;
                else
                    mapdata[j] = 0;
            }
        }
    }
    else
    {
        CvUnDistortData* mapdata = (CvUnDistortData*)map->data.ptr;

        for( i = 0; i < size.height; i++, (char*&)flmapdata += flmap->step,
                                          (char*&)mapdata += map->step )
        {
            for( j = 0; j < size.width; j++ )
            {
                float x = flmapdata[j].x;
                float y = flmapdata[j].y;
                int ix = cvFloor(x);
                int iy = cvFloor(y);

                x -= ix;
                y -= iy;

                if( (unsigned)ix < (unsigned)(size.width-1) &&
                    (unsigned)iy < (unsigned)(size.height-1) )
                {
                    mapdata[j].ind = iy*srcStep + ix*pixSize;
                    mapdata[j].a0 = (ushort)cvRound((1.f - x)*(1.f - y)*(1 << 15));
                    mapdata[j].a1 = (ushort)cvRound(x*(1.f - y)*(1 << 15));
                    mapdata[j].a2 = (ushort)cvRound((1.f - x)*y*(1 << 15));
                    mapdata[j].a3 = (ushort)cvRound(x*y*(1 << 15));
                }
                else
                {
                    mapdata[j].ind = 0;
                    mapdata[j].a0 = 0;
                    mapdata[j].a1 = 0;
                    mapdata[j].a2 = 0;
                    mapdata[j].a3 = 0;
                }
            }
        }
    }

    __END__;
}

/*  End of file  */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?