aboutsummaryrefslogtreecommitdiff
path: root/pix_opencv_warpperspective.cc
blob: e53a1dcc034b6ba12d0ea69962af4a8267fb366c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
//    Copyright (c) 1997-2000 Mark Danks.
//    Copyright (c) Günther Geiger.
//    Copyright (c) 2001-2002 IOhannes m zmoelnig. forum::für::umläute. IEM
//    Copyright (c) 2002 James Tittle & Chris Clepper
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
//
// pix_opencv_warpperspective by Antoine Villeret - 2011/13



#include "pix_opencv_warpperspective.h"
#include <stdio.h>

CPPEXTERN_NEW(pix_opencv_warpperspective)

/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////

pix_opencv_warpperspective :: pix_opencv_warpperspective()
{ 
  //inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("minarea"));
  //inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("maxarea"));
  m_dataout = outlet_new(this->x_obj, 0);
  //m_countout = outlet_new(this->x_obj, 0);
  comp_xsize  = 320;
  comp_ysize  = 240;
  gray = NULL;
  tmp = NULL;
  rgb = NULL;
  mapMatrix = cvCreateMat(3,3,CV_32FC1);
  srcMatrix = cvCreateMat(4,2,CV_32FC1);
  dstMatrix = cvCreateMat(4,2,CV_32FC1);
  
  cvSet(mapMatrix, cvScalar(0)); // set all to 0.
  CV_MAT_ELEM( *mapMatrix, float, 0, 0 ) = 1.; // then make identity
  CV_MAT_ELEM( *mapMatrix, float, 1, 1 ) = 1.;
  
  // initialize srcMatrix & dstMatrix to the same...
  CV_MAT_ELEM( *srcMatrix, float, 0, 0 ) = 0.;
  CV_MAT_ELEM( *srcMatrix, float, 0, 1 ) = 0.;
  CV_MAT_ELEM( *srcMatrix, float, 1, 0 ) = 1.;
  CV_MAT_ELEM( *srcMatrix, float, 1, 1 ) = 0.;
  CV_MAT_ELEM( *srcMatrix, float, 2, 0 ) = 1.;
  CV_MAT_ELEM( *srcMatrix, float, 2, 1 ) = 1.;
  CV_MAT_ELEM( *srcMatrix, float, 3, 0 ) = 0.;
  CV_MAT_ELEM( *srcMatrix, float, 3, 1 ) = 1.;
  
  CV_MAT_ELEM( *dstMatrix, float, 0, 0 ) = 0.;
  CV_MAT_ELEM( *dstMatrix, float, 0, 1 ) = 0.;
  CV_MAT_ELEM( *dstMatrix, float, 1, 0 ) = 1.;
  CV_MAT_ELEM( *dstMatrix, float, 1, 1 ) = 0.;
  CV_MAT_ELEM( *dstMatrix, float, 2, 0 ) = 1.;
  CV_MAT_ELEM( *dstMatrix, float, 2, 1 ) = 1.;
  CV_MAT_ELEM( *dstMatrix, float, 3, 0 ) = 0.;
  CV_MAT_ELEM( *dstMatrix, float, 3, 1 ) = 1.;
  
  mapMatrixList=new t_atom[9];
    
  flags =  CV_WARP_FILL_OUTLIERS; // TODO add a set method
  findmethod = 0; // TODO add a set method
  
  post("pix_opencv_warpperspective by Antoine Villeret");
  post("build on %s at %s", __DATE__, __TIME__);

}

/////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_opencv_warpperspective :: ~pix_opencv_warpperspective()
{ 
    	//Destroy cv_images to clean memory
    	//if (gray) cvReleaseImage(&gray);  // TODO free image header but not data because it points to Gem image...
    	if (rgb) cvReleaseImage(&rgb);
    	//if (tmp) cvReleaseImage(&tmp); // the same
    	if (mapMatrix) cvReleaseMat(&mapMatrix);
     	if (srcMatrix) cvReleaseMat(&srcMatrix);
    	if (dstMatrix) cvReleaseMat(&dstMatrix);
}

/////////////////////////////////////////////////////////
// processImage
//
/////////////////////////////////////////////////////////
void pix_opencv_warpperspective :: processRGBAImage(imageStruct &image)
{
  	if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgb)) 
	{
		this->comp_xsize = image.xsize;
		this->comp_ysize = image.ysize;

		//Destroy cv_images to clean memory
		if(gray) cvReleaseImage(&gray);
		if(rgb) cvReleaseImage(&rgb);
		if(tmp) cvReleaseImage(&tmp);

		// Create images with new sizes
		rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
		tmp = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
	}
	
    // no need to copy a lot of memory, just point to it...
    rgb->imageData = (char*) image.data;
   	cvWarpPerspective(rgb, tmp, mapMatrix, flags, cvScalar(0));
	memcpy(image.data, tmp->imageData, image.xsize*image.ysize*image.csize);	
}

void pix_opencv_warpperspective :: processRGBImage(imageStruct &image)
{
	// TODO
    error("cant't support RGB image for now");
}

void pix_opencv_warpperspective :: processYUVImage(imageStruct &image)
{
	// TODO
	error( "pix_opencv_warpperspective : yuv format not supported" );
}
    	
void pix_opencv_warpperspective :: processGrayImage(imageStruct &image)
{ 
	if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!gray)) 
	{
		this->comp_xsize = image.xsize;
		this->comp_ysize = image.ysize;

		//Destroy cv_images to clean memory
		if(gray) cvReleaseImage(&gray);
		if(rgb) cvReleaseImage(&rgb);
		if(tmp) cvReleaseImage(&tmp);

		// Create images with new sizes
		gray = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 1);
		tmp = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 1);
	}

    // no need to copy a lot of memory, just point to it...
    gray->imageData = (char*) image.data;
    
   	cvWarpPerspective(gray, tmp, mapMatrix, flags, cvScalar(0));
	memcpy(image.data, tmp->imageData, image.xsize*image.ysize);
	
}

/////////////////////////////////////////////////////////
// floatThreshMess
//
/////////////////////////////////////////////////////////
void pix_opencv_warpperspective :: mapMatrixMess (int argc, t_atom *argv)
{
	post("set mapMatrix");
	int i;
	if (argc != 9) { 
			error("map matrix should be 3x3"); 
			return; 
		}
	for ( i = 0; i < 9 ; i++) {
		if (argv[i].a_type != A_FLOAT) { 
			error("map matrix should be float");
			return; 
		}
	}
		
	// fillin the mapMatrix
	CV_MAT_ELEM( *mapMatrix, float, 0, 0 ) = argv[0].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 1, 0 ) = argv[1].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 2, 0 ) = argv[2].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 0, 1 ) = argv[3].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 1, 1 ) = argv[4].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 2, 1 ) = argv[5].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 0, 2 ) = argv[6].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 1, 2 ) = argv[7].a_w.w_float;
	CV_MAT_ELEM( *mapMatrix, float, 2, 2 ) = argv[8].a_w.w_float;
	
	/*
    int j;
	printf("---mapMatrix---\n");
	for ( j = 0 ; j < 3 ; j++ ){
		for( i=0 ; i<3 ; i++){
			printf("%.2f,\t", CV_MAT_ELEM( *mapMatrix, float, i, j));
		}
		printf("\n");
	}
	*/
}

void pix_opencv_warpperspective :: srcMatrixMess (int argc, t_atom *argv)
{
	int i;
	if ( argc % 2 ) { 
		error("src is should be a list of couple x/y values"); 
		return; 
	}
	if ( argc != dstMatrix->rows * dstMatrix->cols )
	{ 
		error("src matrix should have the same size as dst matrix (which is %d x %d)", dstMatrix->cols, dstMatrix->rows);
		return;
	}
	for ( i = 0; i < argc ; i++) {		
		if (argv[i].a_type != A_FLOAT) { 
				error("src matrix should be float"); 
				return; 
			}
	}

	// fillin the srcMatrix
	for ( i = 0 ; i < dstMatrix->rows ; i++ )
	{
		CV_MAT_ELEM( *srcMatrix, float, i, 0 ) = argv[i*2].a_w.w_float;
		CV_MAT_ELEM( *srcMatrix, float, i, 1 ) = argv[i*2+1].a_w.w_float; // does it work ?
	}
	findhomography();
}

void pix_opencv_warpperspective :: dstMatrixMess (int argc, t_atom *argv)
{
	int i;
	if ( argc % 2 ){ 
			error("dstMatrix is should be a list of x/y pairs"); 
			return; 
		}
	for ( i = 0; i < argc ; i++) {
		if (argv[i].a_type != A_FLOAT) {
				error("dstMatrix should be float"); 
				return;
			}
	}
	if ( dstMatrix->rows != argc/2 ) {
		// delete and recreate matrix if needed
		cvReleaseMat(&dstMatrix);
		cvReleaseMat(&srcMatrix);
		dstMatrix = cvCreateMat(argc/2,2,CV_32FC1);
		srcMatrix = cvCreateMat(argc/2,2,CV_32FC1);
		cvSet(srcMatrix, cvScalar(0)); // set all to 0.
	}
	// fillin the dstMatrix
	for ( i = 0 ; i < dstMatrix->rows ; i++ )
	{
		CV_MAT_ELEM( *dstMatrix, float, i, 0 ) = argv[i*2].a_w.w_float;
		CV_MAT_ELEM( *dstMatrix, float, i, 1 ) = argv[i*2+1].a_w.w_float; // does it work ?
	}
	findhomography();
}

void pix_opencv_warpperspective :: invertMess( int argc, t_atom *argv ){
    if ( argc == 0 ){
        error("invert need one argument (0|1)");
    } else if ( argv[0].a_type != A_FLOAT ){
        error("invert need one float argument (0|1)");   
    } else {
        int invert = argv[0].a_w.w_float;
        if (invert > 0){
            flags |= cv::WARP_INVERSE_MAP;
        } else {
            flags ^= cv::WARP_INVERSE_MAP;
        }
    }
}

void pix_opencv_warpperspective :: findhomography( )
{
	int i,j;
		if ( srcMatrix->cols != dstMatrix->cols || srcMatrix->rows != dstMatrix->rows ) {
			error("srcMatrix and dstMatrix should have the same size to compute homography !");
			return;
		}
			
		cvFindHomography(srcMatrix, dstMatrix, mapMatrix, findmethod, 0, NULL);
		
		for ( j = 0 ; j < 3 ; j++ ){
			for( i=0 ; i<3 ; i++){
				SETFLOAT(&mapMatrixList[i+j*3], CV_MAT_ELEM( *mapMatrix, float, i, j));
			}
		}
		
		// send out mapMatrix
		outlet_list( m_dataout, 0, 9, mapMatrixList);
}
/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_opencv_warpperspective :: obj_setupCallback(t_class *classPtr)
{
	// TODO add method for message "flags"
	// TODO treat list messages in a PD way ?
	  class_addmethod(classPtr, (t_method)&pix_opencv_warpperspective::mapMatrixMessCallback,
  		  gensym("mapMatrix"), A_GIMME, A_NULL);	 
	  class_addmethod(classPtr, (t_method)&pix_opencv_warpperspective::srcMatrixMessCallback,
  		  gensym("srcMatrix"), A_GIMME, A_NULL);	 
	  class_addmethod(classPtr, (t_method)&pix_opencv_warpperspective::dstMatrixMessCallback,
  		  gensym("dstMatrix"), A_GIMME, A_NULL);
	  class_addmethod(classPtr, (t_method)&pix_opencv_warpperspective::invertMessCallback,
  		  gensym("invert"), A_GIMME, A_NULL);
}

void pix_opencv_warpperspective :: mapMatrixMessCallback(void *data, t_symbol *s, int argc, t_atom *argv)
{
		GetMyClass(data)->mapMatrixMess(argc, argv);
}

void pix_opencv_warpperspective :: srcMatrixMessCallback(void *data, t_symbol *s, int argc, t_atom *argv)
{
		GetMyClass(data)->srcMatrixMess(argc, argv);
}

void pix_opencv_warpperspective :: dstMatrixMessCallback(void *data, t_symbol *s, int argc, t_atom *argv)
{
		GetMyClass(data)->dstMatrixMess(argc, argv);
}

void pix_opencv_warpperspective :: invertMessCallback(void *data, t_symbol *s, int argc, t_atom *argv)
{
		GetMyClass(data)->invertMess(argc, argv);
}