aboutsummaryrefslogtreecommitdiff
path: root/pix_opencv_matchshape.cc
blob: 75dc9102ef1f83df5d43c03e67a19717975a422f (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
////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////
// based on code written by Lluis Gomez i Bigorda ( lluisgomez _at_ hangar _dot_ org ) (pix_opencv)
// Template for pix_opencv class

#include "pix_opencv_matchshape.h"
#include <stdio.h>
#include <RTE/MessageCallbacks.h>

using namespace std;


CPPEXTERN_NEW(pix_opencv_matchshape)

/////////////////////////////////////////////////////////
//
// pix_opencv_matchshape
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
pix_opencv_matchshape :: pix_opencv_matchshape() : m_threshold(2), m_method(CV_CONTOURS_MATCH_I1)
{ 
	m_dataout = outlet_new(this->x_obj, 0);
	m_template_vec_vec.clear();
	m_template_vec_mat.clear();
}

/////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_opencv_matchshape :: ~pix_opencv_matchshape()
{ 
}

/////////////////////////////////////////////////////////
// processImage
//
/////////////////////////////////////////////////////////
void pix_opencv_matchshape :: processRGBAImage(imageStruct &image)
{ 
	error( "pix_opencv_matchshape : rgba format not supported" );
}

void pix_opencv_matchshape :: processRGBImage(imageStruct &image) {
	error( "pix_opencv_matchshape : rgb format not supported");
}

void pix_opencv_matchshape :: processYUVImage(imageStruct &image) 
{
	error( "pix_opencv_matchshape : yuv format not supported" );
}
    	
void pix_opencv_matchshape :: processGrayImage(imageStruct &image)
{ 
	error( "pix_opencv_matchshape : gray format not supported" );
	error( "pix_opencv_matchshape only works on pd lists" );
	
	// TODO add support to compare grayscale images
}

/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_opencv_matchshape :: obj_setupCallback(t_class *classPtr)
{	
	CPPEXTERN_MSG0(classPtr, 	"clear",		clearMess);
	CPPEXTERN_MSG(classPtr, 	"template",		templateMess);
	CPPEXTERN_MSG(classPtr, 	"contour",		contourMess);
	CPPEXTERN_MSG1(classPtr, 	"threshold",	thresholdMess,	float);
	CPPEXTERN_MSG1(classPtr, 	"method",		methodMess,		int);
}

void pix_opencv_matchshape :: clearMess(void)
{
	for ( vector<vector<cv::Point2f> >::iterator it=m_template_vec_vec.end(); it!=m_template_vec_vec.begin(); it--)
	{
		(*it).clear();
	}
	m_template_vec_vec.clear();
	m_template_vec_mat.clear();
	
	t_atom data;
	SETFLOAT(&data, (float)m_template_vec_vec.size());
	outlet_anything(m_dataout, gensym("template_vec"), 1, &data);

	SETFLOAT(&data, (float)m_template_vec_mat.size());
	outlet_anything(m_dataout, gensym("template_mat"), 1, &data);
}
	

void pix_opencv_matchshape :: contourMess(t_symbol*s, int argc, t_atom*argv)
{
	if ( m_template_vec_vec.empty() ){
		error("no template to match, please load a template before matching");
		return;
	}
	
	if ( (argc%2)!=0 ) {
		error("template message argument should be multiple of 2");
		return;
	}
	
	for (int i=0;i<argc;i++){
		if(argv[i].a_type!=A_FLOAT){
			error("template message should have only float arguments");
			return;
		}
	}
	
	vector<cv::Point2f> tmp_contour;
	for (int i=2;i<argc;i+=2){ // start at index 2, first two elements are reserved (for compatibility)
		cv::Point2f pt;
		pt.x = atom_getfloat(argv+i);
		pt.y = atom_getfloat(argv+i+1);
		tmp_contour.push_back(pt);
	}
	
	double comp, min(99999999999.);
	int id(0),i(0);
	for ( vector<vector<cv::Point2f> >::iterator it=m_template_vec_vec.begin(); it!=m_template_vec_vec.end(); ++it){
		comp = cv::matchShapes(tmp_contour, *it, m_method, 0.);
		if ( comp < min ){
			min=comp;
			id=i;
		}
		i++;
	}
	
	if ( min < m_threshold )
	{
		t_atom data[2];
		SETFLOAT(data,id);
		SETFLOAT(data+1, min);
		outlet_anything(m_dataout, gensym("match_vec"), 2, data);
	}
}

void pix_opencv_matchshape :: templateMess(t_symbol*s, int argc, t_atom*argv)
{
	if ( (argc%2)!=0 ) {
		error("template message argument should be multiple of 2");
		return;
	}
	for (int i=0;i<argc;i++){
		if(argv[i].a_type!=A_FLOAT){
			error("template message should have only float arguments");
			return;
		}
	}
	
	vector<cv::Point2f> tmp_contour;
	tmp_contour.clear();
	for (int i=2;i<argc;i+=2){ // start at index 2, first two elements are reserved (for compatibility)
		cv::Point2f pt;
		pt.x = atom_getfloat(argv+i);
		pt.y = atom_getfloat(argv+i+1);
		tmp_contour.push_back(pt);
	}
	m_template_vec_vec.push_back(tmp_contour);
	
	t_atom data;
	SETFLOAT(&data, (int)m_template_vec_vec.size());
	outlet_anything(m_dataout, gensym("template_vec"), 1, &data);
}

void pix_opencv_matchshape :: thresholdMess(float arg)
{
	m_threshold=(double)(arg>0.?arg:0.);
	t_atom data;
	SETFLOAT(&data, m_threshold);
	outlet_anything(m_dataout, gensym("threshold"), 1, &data);
}

void pix_opencv_matchshape :: methodMess(int arg)
{
	switch (arg) {
		case 1:
			m_method=CV_CONTOURS_MATCH_I1;
			break;
		case 2:
			m_method=CV_CONTOURS_MATCH_I2;
			break;
		case 3:
			m_method=CV_CONTOURS_MATCH_I3;
			break;
		default:
			error("method should be 1, 2 or 3");
			m_method=CV_CONTOURS_MATCH_I1;
		}
	t_atom data;
	switch (m_method) {
		case CV_CONTOURS_MATCH_I1:
			SETFLOAT(&data, 1);
			break;
		case CV_CONTOURS_MATCH_I2:
			SETFLOAT(&data, 2);
			break;
		case CV_CONTOURS_MATCH_I3:
			SETFLOAT(&data, 3);
			break;
		}
	outlet_anything(m_dataout, gensym("method"), 1, &data);
}