diff --git a/modules/ximgproc/CMakeLists.txt b/modules/ximgproc/CMakeLists.txt index 6e163ca76..62a00bd15 100644 --- a/modules/ximgproc/CMakeLists.txt +++ b/modules/ximgproc/CMakeLists.txt @@ -1,2 +1,5 @@ set(the_description "Extended image processing module. It includes edge-aware filters and etc.") ocv_define_module(ximgproc opencv_core opencv_imgproc opencv_calib3d opencv_imgcodecs WRAP python) + +file(COPY samples/peilin_plane.png DESTINATION ${OpenCV_BINARY_DIR}/bin) +file(COPY samples/peilin_shape.png DESTINATION ${OpenCV_BINARY_DIR}/bin) \ No newline at end of file diff --git a/modules/ximgproc/README.md b/modules/ximgproc/README.md index 3cf3e1be2..077a3381b 100644 --- a/modules/ximgproc/README.md +++ b/modules/ximgproc/README.md @@ -12,3 +12,4 @@ Extended Image Processing - Paillou Filter - Fast Line Detector - Deriche Filter +- Pei&Lin Normalization diff --git a/modules/ximgproc/include/opencv2/ximgproc.hpp b/modules/ximgproc/include/opencv2/ximgproc.hpp index 7fdaae3e2..ff7c30631 100644 --- a/modules/ximgproc/include/opencv2/ximgproc.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc.hpp @@ -51,6 +51,7 @@ #include "ximgproc/paillou_filter.hpp" #include "ximgproc/fast_line_detector.hpp" #include "ximgproc/deriche_filter.hpp" +#include "ximgproc/peilin.hpp" /** @defgroup ximgproc Extended Image Processing @{ diff --git a/modules/ximgproc/include/opencv2/ximgproc/peilin.hpp b/modules/ximgproc/include/opencv2/ximgproc/peilin.hpp new file mode 100644 index 000000000..1d865270d --- /dev/null +++ b/modules/ximgproc/include/opencv2/ximgproc/peilin.hpp @@ -0,0 +1,66 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2008, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ +#ifndef __OPENCV_PEILIN_HPP__ +#define __OPENCV_PEILIN_HPP__ + +#include + +namespace cv +{ + //! @addtogroup ximgproc_filters + //! @{ + + /** + * @brief Calculates an affine transformation that normalize given image using Pei&Lin Normalization. + * + * Assume given image :math:`I=T(\bar{I})` where :math:`\bar{I}` is a normalized image and :math:`T` is is an affine transformation distorting this image by translation, rotation, scaling and skew. + * The function returns an affine transformation matrix corresponding to the transformation :math:`T^{-1}` described in [PeiLin95]. + * For more details about this implementation, please see + * [PeiLin95] Soo-Chang Pei and Chao-Nan Lin. Image normalization for pattern recognition. Image and Vision Computing, Vol. 13, N.10, pp. 711-723, 1995. + * + * @param I Given transformed image. + */ + CV_EXPORTS Matx23d PeiLinNormalization ( InputArray I ); + /** @overload */ + CV_EXPORTS_W void PeiLinNormalization ( InputArray I, OutputArray T ); +} + +#endif diff --git a/modules/ximgproc/samples/peilin.cpp b/modules/ximgproc/samples/peilin.cpp new file mode 100644 index 000000000..79b7fe3e8 --- /dev/null +++ b/modules/ximgproc/samples/peilin.cpp @@ -0,0 +1,71 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2008, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ +#include +#include +#include + +static inline cv::Mat operator& ( const cv::Mat& lhs, const cv::Matx23d& rhs ) +{ + cv::Mat ret; + cv::warpAffine ( lhs, ret, rhs, lhs.size(), cv::INTER_LINEAR ); + return ret; +} + +static inline cv::Mat operator& ( const cv::Matx23d& lhs, const cv::Mat& rhs ) +{ + cv::Mat ret; + cv::warpAffine ( rhs, ret, lhs, rhs.size(), cv::INTER_LINEAR | cv::WARP_INVERSE_MAP ); + return ret; +} + +int main() +{ + cv::Mat I = cv::imread ( "../data/peilin_plane.png", 0 ); + cv::Mat N = I & cv::PeiLinNormalization ( I ); + cv::Mat J = cv::imread ( "../data/peilin_shape.png", 0 ); + cv::Mat D = cv::PeiLinNormalization ( J ) & I; + cv::imshow ( "I", I ); + cv::imshow ( "N", N ); + cv::imshow ( "J", J ); + cv::imshow ( "D", D ); + cv::waitKey(); + return 0; +} diff --git a/modules/ximgproc/samples/peilin_plane.png b/modules/ximgproc/samples/peilin_plane.png new file mode 100644 index 000000000..1ef2a28d3 Binary files /dev/null and b/modules/ximgproc/samples/peilin_plane.png differ diff --git a/modules/ximgproc/samples/peilin_shape.png b/modules/ximgproc/samples/peilin_shape.png new file mode 100644 index 000000000..78e9514b9 Binary files /dev/null and b/modules/ximgproc/samples/peilin_shape.png differ diff --git a/modules/ximgproc/src/peilin.cpp b/modules/ximgproc/src/peilin.cpp new file mode 100644 index 000000000..9f6c7af16 --- /dev/null +++ b/modules/ximgproc/src/peilin.cpp @@ -0,0 +1,93 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2008, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ +#include "precomp.hpp" + +namespace cv +{ + + static inline Moments operator& ( const Moments & lhs, const Matx22d & rhs ) + { + return Moments ( + lhs.m00, + rhs ( 0, 0 ) * lhs.m10 + rhs ( 0, 1 ) * lhs.m01, + rhs ( 1, 0 ) * lhs.m10 + rhs ( 1, 1 ) * lhs.m01, + rhs ( 0, 0 ) * rhs ( 0, 0 ) * lhs.m20 + rhs ( 0, 1 ) * rhs ( 0, 1 ) * lhs.m02 + 2 * rhs ( 0, 0 ) * rhs ( 0, 1 ) * lhs.m11, + rhs ( 0, 0 ) * rhs ( 1, 0 ) * lhs.m20 + rhs ( 0, 1 ) * rhs ( 1, 1 ) * lhs.m02 + ( rhs ( 0, 0 ) * rhs ( 1, 1 ) + rhs ( 0, 1 ) * rhs ( 1, 0 ) ) * lhs.m11, + rhs ( 1, 0 ) * rhs ( 1, 0 ) * lhs.m20 + rhs ( 1, 1 ) * rhs ( 1, 1 ) * lhs.m02 + 2 * rhs ( 1, 0 ) * rhs ( 1, 1 ) * lhs.m11, + rhs ( 0, 0 ) * rhs ( 0, 0 ) * rhs ( 0, 0 ) * lhs.m30 + 3 * rhs ( 0, 0 ) * rhs ( 0, 0 ) * rhs ( 0, 1 ) * lhs.m21 + 3 * rhs ( 0, 0 ) * rhs ( 0, 1 ) * rhs ( 0, 1 ) * lhs.m12 + rhs ( 0, 1 ) * rhs ( 0, 1 ) * rhs ( 0, 1 ) * lhs.m03, + rhs ( 0, 0 ) * rhs ( 0, 0 ) * rhs ( 1, 0 ) * lhs.m30 + ( rhs ( 0, 0 ) * rhs ( 0, 0 ) * rhs ( 1, 1 ) + 2 * rhs ( 0, 0 ) * rhs ( 0, 1 ) * rhs ( 1, 0 ) ) * lhs.m21 + ( 2 * rhs ( 0, 0 ) * rhs ( 0, 1 ) * rhs ( 1, 1 ) + rhs ( 0, 1 ) * rhs ( 0, 1 ) * rhs ( 1, 0 ) ) * lhs.m12 + rhs ( 0, 1 ) * rhs ( 0, 1 ) * rhs ( 1, 1 ) * lhs.m03, + rhs ( 0, 0 ) * rhs ( 1, 0 ) * rhs ( 1, 0 ) * lhs.m30 + ( rhs ( 1, 0 ) * rhs ( 1, 0 ) * rhs ( 0, 1 ) + 2 * rhs ( 0, 0 ) * rhs ( 1, 0 ) * rhs ( 1, 1 ) ) * lhs.m21 + ( 2 * rhs ( 0, 1 ) * rhs ( 1, 0 ) * rhs ( 1, 1 ) + rhs ( 1, 1 ) * rhs ( 1, 1 ) * rhs ( 0, 0 ) ) * lhs.m12 + rhs ( 0, 1 ) * rhs ( 1, 1 ) * rhs ( 1, 1 ) * lhs.m03, + rhs ( 1, 0 ) * rhs ( 1, 0 ) * rhs ( 1, 0 ) * lhs.m30 + 3 * rhs ( 1, 0 ) * rhs ( 1, 0 ) * rhs ( 1, 1 ) * lhs.m21 + 3 * rhs ( 1, 0 ) * rhs ( 1, 1 ) * rhs ( 1, 1 ) * lhs.m12 + rhs ( 1, 1 ) * rhs ( 1, 1 ) * rhs ( 1, 1 ) * lhs.m03 + ); + } + + static inline Matx23d operator| ( const Matx22d & lhs, const Matx21d & rhs ) + { + return Matx23d ( lhs ( 0, 0 ), lhs ( 0, 1 ), rhs ( 0 ), lhs ( 1, 0 ), lhs ( 1, 1 ), rhs ( 1 ) ); + } + + Matx23d PeiLinNormalization ( InputArray I ) + { + const Moments M = moments ( I ); + const double l1 = ( M.mu20 / M.m00 + M.mu02 / M.m00 + sqrt ( ( M.mu20 / M.m00 - M.mu02 / M.m00 ) * ( M.mu20 / M.m00 - M.mu02 / M.m00 ) + 4 * M.mu11 / M.m00 * M.mu11 / M.m00 ) ) / 2; + const double l2 = ( M.mu20 / M.m00 + M.mu02 / M.m00 - sqrt ( ( M.mu20 / M.m00 - M.mu02 / M.m00 ) * ( M.mu20 / M.m00 - M.mu02 / M.m00 ) + 4 * M.mu11 / M.m00 * M.mu11 / M.m00 ) ) / 2; + const double ex = ( M.mu11 / M.m00 ) / sqrt ( ( l1 - M.mu20 / M.m00 ) * ( l1 - M.mu20 / M.m00 ) + M.mu11 / M.m00 * M.mu11 / M.m00 ); + const double ey = ( l1 - M.mu20 / M.m00 ) / sqrt ( ( l1 - M.mu20 / M.m00 ) * ( l1 - M.mu20 / M.m00 ) + M.mu11 / M.m00 * M.mu11 / M.m00 ); + const Matx22d E = Matx22d ( ex, ey, -ey, ex ); + const double p = min ( I.size().height, I.size().width ) / 8; + const Matx22d W = Matx22d ( p / sqrt ( l1 ), 0, 0, p / sqrt ( l2 ) ); + const Matx21d c = Matx21d ( M.m10 / M.m00, M.m01 / M.m00 ); + const Matx21d i = Matx21d ( I.size().width / 2, I.size().height / 2 ); + const Moments N = M & W * E; + const double t1 = N.mu12 + N.mu30; + const double t2 = N.mu03 + N.mu21; + const double phi = atan2 ( -t1, t2 ); + const double psi = ( -t1 * sin ( phi ) + t2 * cos ( phi ) >= 0 ) ? phi : ( phi + CV_PI ); + const Matx22d A = Matx22d ( cos ( psi ), sin ( psi ), -sin ( psi ), cos ( psi ) ); + return ( A * W * E ) | ( i - A * W * E * c ); + } + + void PeiLinNormalization ( InputArray I, OutputArray T ) + { + T.assign ( Mat ( PeiLinNormalization ( I ) ) ); + } + +}