Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 0ad6092

Browse files
committed
More files and directories
1 parent afd035e commit 0ad6092

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

classifier/.gitkeep

Whitespace-only changes.

samples/.gitkeep

Whitespace-only changes.

src/mergevec.cpp

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#include <cv.h>
2+
#include <highgui.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <math.h>
6+
7+
#include <cvhaartraining.h>
8+
#include <_cvhaartraining.h> // Load CvVecFile
9+
// Write a vec header into the vec file (located at cvsamples.cpp)
10+
void icvWriteVecHeader( FILE* file, int count, int width, int height );
11+
// Write a sample image into file in the vec format (located at cvsamples.cpp)
12+
void icvWriteVecSample( FILE* file, CvArr* sample );
13+
// Append the body of the input vec to the ouput vec
14+
void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight );
15+
// Merge vec files
16+
void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height );
17+
18+
// Append the body of the input vec to the ouput vec
19+
void icvAppendVec( CvVecFile &in, CvVecFile &out, int *showsamples, int winwidth, int winheight )
20+
{
21+
CvMat* sample;
22+
23+
if( *showsamples )
24+
{
25+
cvNamedWindow( "Sample", CV_WINDOW_AUTOSIZE );
26+
}
27+
if( !feof( in.input ) )
28+
{
29+
in.last = 0;
30+
in.vector = (short*) cvAlloc( sizeof( *in.vector ) * in.vecsize );
31+
if ( *showsamples )
32+
{
33+
if ( in.vecsize != winheight * winwidth )
34+
{
35+
fprintf( stderr, "ERROR: -show: the size of images inside of vec files does not match with %d x %d, but %d\n", winheight, winwidth, in.vecsize );
36+
exit(1);
37+
}
38+
sample = cvCreateMat( winheight, winwidth, CV_8UC1 );
39+
}
40+
else
41+
{
42+
sample = cvCreateMat( in.vecsize, 1, CV_8UC1 );
43+
}
44+
for( int i = 0; i < in.count; i++ )
45+
{
46+
icvGetHaarTraininDataFromVecCallback( sample, &in );
47+
icvWriteVecSample ( out.input, sample );
48+
if( *showsamples )
49+
{
50+
cvShowImage( "Sample", sample );
51+
if( cvWaitKey( 0 ) == 27 )
52+
{
53+
*showsamples = 0;
54+
}
55+
}
56+
}
57+
cvReleaseMat( &sample );
58+
cvFree( (void**) &in.vector );
59+
}
60+
}
61+
62+
void icvMergeVecs( char* infoname, const char* outvecname, int showsamples, int width, int height )
63+
{
64+
char onevecname[PATH_MAX];
65+
int i = 0;
66+
int filenum = 0;
67+
short tmp;
68+
FILE *info;
69+
CvVecFile outvec;
70+
CvVecFile invec;
71+
int prev_vecsize;
72+
73+
// fopen input and output file
74+
info = fopen( infoname, "r" );
75+
if ( info == NULL )
76+
{
77+
fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", infoname );
78+
exit(1);
79+
}
80+
outvec.input = fopen( outvecname, "wb" );
81+
if ( outvec.input == NULL )
82+
{
83+
fprintf( stderr, "ERROR: Output file %s is not writable.\n", outvecname );
84+
exit(1);
85+
}
86+
87+
// Header
88+
rewind( info );
89+
outvec.count = 0;
90+
for ( filenum = 0; ; filenum++ )
91+
{
92+
if ( fscanf( info, "%s", onevecname ) == EOF )
93+
{
94+
break;
95+
}
96+
invec.input = fopen( onevecname, "rb" );
97+
if ( invec.input == NULL )
98+
{
99+
fprintf( stderr, "ERROR: Input file %s does not exist or not readable.\n", onevecname );
100+
exit(1);
101+
}
102+
fread( &invec.count, sizeof( invec.count ) , 1, invec.input );
103+
fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );
104+
fread( &tmp, sizeof( tmp ), 1, invec.input );
105+
fread( &tmp, sizeof( tmp ), 1, invec.input );
106+
107+
outvec.count += invec.count;
108+
if( i > 0 && invec.vecsize != prev_vecsize )
109+
{
110+
fprintf( stderr, "ERROR: The size of images in %s(%d) is different with the previous vec file(%d).\n", onevecname, invec.vecsize, prev_vecsize );
111+
exit(1);
112+
}
113+
prev_vecsize = invec.vecsize;
114+
fclose( invec.input );
115+
}
116+
outvec.vecsize = invec.vecsize;
117+
icvWriteVecHeader( outvec.input, outvec.count, outvec.vecsize, 1);
118+
119+
// Contents
120+
rewind( info );
121+
outvec.count = 0;
122+
for ( i = 0; i < filenum ; i++ )
123+
{
124+
if (fscanf( info, "%s", onevecname ) == EOF) {
125+
break;
126+
}
127+
invec.input = fopen( onevecname, "rb" );
128+
fread( &invec.count, sizeof( invec.count ) , 1, invec.input );
129+
fread( &invec.vecsize, sizeof( invec.vecsize ), 1, invec.input );
130+
fread( &tmp, sizeof( tmp ), 1, invec.input );
131+
fread( &tmp, sizeof( tmp ), 1, invec.input );
132+
133+
icvAppendVec( invec, outvec, &showsamples, width, height );
134+
fclose( invec.input );
135+
}
136+
fclose( outvec.input );
137+
}
138+
139+
int main( int argc, char **argv )
140+
{
141+
int i;
142+
char *infoname = NULL;
143+
char *outvecname = NULL;
144+
int showsamples = 0;
145+
int width = 24;
146+
int height = 24;
147+
148+
if( argc == 1 )
149+
{
150+
printf( "Usage: %s\n <collection_file_of_vecs>\n"
151+
" <output_vec_filename>\n"
152+
" [-show] [-w <sample_width = %d>] [-h <sample_height = %d>]\n",
153+
argv[0], width, height );
154+
return 0;
155+
}
156+
for( i = 1; i < argc; ++i )
157+
{
158+
if( !strcmp( argv[i], "-show" ) )
159+
{
160+
showsamples = 1;
161+
// width = atoi( argv[++i] ); // obsolete -show width height
162+
// height = atoi( argv[++i] );
163+
}
164+
else if( !strcmp( argv[i], "-w" ) )
165+
{
166+
width = atoi( argv[++i] );
167+
}
168+
else if( !strcmp( argv[i], "-h" ) )
169+
{
170+
height = atoi( argv[++i] );
171+
}
172+
else if( argv[i][0] == '-' )
173+
{
174+
fprintf( stderr, "ERROR: The option %s does not exist. n", argv[i] );
175+
exit(1);
176+
}
177+
else if( infoname == NULL )
178+
{
179+
infoname = argv[i];
180+
}
181+
else if( outvecname == NULL )
182+
{
183+
outvecname = argv[i];
184+
}
185+
}
186+
if( infoname == NULL )
187+
{
188+
fprintf( stderr, "ERROR: No input file\n" );
189+
exit(1);
190+
}
191+
if( outvecname == NULL )
192+
{
193+
fprintf( stderr, "ERROR: No output file\n" );
194+
exit(1);
195+
}
196+
icvMergeVecs( infoname, outvecname, showsamples, width, height );
197+
return 0;
198+
}

0 commit comments

Comments
 (0)