Created
May 24, 2012 07:53
-
-
Save mokemokechicken/2780099 to your computer and use it in GitHub Desktop.
patch of opencv_createsamples for "-maxtrans" option which make pos images randomly transparent when overlay on bgimage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -x 'performance.cpp*' -aru haartraining.orig/_cvhaartraining.h haartraining/_cvhaartraining.h | |
--- haartraining.orig/_cvhaartraining.h 2012-05-24 16:46:30.000000000 +0900 | |
+++ haartraining/_cvhaartraining.h 2012-05-24 16:24:54.000000000 +0900 | |
@@ -320,6 +320,14 @@ | |
double maxxangle, double maxyangle, double maxzangle, | |
int inscribe, double maxshiftf, double maxscalef, | |
CvSampleDistortionData* data ); | |
+ | |
+void icvPlaceDistortedSample( CvArr* background, | |
+ int inverse, int maxintensitydev, | |
+ double maxxangle, double maxyangle, double maxzangle, | |
+ int inscribe, double maxshiftf, double maxscalef, | |
+ CvSampleDistortionData* data, | |
+ float maxTransparent); | |
+ | |
void icvEndSampleDistortion( CvSampleDistortionData* data ); | |
int icvStartSampleDistortion( const char* imgfilename, int bgcolor, int bgthreshold, | |
diff -x 'performance.cpp*' -aru haartraining.orig/createsamples.cpp haartraining/createsamples.cpp | |
--- haartraining.orig/createsamples.cpp 2012-05-24 16:46:30.000000000 +0900 | |
+++ haartraining/createsamples.cpp 2012-05-24 16:37:56.000000000 +0900 | |
@@ -76,6 +76,7 @@ | |
double scale = 4.0; | |
int width = 24; | |
int height = 24; | |
+ float maxTransparent = 0; | |
srand((unsigned int)time(0)); | |
@@ -92,9 +93,11 @@ | |
" [-maxyangle <max_y_rotation_angle = %f>]\n" | |
" [-maxzangle <max_z_rotation_angle = %f>]\n" | |
" [-show [<scale = %f>]]\n" | |
- " [-w <sample_width = %d>]\n [-h <sample_height = %d>]\n", | |
+ " [-w <sample_width = %d>]\n [-h <sample_height = %d>]\n" | |
+ " [-maxtrans (0..1) <max transparent = %f>]\n" | |
+ , | |
argv[0], num, bgcolor, bgthreshold, maxintensitydev, | |
- maxxangle, maxyangle, maxzangle, scale, width, height ); | |
+ maxxangle, maxyangle, maxzangle, scale, width, height, maxTransparent); | |
return 0; | |
} | |
@@ -153,6 +156,10 @@ | |
{ | |
maxzangle = atof( argv[++i] ); | |
} | |
+ else if( !strcmp( argv[i], "-maxtrans" ) ) | |
+ { | |
+ maxTransparent = atof( argv[++i] ); | |
+ } | |
else if( !strcmp( argv[i], "-show" ) ) | |
{ | |
showsamples = 1; | |
@@ -187,6 +194,7 @@ | |
printf( "Max x angle: %g\n", maxxangle ); | |
printf( "Max y angle: %g\n", maxyangle ); | |
printf( "Max z angle: %g\n", maxzangle ); | |
+ printf( "Max Transparent: %f\n", maxTransparent ); | |
printf( "Show samples: %s\n", (showsamples) ? "TRUE" : "FALSE" ); | |
if( showsamples ) | |
{ | |
@@ -203,7 +211,7 @@ | |
cvCreateTrainingSamples( vecname, imagename, bgcolor, bgthreshold, bgfilename, | |
num, invert, maxintensitydev, | |
maxxangle, maxyangle, maxzangle, | |
- showsamples, width, height ); | |
+ showsamples, width, height, maxTransparent ); | |
printf( "Done\n" ); | |
} | |
@@ -213,7 +221,7 @@ | |
cvCreateTestSamples( infoname, imagename, bgcolor, bgthreshold, bgfilename, num, | |
invert, maxintensitydev, | |
- maxxangle, maxyangle, maxzangle, showsamples, width, height ); | |
+ maxxangle, maxyangle, maxzangle, showsamples, width, height, maxTransparent ); | |
printf( "Done\n" ); | |
} | |
diff -x 'performance.cpp*' -aru haartraining.orig/cvhaartraining.cpp haartraining/cvhaartraining.cpp | |
--- haartraining.orig/cvhaartraining.cpp 2012-05-24 16:46:30.000000000 +0900 | |
+++ haartraining/cvhaartraining.cpp 2012-05-24 16:36:34.000000000 +0900 | |
@@ -2841,14 +2841,14 @@ | |
} | |
- | |
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 ) | |
+ int winwidth, int winheight, | |
+ float maxTransparent) | |
{ | |
CvSampleDistortionData data; | |
@@ -2907,7 +2907,7 @@ | |
0 /* nonzero means placing image without cut offs */, | |
0.0 /* nozero adds random shifting */, | |
0.0 /* nozero adds random scaling */, | |
- &data ); | |
+ &data, maxTransparent ); | |
if( showsamples ) | |
{ | |
@@ -2943,14 +2943,14 @@ | |
#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, | |
- int winwidth, int winheight ) | |
+ int winwidth, int winheight, | |
+ float maxTransparent) | |
{ | |
CvSampleDistortionData data; | |
@@ -3026,7 +3026,7 @@ | |
} | |
icvPlaceDistortedSample( &win, inverse, maxintensitydev, | |
maxxangle, maxyangle, maxzangle, | |
- 1, 0.0, 0.0, &data ); | |
+ 1, 0.0, 0.0, &data, maxTransparent ); | |
sprintf( filename, "%04d_%04d_%04d_%04d_%04d.jpg", | |
diff -x 'performance.cpp*' -aru haartraining.orig/cvhaartraining.h haartraining/cvhaartraining.h | |
--- haartraining.orig/cvhaartraining.h 2012-05-24 16:46:30.000000000 +0900 | |
+++ haartraining/cvhaartraining.h 2012-05-24 16:36:08.000000000 +0900 | |
@@ -82,7 +82,8 @@ | |
double maxyangle = 1.1, | |
double maxzangle = 0.5, | |
int showsamples = 0, | |
- int winwidth = 24, int winheight = 24 ); | |
+ int winwidth = 24, int winheight = 24, | |
+ float maxTransparent = 0); | |
void cvCreateTestSamples( const char* infoname, | |
const char* imgfilename, int bgcolor, int bgthreshold, | |
@@ -90,7 +91,8 @@ | |
int invert, int maxintensitydev, | |
double maxxangle, double maxyangle, double maxzangle, | |
int showsamples, | |
- int winwidth, int winheight ); | |
+ int winwidth, int winheight, | |
+ float maxTransparent = 0); | |
/* | |
* cvCreateTrainingSamplesFromInfo | |
diff -x 'performance.cpp*' -aru haartraining.orig/cvsamples.cpp haartraining/cvsamples.cpp | |
--- haartraining.orig/cvsamples.cpp 2012-05-24 16:46:30.000000000 +0900 | |
+++ haartraining/cvsamples.cpp 2012-05-24 16:20:04.000000000 +0900 | |
@@ -549,11 +549,13 @@ | |
return 0; | |
} | |
+ | |
void icvPlaceDistortedSample( CvArr* background, | |
int inverse, int maxintensitydev, | |
double maxxangle, double maxyangle, double maxzangle, | |
int inscribe, double maxshiftf, double maxscalef, | |
- CvSampleDistortionData* data ) | |
+ CvSampleDistortionData* data, | |
+ float maxTransparent) | |
{ | |
double quad[4][2]; | |
int r, c; | |
@@ -645,15 +647,17 @@ | |
{ | |
for( c = 0; c < img->width; c++ ) | |
{ | |
+ float noise = 1 - (maxTransparent * rand()/RAND_MAX); | |
pimg = (uchar*) img->imageData + r * img->widthStep + c; | |
pbg = (uchar*) bgimg->data.ptr + r * bgimg->step + c; | |
palpha = (uchar*) maskimg->imageData + r * maskimg->widthStep + c; | |
chartmp = (uchar) MAX( 0, MIN( 255, forecolordev + (*pimg) ) ); | |
+ float alphatmp = MAX(0, (*palpha) * noise); | |
if( inverse ) | |
{ | |
chartmp ^= 0xFF; | |
} | |
- *pbg = (uchar) (( chartmp*(*palpha )+(255 - (*palpha) )*(*pbg) ) / 255); | |
+ *pbg = (uchar) (( chartmp*(alphatmp )+(255 - (alphatmp) )*(*pbg) ) / 255); | |
} | |
} | |
@@ -661,6 +665,23 @@ | |
cvReleaseImage( &maskimg ); | |
} | |
+ | |
+void icvPlaceDistortedSample( CvArr* background, | |
+ int inverse, int maxintensitydev, | |
+ double maxxangle, double maxyangle, double maxzangle, | |
+ int inscribe, double maxshiftf, double maxscalef, | |
+ CvSampleDistortionData* data) | |
+{ | |
+ icvPlaceDistortedSample(background, | |
+ inverse, maxintensitydev, | |
+ maxxangle, maxyangle, maxzangle, | |
+ inscribe, maxshiftf, maxscalef, | |
+ data, | |
+ 0); | |
+} | |
+ | |
+ | |
+ | |
void icvEndSampleDistortion( CvSampleDistortionData* data ) | |
{ | |
if( data->src ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cd OpenCV-2.4.0/apps/haartraining
patch -p1 < haartraining.20120524.diff