2014-12-08 21:53:14 +08:00
|
|
|
/*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.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, 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 <cstring>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <direct.h>
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
#include "utility.hpp"
|
|
|
|
#include "opencv2/core.hpp"
|
2017-12-08 17:23:44 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/calib3d.hpp"
|
|
|
|
|
|
|
|
using namespace cv;
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 512
|
|
|
|
#endif /* PATH_MAX */
|
|
|
|
|
|
|
|
#define __BEGIN__ __CV_BEGIN__
|
|
|
|
#define __END__ __CV_END__
|
|
|
|
#define EXIT __CV_EXIT__
|
|
|
|
|
|
|
|
static int icvMkDir( const char* filename )
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char* p;
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
struct _stat st;
|
|
|
|
#else /* _WIN32 */
|
|
|
|
struct stat st;
|
|
|
|
mode_t mode;
|
|
|
|
|
|
|
|
mode = 0755;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
strcpy( path, filename );
|
|
|
|
|
|
|
|
p = path;
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
pos = (int)strcspn( p, "/\\" );
|
|
|
|
|
|
|
|
if( pos == (int) strlen( p ) ) break;
|
|
|
|
if( pos != 0 )
|
|
|
|
{
|
|
|
|
p[pos] = '\0';
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if( p[pos-1] != ':' )
|
|
|
|
{
|
|
|
|
if( _stat( path, &st ) != 0 )
|
|
|
|
{
|
|
|
|
if( _mkdir( path ) != 0 ) return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if( stat( path, &st ) != 0 )
|
|
|
|
{
|
|
|
|
if( mkdir( path, mode ) != 0 ) return 0;
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
p[pos] = '/';
|
|
|
|
|
|
|
|
p += pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void icvWriteVecHeader( FILE* file, int count, int width, int height )
|
|
|
|
{
|
|
|
|
int vecsize;
|
|
|
|
short tmp;
|
|
|
|
|
|
|
|
/* number of samples */
|
|
|
|
fwrite( &count, sizeof( count ), 1, file );
|
|
|
|
/* vector size */
|
|
|
|
vecsize = width * height;
|
|
|
|
fwrite( &vecsize, sizeof( vecsize ), 1, file );
|
|
|
|
/* min/max values */
|
|
|
|
tmp = 0;
|
|
|
|
fwrite( &tmp, sizeof( tmp ), 1, file );
|
|
|
|
fwrite( &tmp, sizeof( tmp ), 1, file );
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
static void icvWriteVecSample( FILE* file, Mat sample )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
uchar chartmp = 0;
|
2014-12-08 21:53:14 +08:00
|
|
|
fwrite( &chartmp, sizeof( chartmp ), 1, file );
|
2017-12-08 17:23:44 +08:00
|
|
|
for(int r = 0; r < sample.rows; r++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
for(int c = 0; c < sample.cols; c++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
short tmp = sample.at<uchar>(r,c);
|
2014-12-08 21:53:14 +08:00
|
|
|
fwrite( &tmp, sizeof( tmp ), 1, file );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculates coefficients of perspective transformation
|
|
|
|
* which maps <quad> into rectangle ((0,0), (w,0), (w,h), (h,0)):
|
|
|
|
*
|
|
|
|
* c00*xi + c01*yi + c02
|
|
|
|
* ui = ---------------------
|
|
|
|
* c20*xi + c21*yi + c22
|
|
|
|
*
|
|
|
|
* c10*xi + c11*yi + c12
|
|
|
|
* vi = ---------------------
|
|
|
|
* c20*xi + c21*yi + c22
|
|
|
|
*
|
|
|
|
* Coefficients are calculated by solving linear system:
|
|
|
|
* / x0 y0 1 0 0 0 -x0*u0 -y0*u0 \ /c00\ /u0\
|
|
|
|
* | x1 y1 1 0 0 0 -x1*u1 -y1*u1 | |c01| |u1|
|
|
|
|
* | x2 y2 1 0 0 0 -x2*u2 -y2*u2 | |c02| |u2|
|
|
|
|
* | x3 y3 1 0 0 0 -x3*u3 -y3*u3 |.|c10|=|u3|,
|
|
|
|
* | 0 0 0 x0 y0 1 -x0*v0 -y0*v0 | |c11| |v0|
|
|
|
|
* | 0 0 0 x1 y1 1 -x1*v1 -y1*v1 | |c12| |v1|
|
|
|
|
* | 0 0 0 x2 y2 1 -x2*v2 -y2*v2 | |c20| |v2|
|
|
|
|
* \ 0 0 0 x3 y3 1 -x3*v3 -y3*v3 / \c21/ \v3/
|
|
|
|
*
|
|
|
|
* where:
|
|
|
|
* (xi, yi) = (quad[i][0], quad[i][1])
|
|
|
|
* cij - coeffs[i][j], coeffs[2][2] = 1
|
|
|
|
* (ui, vi) - rectangle vertices
|
|
|
|
*/
|
2017-12-08 17:23:44 +08:00
|
|
|
static void cvGetPerspectiveTransform( Size src_size, double quad[4][2], double coeffs[3][3] )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
double a[8][8];
|
|
|
|
double b[8];
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat A( 8, 8, CV_64FC1, a );
|
|
|
|
Mat B( 8, 1, CV_64FC1, b );
|
|
|
|
Mat X( 8, 1, CV_64FC1, coeffs );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < 4; ++i )
|
|
|
|
{
|
|
|
|
a[i][0] = quad[i][0]; a[i][1] = quad[i][1]; a[i][2] = 1;
|
|
|
|
a[i][3] = a[i][4] = a[i][5] = a[i][6] = a[i][7] = 0;
|
|
|
|
b[i] = 0;
|
|
|
|
}
|
|
|
|
for( i = 4; i < 8; ++i )
|
|
|
|
{
|
|
|
|
a[i][3] = quad[i-4][0]; a[i][4] = quad[i-4][1]; a[i][5] = 1;
|
|
|
|
a[i][0] = a[i][1] = a[i][2] = a[i][6] = a[i][7] = 0;
|
|
|
|
b[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int u = src_size.width - 1;
|
|
|
|
int v = src_size.height - 1;
|
|
|
|
|
|
|
|
a[1][6] = -quad[1][0] * u; a[1][7] = -quad[1][1] * u;
|
|
|
|
a[2][6] = -quad[2][0] * u; a[2][7] = -quad[2][1] * u;
|
|
|
|
b[1] = b[2] = u;
|
|
|
|
|
|
|
|
a[6][6] = -quad[2][0] * v; a[6][7] = -quad[2][1] * v;
|
|
|
|
a[7][6] = -quad[3][0] * v; a[7][7] = -quad[3][1] * v;
|
|
|
|
b[6] = b[7] = v;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
solve( A, B, X );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
coeffs[2][2] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Warps source into destination by a perspective transform */
|
2017-12-08 17:23:44 +08:00
|
|
|
static void cvWarpPerspective( Mat src, Mat dst, double quad[4][2] )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
int fill_value = 0;
|
|
|
|
|
|
|
|
double c[3][3]; /* transformation coefficients */
|
|
|
|
double q[4][2]; /* rearranged quad */
|
|
|
|
|
|
|
|
int left = 0;
|
|
|
|
int right = 0;
|
|
|
|
int next_right = 0;
|
|
|
|
int next_left = 0;
|
|
|
|
double y_min = 0;
|
|
|
|
double y_max = 0;
|
|
|
|
double k_left, b_left, k_right, b_right;
|
|
|
|
|
|
|
|
double d = 0;
|
|
|
|
int direction = 0;
|
|
|
|
int i;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
if( src.type() != CV_8UC1 || src.dims != 2 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Error( Error::StsBadArg,
|
2014-12-08 21:53:14 +08:00
|
|
|
"Source must be two-dimensional array of CV_8UC1 type." );
|
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
if( dst.type() != CV_8UC1 || dst.dims != 2 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Error( Error::StsBadArg,
|
2014-12-08 21:53:14 +08:00
|
|
|
"Destination must be two-dimensional array of CV_8UC1 type." );
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
cvGetPerspectiveTransform( src.size(), quad, c );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
/* if direction > 0 then vertices in quad follow in a CW direction,
|
|
|
|
otherwise they follow in a CCW direction */
|
|
|
|
direction = 0;
|
|
|
|
for( i = 0; i < 4; ++i )
|
|
|
|
{
|
|
|
|
int ni = i + 1; if( ni == 4 ) ni = 0;
|
|
|
|
int pi = i - 1; if( pi == -1 ) pi = 3;
|
|
|
|
|
|
|
|
d = (quad[i][0] - quad[pi][0])*(quad[ni][1] - quad[i][1]) -
|
|
|
|
(quad[i][1] - quad[pi][1])*(quad[ni][0] - quad[i][0]);
|
2017-12-08 17:23:44 +08:00
|
|
|
int cur_direction = d > 0 ? 1 : d < 0 ? -1 : 0;
|
2014-12-08 21:53:14 +08:00
|
|
|
if( direction == 0 )
|
|
|
|
{
|
|
|
|
direction = cur_direction;
|
|
|
|
}
|
|
|
|
else if( direction * cur_direction < 0 )
|
|
|
|
{
|
|
|
|
direction = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( direction == 0 )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Quadrangle is nonconvex or degenerated." );
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* <left> is the index of the topmost quad vertice
|
|
|
|
if there are two such vertices <left> is the leftmost one */
|
|
|
|
left = 0;
|
|
|
|
for( i = 1; i < 4; ++i )
|
|
|
|
{
|
|
|
|
if( (quad[i][1] < quad[left][1]) ||
|
|
|
|
((quad[i][1] == quad[left][1]) && (quad[i][0] < quad[left][0])) )
|
|
|
|
{
|
|
|
|
left = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* rearrange <quad> vertices in such way that they follow in a CW
|
|
|
|
direction and the first vertice is the topmost one and put them
|
|
|
|
into <q> */
|
|
|
|
if( direction > 0 )
|
|
|
|
{
|
|
|
|
for( i = left; i < 4; ++i )
|
|
|
|
{
|
|
|
|
q[i-left][0] = quad[i][0];
|
|
|
|
q[i-left][1] = quad[i][1];
|
|
|
|
}
|
|
|
|
for( i = 0; i < left; ++i )
|
|
|
|
{
|
|
|
|
q[4-left+i][0] = quad[i][0];
|
|
|
|
q[4-left+i][1] = quad[i][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( i = left; i >= 0; --i )
|
|
|
|
{
|
|
|
|
q[left-i][0] = quad[i][0];
|
|
|
|
q[left-i][1] = quad[i][1];
|
|
|
|
}
|
|
|
|
for( i = 3; i > left; --i )
|
|
|
|
{
|
|
|
|
q[4+left-i][0] = quad[i][0];
|
|
|
|
q[4+left-i][1] = quad[i][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
left = right = 0;
|
|
|
|
/* if there are two topmost points, <right> is the index of the rightmost one
|
|
|
|
otherwise <right> */
|
|
|
|
if( q[left][1] == q[left+1][1] )
|
|
|
|
{
|
|
|
|
right = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* <next_left> follows <left> in a CCW direction */
|
|
|
|
next_left = 3;
|
|
|
|
/* <next_right> follows <right> in a CW direction */
|
|
|
|
next_right = right + 1;
|
|
|
|
|
|
|
|
/* subtraction of 1 prevents skipping of the first row */
|
|
|
|
y_min = q[left][1] - 1;
|
|
|
|
|
|
|
|
/* left edge equation: y = k_left * x + b_left */
|
|
|
|
k_left = (q[left][0] - q[next_left][0]) /
|
|
|
|
(q[left][1] - q[next_left][1]);
|
|
|
|
b_left = (q[left][1] * q[next_left][0] -
|
|
|
|
q[left][0] * q[next_left][1]) /
|
|
|
|
(q[left][1] - q[next_left][1]);
|
|
|
|
|
|
|
|
/* right edge equation: y = k_right * x + b_right */
|
|
|
|
k_right = (q[right][0] - q[next_right][0]) /
|
|
|
|
(q[right][1] - q[next_right][1]);
|
|
|
|
b_right = (q[right][1] * q[next_right][0] -
|
|
|
|
q[right][0] * q[next_right][1]) /
|
|
|
|
(q[right][1] - q[next_right][1]);
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
y_max = MIN( q[next_left][1], q[next_right][1] );
|
|
|
|
|
|
|
|
int iy_min = MAX( cvRound(y_min), 0 ) + 1;
|
2017-12-08 17:23:44 +08:00
|
|
|
int iy_max = MIN( cvRound(y_max), dst.rows - 1 );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
double x_min = k_left * iy_min + b_left;
|
|
|
|
double x_max = k_right * iy_min + b_right;
|
|
|
|
|
|
|
|
/* walk through the destination quadrangle row by row */
|
|
|
|
for( y = iy_min; y <= iy_max; ++y )
|
|
|
|
{
|
|
|
|
int ix_min = MAX( cvRound( x_min ), 0 );
|
2017-12-08 17:23:44 +08:00
|
|
|
int ix_max = MIN( cvRound( x_max ), dst.cols - 1 );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
for( x = ix_min; x <= ix_max; ++x )
|
|
|
|
{
|
|
|
|
/* calculate coordinates of the corresponding source array point */
|
|
|
|
double div = (c[2][0] * x + c[2][1] * y + c[2][2]);
|
|
|
|
double src_x = (c[0][0] * x + c[0][1] * y + c[0][2]) / div;
|
|
|
|
double src_y = (c[1][0] * x + c[1][1] * y + c[1][2]) / div;
|
|
|
|
|
|
|
|
int isrc_x = cvFloor( src_x );
|
|
|
|
int isrc_y = cvFloor( src_y );
|
|
|
|
double delta_x = src_x - isrc_x;
|
|
|
|
double delta_y = src_y - isrc_y;
|
|
|
|
|
|
|
|
int i00, i10, i01, i11;
|
|
|
|
i00 = i10 = i01 = i11 = (int) fill_value;
|
|
|
|
|
|
|
|
/* linear interpolation using 2x2 neighborhood */
|
2017-12-08 17:23:44 +08:00
|
|
|
if( isrc_x >= 0 && isrc_x <= src.cols &&
|
|
|
|
isrc_y >= 0 && isrc_y <= src.rows )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
i00 = src.at<uchar>(isrc_y, isrc_x);
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
if( isrc_x >= -1 && isrc_x < src.cols &&
|
|
|
|
isrc_y >= 0 && isrc_y <= src.rows )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
i10 = src.at<uchar>(isrc_y, isrc_x + 1);
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
if( isrc_x >= 0 && isrc_x <= src.cols &&
|
|
|
|
isrc_y >= -1 && isrc_y < src.rows )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
i01 = src.at<uchar>(isrc_y + 1, isrc_x);
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
if( isrc_x >= -1 && isrc_x < src.cols &&
|
|
|
|
isrc_y >= -1 && isrc_y < src.rows )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
i11 = src.at<uchar>(isrc_y + 1, isrc_x + 1);
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double i0 = i00 + (i10 - i00)*delta_x;
|
|
|
|
double i1 = i01 + (i11 - i01)*delta_x;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
dst.at<uchar>(y, x) = (uchar) (i0 + (i1 - i0)*delta_y);
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
x_min += k_left;
|
|
|
|
x_max += k_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (next_left == next_right) ||
|
|
|
|
(next_left+1 == next_right && q[next_left][1] == q[next_right][1]) )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( y_max == q[next_left][1] )
|
|
|
|
{
|
|
|
|
left = next_left;
|
|
|
|
next_left = left - 1;
|
|
|
|
|
|
|
|
k_left = (q[left][0] - q[next_left][0]) /
|
|
|
|
(q[left][1] - q[next_left][1]);
|
|
|
|
b_left = (q[left][1] * q[next_left][0] -
|
|
|
|
q[left][0] * q[next_left][1]) /
|
|
|
|
(q[left][1] - q[next_left][1]);
|
|
|
|
}
|
|
|
|
if( y_max == q[next_right][1] )
|
|
|
|
{
|
|
|
|
right = next_right;
|
|
|
|
next_right = right + 1;
|
|
|
|
|
|
|
|
k_right = (q[right][0] - q[next_right][0]) /
|
|
|
|
(q[right][1] - q[next_right][1]);
|
|
|
|
b_right = (q[right][1] * q[next_right][0] -
|
|
|
|
q[right][0] * q[next_right][1]) /
|
|
|
|
(q[right][1] - q[next_right][1]);
|
|
|
|
}
|
|
|
|
y_min = y_max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void icvRandomQuad( int width, int height, double quad[4][2],
|
|
|
|
double maxxangle,
|
|
|
|
double maxyangle,
|
|
|
|
double maxzangle )
|
|
|
|
{
|
|
|
|
double distfactor = 3.0;
|
|
|
|
double distfactor2 = 1.0;
|
|
|
|
|
|
|
|
double halfw, halfh;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
double rotVectData[3];
|
|
|
|
double vectData[3];
|
|
|
|
double rotMatData[9];
|
|
|
|
|
|
|
|
double d;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat rotVect( 3, 1, CV_64FC1, &rotVectData[0] );
|
|
|
|
Mat rotMat( 3, 3, CV_64FC1, &rotMatData[0] );
|
|
|
|
Mat vect( 3, 1, CV_64FC1, &vectData[0] );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
rotVectData[0] = maxxangle * (2.0 * rand() / RAND_MAX - 1.0);
|
|
|
|
rotVectData[1] = ( maxyangle - fabs( rotVectData[0] ) )
|
|
|
|
* (2.0 * rand() / RAND_MAX - 1.0);
|
|
|
|
rotVectData[2] = maxzangle * (2.0 * rand() / RAND_MAX - 1.0);
|
|
|
|
d = (distfactor + distfactor2 * (2.0 * rand() / RAND_MAX - 1.0)) * width;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
Rodrigues( rotVect, rotMat );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
halfw = 0.5 * width;
|
|
|
|
halfh = 0.5 * height;
|
|
|
|
|
|
|
|
quad[0][0] = -halfw;
|
|
|
|
quad[0][1] = -halfh;
|
|
|
|
quad[1][0] = halfw;
|
|
|
|
quad[1][1] = -halfh;
|
|
|
|
quad[2][0] = halfw;
|
|
|
|
quad[2][1] = halfh;
|
|
|
|
quad[3][0] = -halfw;
|
|
|
|
quad[3][1] = halfh;
|
|
|
|
|
|
|
|
for( i = 0; i < 4; i++ )
|
|
|
|
{
|
|
|
|
rotVectData[0] = quad[i][0];
|
|
|
|
rotVectData[1] = quad[i][1];
|
|
|
|
rotVectData[2] = 0.0;
|
2017-12-08 17:23:44 +08:00
|
|
|
gemm(rotMat, rotVect, 1., Mat(), 1., vect);
|
2014-12-08 21:53:14 +08:00
|
|
|
quad[i][0] = vectData[0] * d / (d + vectData[2]) + halfw;
|
|
|
|
quad[i][1] = vectData[1] * d / (d + vectData[2]) + halfh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct CvSampleDistortionData
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat src;
|
|
|
|
Mat erode;
|
|
|
|
Mat dilate;
|
|
|
|
Mat mask;
|
|
|
|
Mat img;
|
|
|
|
Mat maskimg;
|
2014-12-08 21:53:14 +08:00
|
|
|
int dx;
|
|
|
|
int dy;
|
|
|
|
int bgcolor;
|
|
|
|
} CvSampleDistortionData;
|
|
|
|
|
|
|
|
#if defined CV_OPENMP && (defined _MSC_VER || defined CV_ICC)
|
|
|
|
#define CV_OPENMP 1
|
|
|
|
#else
|
|
|
|
#undef CV_OPENMP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct CvBackgroundData
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
char** filename;
|
|
|
|
int last;
|
|
|
|
int round;
|
2017-12-08 17:23:44 +08:00
|
|
|
Size winsize;
|
2014-12-08 21:53:14 +08:00
|
|
|
} CvBackgroundData;
|
|
|
|
|
|
|
|
typedef struct CvBackgroundReader
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat src;
|
|
|
|
Mat img;
|
|
|
|
Point offset;
|
2014-12-08 21:53:14 +08:00
|
|
|
float scale;
|
|
|
|
float scalefactor;
|
|
|
|
float stepfactor;
|
2017-12-08 17:23:44 +08:00
|
|
|
Point point;
|
2014-12-08 21:53:14 +08:00
|
|
|
} CvBackgroundReader;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Background reader
|
|
|
|
* Created in each thread
|
|
|
|
*/
|
|
|
|
CvBackgroundReader* cvbgreader = NULL;
|
|
|
|
|
|
|
|
#if defined CV_OPENMP
|
|
|
|
#pragma omp threadprivate(cvbgreader)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CvBackgroundData* cvbgdata = NULL;
|
|
|
|
|
|
|
|
static int icvStartSampleDistortion( const char* imgfilename, int bgcolor, int bgthreshold,
|
|
|
|
CvSampleDistortionData* data )
|
|
|
|
{
|
|
|
|
memset( data, 0, sizeof( *data ) );
|
2017-12-08 17:23:44 +08:00
|
|
|
data->src = imread( imgfilename, IMREAD_GRAYSCALE );
|
|
|
|
if( !(data->src.empty()) && data->src.type() == CV_8UC1 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
int r, c;
|
2017-12-08 17:23:44 +08:00
|
|
|
|
|
|
|
data->dx = data->src.cols / 2;
|
|
|
|
data->dy = data->src.rows / 2;
|
2014-12-08 21:53:14 +08:00
|
|
|
data->bgcolor = bgcolor;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
data->mask = data->src.clone();
|
|
|
|
data->erode = data->src.clone();
|
|
|
|
data->dilate = data->src.clone();
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
/* make mask image */
|
2017-12-08 17:23:44 +08:00
|
|
|
for( r = 0; r < data->mask.rows; r++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
for( c = 0; c < data->mask.cols; c++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
uchar& pmask = data->mask.at<uchar>(r, c);
|
|
|
|
if( bgcolor - bgthreshold <= (int)pmask &&
|
|
|
|
(int)pmask <= bgcolor + bgthreshold )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
pmask = (uchar) 0;
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
pmask = (uchar) 255;
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extend borders of source image */
|
2017-12-08 17:23:44 +08:00
|
|
|
erode( data->src, data->erode, Mat() );
|
|
|
|
dilate( data->src, data->dilate, Mat() );
|
|
|
|
for( r = 0; r < data->mask.rows; r++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
for( c = 0; c < data->mask.cols; c++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
uchar& pmask = data->mask.at<uchar>(r, c);
|
|
|
|
if( pmask == 0 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
uchar& psrc = data->src.at<uchar>(r, c);
|
|
|
|
uchar& perode = data->erode.at<uchar>(r, c);
|
|
|
|
uchar& pdilate = data->dilate.at<uchar>(r, c);
|
|
|
|
uchar de = (uchar)(bgcolor - perode);
|
|
|
|
uchar dd = (uchar)(pdilate - bgcolor);
|
2014-12-08 21:53:14 +08:00
|
|
|
if( de >= dd && de > bgthreshold )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
psrc = perode;
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
if( dd > de && dd > bgthreshold )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
psrc = pdilate;
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
data->img = Mat(Size( data->src.cols + 2 * data->dx, data->src.rows + 2 * data->dy ), CV_8UC1);
|
|
|
|
data->maskimg = Mat(Size(data->src.cols + 2 * data->dx, data->src.rows + 2 * data->dy), CV_8UC1);
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2017-12-08 17:23:44 +08:00
|
|
|
void icvPlaceDistortedSample( Mat background,
|
2014-12-08 21:53:14 +08:00
|
|
|
int inverse, int maxintensitydev,
|
|
|
|
double maxxangle, double maxyangle, double maxzangle,
|
|
|
|
int inscribe, double maxshiftf, double maxscalef,
|
|
|
|
CvSampleDistortionData* data )
|
|
|
|
{
|
|
|
|
double quad[4][2];
|
|
|
|
int r, c;
|
|
|
|
int forecolordev;
|
|
|
|
float scale;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
Rect cr;
|
|
|
|
Rect roi;
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
double xshift, yshift, randscale;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
icvRandomQuad( data->src.cols, data->src.rows, quad,
|
2014-12-08 21:53:14 +08:00
|
|
|
maxxangle, maxyangle, maxzangle );
|
|
|
|
quad[0][0] += (double) data->dx;
|
|
|
|
quad[0][1] += (double) data->dy;
|
|
|
|
quad[1][0] += (double) data->dx;
|
|
|
|
quad[1][1] += (double) data->dy;
|
|
|
|
quad[2][0] += (double) data->dx;
|
|
|
|
quad[2][1] += (double) data->dy;
|
|
|
|
quad[3][0] += (double) data->dx;
|
|
|
|
quad[3][1] += (double) data->dy;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
data->img = data->bgcolor;
|
|
|
|
data->maskimg = 0;
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
cvWarpPerspective( data->src, data->img, quad );
|
|
|
|
cvWarpPerspective( data->mask, data->maskimg, quad );
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
GaussianBlur( data->maskimg, data->maskimg, Size(3, 3), 0, 0 );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
cr.x = data->dx;
|
|
|
|
cr.y = data->dy;
|
2017-12-08 17:23:44 +08:00
|
|
|
cr.width = data->src.cols;
|
|
|
|
cr.height = data->src.rows;
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
if( inscribe )
|
|
|
|
{
|
|
|
|
/* quad's circumscribing rectangle */
|
|
|
|
cr.x = (int) MIN( quad[0][0], quad[3][0] );
|
|
|
|
cr.y = (int) MIN( quad[0][1], quad[1][1] );
|
|
|
|
cr.width = (int) (MAX( quad[1][0], quad[2][0] ) + 0.5F ) - cr.x;
|
|
|
|
cr.height = (int) (MAX( quad[2][1], quad[3][1] ) + 0.5F ) - cr.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
xshift = maxshiftf * rand() / RAND_MAX;
|
|
|
|
yshift = maxshiftf * rand() / RAND_MAX;
|
|
|
|
|
|
|
|
cr.x -= (int) ( xshift * cr.width );
|
|
|
|
cr.y -= (int) ( yshift * cr.height );
|
|
|
|
cr.width = (int) ((1.0 + maxshiftf) * cr.width );
|
|
|
|
cr.height = (int) ((1.0 + maxshiftf) * cr.height);
|
|
|
|
|
|
|
|
randscale = maxscalef * rand() / RAND_MAX;
|
|
|
|
cr.x -= (int) ( 0.5 * randscale * cr.width );
|
|
|
|
cr.y -= (int) ( 0.5 * randscale * cr.height );
|
|
|
|
cr.width = (int) ((1.0 + randscale) * cr.width );
|
|
|
|
cr.height = (int) ((1.0 + randscale) * cr.height);
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
scale = MAX( ((float) cr.width) / background.cols, ((float) cr.height) / background.rows );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
roi.x = (int) (-0.5F * (scale * background.cols - cr.width) + cr.x);
|
|
|
|
roi.y = (int) (-0.5F * (scale * background.rows - cr.height) + cr.y);
|
|
|
|
roi.width = (int) (scale * background.cols);
|
|
|
|
roi.height = (int) (scale * background.rows);
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat img( background.size(), CV_8UC1 );
|
|
|
|
Mat maskimg( background.size(), CV_8UC1 );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
resize( data->img(roi), img, img.size(), 0, 0, INTER_LINEAR);
|
|
|
|
resize( data->maskimg(roi), maskimg, maskimg.size(), 0, 0, INTER_LINEAR);
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
forecolordev = (int) (maxintensitydev * (2.0 * rand() / RAND_MAX - 1.0));
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
for( r = 0; r < img.rows; r++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
for( c = 0; c < img.cols; c++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
uchar& pbg = background.at<uchar>(r, c);
|
|
|
|
uchar& palpha = maskimg.at<uchar>(r, c);
|
|
|
|
uchar chartmp = (uchar) MAX( 0, MIN( 255, forecolordev + img.at<uchar>(r, c)) );
|
2014-12-08 21:53:14 +08:00
|
|
|
if( inverse )
|
|
|
|
{
|
|
|
|
chartmp ^= 0xFF;
|
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
pbg = (uchar) ((chartmp*palpha + (255 - palpha)*pbg) / 255);
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2017-12-08 17:23:44 +08:00
|
|
|
CvBackgroundData* icvCreateBackgroundData( const char* filename, Size winsize )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
CvBackgroundData* data = NULL;
|
|
|
|
|
|
|
|
const char* dir = NULL;
|
|
|
|
char full[PATH_MAX];
|
|
|
|
char* imgfilename = NULL;
|
|
|
|
size_t datasize = 0;
|
|
|
|
int count = 0;
|
|
|
|
FILE* input = NULL;
|
|
|
|
char* tmp = NULL;
|
|
|
|
int len = 0;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( filename != NULL );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
dir = strrchr( filename, '\\' );
|
|
|
|
if( dir == NULL )
|
|
|
|
{
|
|
|
|
dir = strrchr( filename, '/' );
|
|
|
|
}
|
|
|
|
if( dir == NULL )
|
|
|
|
{
|
|
|
|
imgfilename = &(full[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy( &(full[0]), filename, (dir - filename + 1) );
|
|
|
|
imgfilename = &(full[(dir - filename + 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
input = fopen( filename, "r" );
|
|
|
|
if( input != NULL )
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
datasize = 0;
|
|
|
|
|
|
|
|
/* count */
|
|
|
|
while( !feof( input ) )
|
|
|
|
{
|
|
|
|
*imgfilename = '\0';
|
|
|
|
if( !fgets( imgfilename, PATH_MAX - (int)(imgfilename - full) - 1, input ))
|
|
|
|
break;
|
|
|
|
len = (int)strlen( imgfilename );
|
|
|
|
for( ; len > 0 && isspace(imgfilename[len-1]); len-- )
|
|
|
|
imgfilename[len-1] = '\0';
|
|
|
|
if( len > 0 )
|
|
|
|
{
|
|
|
|
if( (*imgfilename) == '#' ) continue; /* comment */
|
|
|
|
count++;
|
|
|
|
datasize += sizeof( char ) * (strlen( &(full[0]) ) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( count > 0 )
|
|
|
|
{
|
|
|
|
//rewind( input );
|
|
|
|
fseek( input, 0, SEEK_SET );
|
|
|
|
datasize += sizeof( *data ) + sizeof( char* ) * count;
|
2017-12-08 17:23:44 +08:00
|
|
|
data = (CvBackgroundData*) fastMalloc( datasize );
|
2014-12-08 21:53:14 +08:00
|
|
|
memset( (void*) data, 0, datasize );
|
|
|
|
data->count = count;
|
|
|
|
data->filename = (char**) (data + 1);
|
|
|
|
data->last = 0;
|
|
|
|
data->round = 0;
|
|
|
|
data->winsize = winsize;
|
|
|
|
tmp = (char*) (data->filename + data->count);
|
|
|
|
count = 0;
|
|
|
|
while( !feof( input ) )
|
|
|
|
{
|
|
|
|
*imgfilename = '\0';
|
|
|
|
if( !fgets( imgfilename, PATH_MAX - (int)(imgfilename - full) - 1, input ))
|
|
|
|
break;
|
|
|
|
len = (int)strlen( imgfilename );
|
|
|
|
if( len > 0 && imgfilename[len-1] == '\n' )
|
|
|
|
imgfilename[len-1] = 0, len--;
|
|
|
|
if( len > 0 )
|
|
|
|
{
|
|
|
|
if( (*imgfilename) == '#' ) continue; /* comment */
|
|
|
|
data->filename[count++] = tmp;
|
|
|
|
strcpy( tmp, &(full[0]) );
|
|
|
|
tmp += strlen( &(full[0]) ) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose( input );
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
CvBackgroundReader* icvCreateBackgroundReader()
|
|
|
|
{
|
|
|
|
CvBackgroundReader* reader = NULL;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
reader = new CvBackgroundReader;
|
2014-12-08 21:53:14 +08:00
|
|
|
reader->scale = 1.0F;
|
|
|
|
reader->scalefactor = 1.4142135623730950488016887242097F;
|
|
|
|
reader->stepfactor = 0.5F;
|
|
|
|
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void icvGetNextFromBackgroundData( CvBackgroundData* data,
|
|
|
|
CvBackgroundReader* reader )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat img;
|
2014-12-08 21:53:14 +08:00
|
|
|
int round = 0;
|
|
|
|
int i = 0;
|
2017-12-08 17:23:44 +08:00
|
|
|
Point offset;
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( data != NULL && reader != NULL );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
#ifdef CV_OPENMP
|
|
|
|
#pragma omp critical(c_background_data)
|
|
|
|
#endif /* CV_OPENMP */
|
|
|
|
{
|
|
|
|
for( i = 0; i < data->count; i++ )
|
|
|
|
{
|
|
|
|
round = data->round;
|
|
|
|
|
2017-12-04 05:46:22 +08:00
|
|
|
data->last = rand() % data->count;
|
|
|
|
|
2014-12-08 21:53:14 +08:00
|
|
|
#ifdef CV_VERBOSE
|
|
|
|
printf( "Open background image: %s\n", data->filename[data->last] );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
img = imread( data->filename[data->last], IMREAD_GRAYSCALE );
|
|
|
|
if( img.empty() )
|
2014-12-08 21:53:14 +08:00
|
|
|
continue;
|
|
|
|
data->round += data->last / data->count;
|
|
|
|
data->round = data->round % (data->winsize.width * data->winsize.height);
|
|
|
|
|
|
|
|
offset.x = round % data->winsize.width;
|
|
|
|
offset.y = round / data->winsize.width;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
offset.x = MIN( offset.x, img.cols - data->winsize.width );
|
|
|
|
offset.y = MIN( offset.y, img.rows - data->winsize.height );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
if( !img.empty() && img.type() == CV_8UC1 && offset.x >= 0 && offset.y >= 0 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
img = Mat();
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
if( img.empty() )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
/* no appropriate image */
|
|
|
|
|
|
|
|
#ifdef CV_VERBOSE
|
|
|
|
printf( "Invalid background description file.\n" );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( 0 );
|
2014-12-08 21:53:14 +08:00
|
|
|
exit( 1 );
|
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
|
|
|
|
reader->src = img;
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
//reader->offset.x = round % data->winsize.width;
|
|
|
|
//reader->offset.y = round / data->winsize.width;
|
|
|
|
reader->offset = offset;
|
|
|
|
reader->point = reader->offset;
|
|
|
|
reader->scale = MAX(
|
|
|
|
((float) data->winsize.width + reader->point.x) / ((float) reader->src.cols),
|
|
|
|
((float) data->winsize.height + reader->point.y) / ((float) reader->src.rows) );
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
resize( reader->src, reader->img,
|
|
|
|
Size((int)(reader->scale * reader->src.rows + 0.5F), (int)(reader->scale * reader->src.cols + 0.5F)), 0, 0, INTER_LINEAR );
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* icvGetBackgroundImage
|
|
|
|
*
|
|
|
|
* Get an image from background
|
|
|
|
* <img> must be allocated and have size, previously passed to icvInitBackgroundReaders
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
* icvInitBackgroundReaders( "bg.txt", cvSize( 24, 24 ) );
|
|
|
|
* ...
|
|
|
|
* #pragma omp parallel
|
|
|
|
* {
|
|
|
|
* ...
|
|
|
|
* icvGetBackgourndImage( cvbgdata, cvbgreader, img );
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* icvDestroyBackgroundReaders();
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
void icvGetBackgroundImage( CvBackgroundData* data,
|
|
|
|
CvBackgroundReader* reader,
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat& img )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( data != NULL && reader != NULL );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
if( reader->img.empty() )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
icvGetNextFromBackgroundData( data, reader );
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
img = reader->img(Rect(reader->point.x, reader->point.y, data->winsize.height, data->winsize.width)).clone();
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
if( (int) ( reader->point.x + (1.0F + reader->stepfactor ) * data->winsize.width )
|
|
|
|
< reader->img.cols )
|
|
|
|
{
|
|
|
|
reader->point.x += (int) (reader->stepfactor * data->winsize.width);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader->point.x = reader->offset.x;
|
|
|
|
if( (int) ( reader->point.y + (1.0F + reader->stepfactor ) * data->winsize.height )
|
|
|
|
< reader->img.rows )
|
|
|
|
{
|
|
|
|
reader->point.y += (int) (reader->stepfactor * data->winsize.height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader->point.y = reader->offset.y;
|
|
|
|
reader->scale *= reader->scalefactor;
|
|
|
|
if( reader->scale <= 1.0F )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
resize(reader->src, reader->img,
|
|
|
|
Size((int)(reader->scale * reader->src.rows), (int)(reader->scale * reader->src.cols)), 0, 0, INTER_LINEAR);
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
icvGetNextFromBackgroundData( data, reader );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* icvInitBackgroundReaders
|
|
|
|
*
|
|
|
|
* Initialize background reading process.
|
|
|
|
* <cvbgreader> and <cvbgdata> are initialized.
|
|
|
|
* Must be called before any usage of background
|
|
|
|
*
|
|
|
|
* filename - name of background description file
|
|
|
|
* winsize - size of images will be obtained from background
|
|
|
|
*
|
|
|
|
* return 1 on success, 0 otherwise.
|
|
|
|
*/
|
2017-12-08 17:23:44 +08:00
|
|
|
static int icvInitBackgroundReaders( const char* filename, Size winsize )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
if( cvbgdata == NULL && filename != NULL )
|
|
|
|
{
|
|
|
|
cvbgdata = icvCreateBackgroundData( filename, winsize );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cvbgdata )
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef CV_OPENMP
|
|
|
|
#pragma omp parallel
|
|
|
|
#endif /* CV_OPENMP */
|
|
|
|
{
|
|
|
|
#ifdef CV_OPENMP
|
|
|
|
#pragma omp critical(c_create_bg_data)
|
|
|
|
#endif /* CV_OPENMP */
|
|
|
|
{
|
|
|
|
if( cvbgreader == NULL )
|
|
|
|
{
|
|
|
|
cvbgreader = icvCreateBackgroundReader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return (cvbgdata != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* icvDestroyBackgroundReaders
|
|
|
|
*
|
|
|
|
* Finish backgournd reading process
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
void icvDestroyBackgroundReaders()
|
|
|
|
{
|
|
|
|
/* release background reader in each thread */
|
|
|
|
#ifdef CV_OPENMP
|
|
|
|
#pragma omp parallel
|
|
|
|
#endif /* CV_OPENMP */
|
|
|
|
{
|
|
|
|
#ifdef CV_OPENMP
|
|
|
|
#pragma omp critical(c_release_bg_data)
|
|
|
|
#endif /* CV_OPENMP */
|
|
|
|
{
|
|
|
|
if( cvbgreader != NULL )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
delete cvbgreader;
|
2014-12-08 21:53:14 +08:00
|
|
|
cvbgreader = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cvbgdata != NULL )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
fastFree(cvbgdata);
|
2014-12-08 21:53:14 +08:00
|
|
|
cvbgdata = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cvCreateTrainingSamples( const char* filename,
|
|
|
|
const char* imgfilename, int bgcolor, int bgthreshold,
|
|
|
|
const char* bgfilename, int count,
|
|
|
|
int invert, int maxintensitydev,
|
|
|
|
double maxxangle, double maxyangle, double maxzangle,
|
|
|
|
int showsamples,
|
|
|
|
int winwidth, int winheight )
|
|
|
|
{
|
|
|
|
CvSampleDistortionData data;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( filename != NULL );
|
|
|
|
CV_Assert( imgfilename != NULL );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
if( !icvMkDir( filename ) )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Unable to create output file: %s\n", filename );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( icvStartSampleDistortion( imgfilename, bgcolor, bgthreshold, &data ) )
|
|
|
|
{
|
|
|
|
FILE* output = NULL;
|
|
|
|
|
|
|
|
output = fopen( filename, "wb" );
|
|
|
|
if( output != NULL )
|
|
|
|
{
|
|
|
|
int hasbg;
|
|
|
|
int i;
|
|
|
|
int inverse;
|
|
|
|
|
|
|
|
hasbg = 0;
|
|
|
|
hasbg = (bgfilename != NULL && icvInitBackgroundReaders( bgfilename,
|
2017-12-08 17:23:44 +08:00
|
|
|
Size( winwidth,winheight ) ) );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat sample( winheight, winwidth, CV_8UC1 );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
icvWriteVecHeader( output, count, sample.cols, sample.rows );
|
|
|
|
|
|
|
|
if( showsamples )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
namedWindow( "Sample", WINDOW_AUTOSIZE );
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inverse = invert;
|
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
if( hasbg )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
icvGetBackgroundImage( cvbgdata, cvbgreader, sample );
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
sample = bgcolor;
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( invert == CV_RANDOM_INVERT )
|
|
|
|
{
|
|
|
|
inverse = (rand() > (RAND_MAX/2));
|
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
icvPlaceDistortedSample( sample, inverse, maxintensitydev,
|
2014-12-08 21:53:14 +08:00
|
|
|
maxxangle, maxyangle, maxzangle,
|
|
|
|
0 /* nonzero means placing image without cut offs */,
|
|
|
|
0.0 /* nozero adds random shifting */,
|
|
|
|
0.0 /* nozero adds random scaling */,
|
|
|
|
&data );
|
|
|
|
|
|
|
|
if( showsamples )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
imshow( "Sample", sample );
|
|
|
|
if( (waitKey( 0 ) & 0xFF) == 27 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
showsamples = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
icvWriteVecSample( output, sample );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
#ifdef CV_VERBOSE
|
|
|
|
if( i % 500 == 0 )
|
|
|
|
{
|
|
|
|
printf( "\r%3d%%", 100 * i / count );
|
|
|
|
}
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
}
|
|
|
|
icvDestroyBackgroundReaders();
|
|
|
|
fclose( output );
|
|
|
|
} /* if( output != NULL ) */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CV_VERBOSE
|
|
|
|
printf( "\r \r" );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CV_INFO_FILENAME "info.dat"
|
|
|
|
|
|
|
|
void cvCreateTestSamples( const char* infoname,
|
|
|
|
const char* imgfilename, int bgcolor, int bgthreshold,
|
|
|
|
const char* bgfilename, int count,
|
|
|
|
int invert, int maxintensitydev,
|
|
|
|
double maxxangle, double maxyangle, double maxzangle,
|
|
|
|
int showsamples,
|
2016-04-21 10:53:52 +08:00
|
|
|
int winwidth, int winheight, double maxscale )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
CvSampleDistortionData data;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( infoname != NULL );
|
|
|
|
CV_Assert( imgfilename != NULL );
|
|
|
|
CV_Assert( bgfilename != NULL );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
if( !icvMkDir( infoname ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
#if CV_VERBOSE
|
|
|
|
fprintf( stderr, "Unable to create directory hierarchy: %s\n", infoname );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( icvStartSampleDistortion( imgfilename, bgcolor, bgthreshold, &data ) )
|
|
|
|
{
|
|
|
|
char fullname[PATH_MAX];
|
|
|
|
char* filename;
|
|
|
|
FILE* info;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
if( icvInitBackgroundReaders( bgfilename, Size( 10, 10 ) ) )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int x, y, width, height;
|
|
|
|
float scale;
|
|
|
|
int inverse;
|
|
|
|
|
|
|
|
if( showsamples )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
namedWindow( "Image", WINDOW_AUTOSIZE );
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
info = fopen( infoname, "w" );
|
|
|
|
strcpy( fullname, infoname );
|
|
|
|
filename = strrchr( fullname, '\\' );
|
|
|
|
if( filename == NULL )
|
|
|
|
{
|
|
|
|
filename = strrchr( fullname, '/' );
|
|
|
|
}
|
|
|
|
if( filename == NULL )
|
|
|
|
{
|
|
|
|
filename = fullname;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename++;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = MIN( count, cvbgdata->count );
|
|
|
|
inverse = invert;
|
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
icvGetNextFromBackgroundData( cvbgdata, cvbgreader );
|
2016-04-21 10:53:52 +08:00
|
|
|
if( maxscale < 0.0 )
|
|
|
|
{
|
|
|
|
maxscale = MIN( 0.7F * cvbgreader->src.cols / winwidth,
|
2014-12-08 21:53:14 +08:00
|
|
|
0.7F * cvbgreader->src.rows / winheight );
|
2016-04-21 10:53:52 +08:00
|
|
|
}
|
|
|
|
|
2014-12-08 21:53:14 +08:00
|
|
|
if( maxscale < 1.0F ) continue;
|
|
|
|
|
2016-04-21 20:47:13 +08:00
|
|
|
scale = ((float)maxscale - 1.0F) * rand() / RAND_MAX + 1.0F;
|
2016-04-21 10:53:52 +08:00
|
|
|
|
2014-12-08 21:53:14 +08:00
|
|
|
width = (int) (scale * winwidth);
|
|
|
|
height = (int) (scale * winheight);
|
|
|
|
x = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.cols - width));
|
|
|
|
y = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.rows - height));
|
|
|
|
|
|
|
|
if( invert == CV_RANDOM_INVERT )
|
|
|
|
{
|
|
|
|
inverse = (rand() > (RAND_MAX/2));
|
|
|
|
}
|
2017-12-08 17:23:44 +08:00
|
|
|
icvPlaceDistortedSample( cvbgreader->src(Rect(x, y, width, height)), inverse, maxintensitydev,
|
2014-12-08 21:53:14 +08:00
|
|
|
maxxangle, maxyangle, maxzangle,
|
|
|
|
1, 0.0, 0.0, &data );
|
|
|
|
|
|
|
|
|
|
|
|
sprintf( filename, "%04d_%04d_%04d_%04d_%04d.jpg",
|
|
|
|
(i + 1), x, y, width, height );
|
|
|
|
|
|
|
|
if( info )
|
|
|
|
{
|
|
|
|
fprintf( info, "%s %d %d %d %d %d\n",
|
|
|
|
filename, 1, x, y, width, height );
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
imwrite( fullname, cvbgreader->src );
|
2014-12-08 21:53:14 +08:00
|
|
|
if( showsamples )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
imshow( "Image", cvbgreader->src );
|
|
|
|
if( (waitKey( 0 ) & 0xFF) == 27 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
showsamples = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( info ) fclose( info );
|
|
|
|
icvDestroyBackgroundReaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int cvCreateTrainingSamplesFromInfo( const char* infoname, const char* vecfilename,
|
|
|
|
int num,
|
|
|
|
int showsamples,
|
|
|
|
int winwidth, int winheight )
|
|
|
|
{
|
|
|
|
char fullname[PATH_MAX];
|
|
|
|
char* filename;
|
|
|
|
|
|
|
|
FILE* info;
|
|
|
|
FILE* vec;
|
|
|
|
int line;
|
|
|
|
int error;
|
|
|
|
int i;
|
|
|
|
int x, y, width, height;
|
|
|
|
int total;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( infoname != NULL );
|
|
|
|
CV_Assert( vecfilename != NULL );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
total = 0;
|
|
|
|
if( !icvMkDir( vecfilename ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
#if CV_VERBOSE
|
|
|
|
fprintf( stderr, "Unable to create directory hierarchy: %s\n", vecfilename );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = fopen( infoname, "r" );
|
|
|
|
if( info == NULL )
|
|
|
|
{
|
|
|
|
|
|
|
|
#if CV_VERBOSE
|
|
|
|
fprintf( stderr, "Unable to open file: %s\n", infoname );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec = fopen( vecfilename, "wb" );
|
|
|
|
if( vec == NULL )
|
|
|
|
{
|
|
|
|
|
|
|
|
#if CV_VERBOSE
|
|
|
|
fprintf( stderr, "Unable to open file: %s\n", vecfilename );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
|
|
|
fclose( info );
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
icvWriteVecHeader( vec, num, winwidth, winheight );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
if( showsamples )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
namedWindow( "Sample", WINDOW_AUTOSIZE );
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
strcpy( fullname, infoname );
|
|
|
|
filename = strrchr( fullname, '\\' );
|
|
|
|
if( filename == NULL )
|
|
|
|
{
|
|
|
|
filename = strrchr( fullname, '/' );
|
|
|
|
}
|
|
|
|
if( filename == NULL )
|
|
|
|
{
|
|
|
|
filename = fullname;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( line = 1, error = 0, total = 0; total < num ;line++ )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat src;
|
2014-12-08 21:53:14 +08:00
|
|
|
int count;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
if(fscanf(info, "%s %d", filename, &count) == 2)
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
src = imread( fullname, IMREAD_GRAYSCALE );
|
|
|
|
if(src.empty())
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
#if CV_VERBOSE
|
|
|
|
fprintf( stderr, "Unable to open image: %s\n", fullname );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for( i = 0; (i < count) && (total < num); i++, total++ )
|
|
|
|
{
|
|
|
|
error = ( fscanf( info, "%d %d %d %d", &x, &y, &width, &height ) != 4 );
|
|
|
|
if( error ) break;
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat sample;
|
|
|
|
resize( src(Rect(x, y, width, height)), sample, Size(winwidth, winheight), 0, 0,
|
|
|
|
width >= winwidth && height >= winheight ? INTER_AREA : INTER_LINEAR );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
|
|
|
if( showsamples )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
imshow( "Sample", sample );
|
|
|
|
if( (waitKey( 0 ) & 0xFF) == 27 )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
showsamples = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
icvWriteVecSample( vec, sample );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
|
|
|
|
#if CV_VERBOSE
|
|
|
|
fprintf( stderr, "%s(%d) : parse error", infoname, line );
|
|
|
|
#endif /* CV_VERBOSE */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( vec );
|
|
|
|
fclose( info );
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct CvVecFile
|
|
|
|
{
|
|
|
|
FILE* input;
|
|
|
|
int count;
|
|
|
|
int vecsize;
|
|
|
|
int last;
|
|
|
|
} CvVecFile;
|
|
|
|
|
|
|
|
static
|
2017-12-08 17:23:44 +08:00
|
|
|
int icvGetTraininDataFromVec( Mat& img, CvVecFile& userdata )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
AutoBuffer<short> vector(userdata.vecsize);
|
2014-12-08 21:53:14 +08:00
|
|
|
uchar tmp = 0;
|
|
|
|
int r = 0;
|
|
|
|
int c = 0;
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
CV_Assert( img.rows * img.cols == userdata.vecsize );
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
size_t elements_read = fread( &tmp, sizeof( tmp ), 1, userdata.input );
|
2014-12-08 21:53:14 +08:00
|
|
|
CV_Assert(elements_read == 1);
|
2017-12-08 17:23:44 +08:00
|
|
|
elements_read = fread( vector, sizeof( short ), userdata.vecsize, userdata.input );
|
|
|
|
CV_Assert(elements_read == (size_t)userdata.vecsize);
|
2014-12-08 21:53:14 +08:00
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
if( feof( userdata.input ) || userdata.last++ >= userdata.count )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:23:44 +08:00
|
|
|
for( r = 0; r < img.rows; r++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
for( c = 0; c < img.cols; c++ )
|
2014-12-08 21:53:14 +08:00
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
img.at<uchar>(r, c) = (uchar) ( vector[r * img.cols + c] );
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
void cvShowVecSamples( const char* filename, int winwidth, int winheight,
|
|
|
|
double scale )
|
|
|
|
{
|
|
|
|
CvVecFile file;
|
|
|
|
short tmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
tmp = 0;
|
|
|
|
file.input = fopen( filename, "rb" );
|
|
|
|
|
|
|
|
if( file.input != NULL )
|
|
|
|
{
|
|
|
|
size_t elements_read1 = fread( &file.count, sizeof( file.count ), 1, file.input );
|
|
|
|
size_t elements_read2 = fread( &file.vecsize, sizeof( file.vecsize ), 1, file.input );
|
|
|
|
size_t elements_read3 = fread( &tmp, sizeof( tmp ), 1, file.input );
|
|
|
|
size_t elements_read4 = fread( &tmp, sizeof( tmp ), 1, file.input );
|
|
|
|
CV_Assert(elements_read1 == 1 && elements_read2 == 1 && elements_read3 == 1 && elements_read4 == 1);
|
|
|
|
|
|
|
|
if( file.vecsize != winwidth * winheight )
|
|
|
|
{
|
|
|
|
int guessed_w = 0;
|
|
|
|
int guessed_h = 0;
|
|
|
|
|
|
|
|
fprintf( stderr, "Warning: specified sample width=%d and height=%d "
|
|
|
|
"does not correspond to .vec file vector size=%d.\n",
|
|
|
|
winwidth, winheight, file.vecsize );
|
|
|
|
if( file.vecsize > 0 )
|
|
|
|
{
|
|
|
|
guessed_w = cvFloor( sqrt( (float) file.vecsize ) );
|
|
|
|
if( guessed_w > 0 )
|
|
|
|
{
|
|
|
|
guessed_h = file.vecsize / guessed_w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( guessed_w <= 0 || guessed_h <= 0 || guessed_w * guessed_h != file.vecsize)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Error: failed to guess sample width and height\n" );
|
|
|
|
fclose( file.input );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
winwidth = guessed_w;
|
|
|
|
winheight = guessed_h;
|
|
|
|
fprintf( stderr, "Guessed width=%d, guessed height=%d\n",
|
|
|
|
winwidth, winheight );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !feof( file.input ) && scale > 0 )
|
|
|
|
{
|
|
|
|
file.last = 0;
|
2017-12-08 17:23:44 +08:00
|
|
|
namedWindow( "Sample", WINDOW_AUTOSIZE );
|
2014-12-08 21:53:14 +08:00
|
|
|
for( i = 0; i < file.count; i++ )
|
|
|
|
{
|
2017-12-08 17:23:44 +08:00
|
|
|
Mat sample(winheight, winwidth, CV_8UC1);
|
|
|
|
icvGetTraininDataFromVec( sample, file );
|
|
|
|
if( scale != 1.0 )
|
|
|
|
resize( sample, sample,
|
|
|
|
Size(MAX(1, cvCeil(scale * winheight)), MAX(1, cvCeil(scale * winwidth))), 0, 0, INTER_LINEAR);
|
|
|
|
imshow( "Sample", sample );
|
|
|
|
if( (waitKey( 0 ) & 0xFF) == 27 ) break;
|
2014-12-08 21:53:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose( file.input );
|
|
|
|
}
|
2016-08-12 21:11:30 +08:00
|
|
|
}
|