Commit 1e354070 authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #1379 from terfendail:PeiLin

parents 9a5103ce 969bb1ef
......@@ -12,3 +12,4 @@ Extended Image Processing
- Paillou Filter
- Fast Line Detector
- Deriche Filter
- Pei&Lin Normalization
......@@ -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
@{
......
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_PEILIN_HPP__
#define __OPENCV_PEILIN_HPP__
#include <opencv2/core.hpp>
namespace cv
{
//! @addtogroup ximgproc
//! @{
/**
* @brief Calculates an affine transformation that normalize given image using Pei&Lin Normalization.
*
* Assume given image \f$I=T(\bar{I})\f$ where \f$\bar{I}\f$ is a normalized image and \f$T\f$ is an affine transformation distorting this image by translation, rotation, scaling and skew.
* The function returns an affine transformation matrix corresponding to the transformation \f$T^{-1}\f$ 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.
* @return Transformation matrix corresponding to inversed image transformation
*/
CV_EXPORTS Matx23d PeiLinNormalization ( InputArray I );
/** @overload */
CV_EXPORTS_W void PeiLinNormalization ( InputArray I, OutputArray T );
}
#endif
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ximgproc.hpp>
#include <iostream>
static void help()
{
std::cout << "\nThis program demonstrates Pei&Lin Normalization\n"
"Usage:\n"
"./peilin [image1_name -- default is ../data/peilin_plane.png] [image2_name -- default is ../data/peilin_shape.png]\n" << std::endl;
}
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(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, "{help h | | }{ @input1 | ../data/peilin_plane.png | }{ @input2 | ../data/peilin_plane.png | }");
if (parser.has("help"))
{
help();
return 0;
}
std::string filename1 = parser.get<std::string>("@input1");
std::string filename2 = parser.get<std::string>("@input2");
cv::Mat I = cv::imread(filename1, 0);
if (I.empty())
{
std::cout << "Couldn't open image " << filename1 << std::endl;
return 0;
}
cv::Mat J = cv::imread(filename2, 0);
if (J.empty())
{
std::cout << "Couldn't open image " << filename2 << std::endl;
return 0;
}
cv::Mat N = I & cv::PeiLinNormalization ( I );
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;
}
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#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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 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 ) ) );
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment