summaryrefslogtreecommitdiff
path: root/cvg.c
blob: c2d0cd7fc63db71a224ce51123ae71dbbe281ca0 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <string.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cv.h>
#include <highgui.h>
#include <errno.h>

struct cvg {
	GtkWidget *drawing_area1;
	GtkWidget *drawing_area2;
	GtkButton *open_btn;
	CvCapture *capture;
	IplImage *image;
	IplImage *output;
	cairo_surface_t *input_surface;
	cairo_surface_t *output_surface;
	guint last_press;
	int idle_source;

	IplImage *(*filter)(struct cvg *cvg, IplImage *image);
};

static cairo_surface_t *
cairo_surface_create_from_ipl_image(IplImage *image)
{
	cairo_surface_t *surface;
	guchar *row1, *row2, *p1, *p2;
	int x, y;

	surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
					     image->width, image->height);
	row1 = (guchar *) image->imageData;
	row2 = cairo_image_surface_get_data(surface);
	for (y = 0; y < image->height; ++y) {
		p1 = row1;
		p2 = row2;
		for (x = 0; x < image->width; ++x) {
			memcpy(p2, p1, 3);

			p1 += image->nChannels;
			p2 += 4;
		}
		row1 += image->widthStep;
		row2 += cairo_image_surface_get_stride(surface);
	}

	return surface;
}

static inline double
min3(double x1, double x2, double x3)
{
	return MIN(MIN(x1, x2), x3);
}

static void
scale_centered(cairo_t *cr, double w, double h, double sw, double sh)
{
	double scale = min3(sw / w, sh / h, 1.0);

	cairo_translate(cr, (sw - scale * w) / 2.0, (sh - scale * h) / 2.0);
	cairo_scale(cr, scale, scale);
}

gboolean
draw(GtkWidget *widget, cairo_t *cr, gpointer userdata)
{
	struct cvg *cvg = userdata;
	cairo_surface_t *surface;

	if (widget == cvg->drawing_area1)
		surface = cvg->input_surface;
	else
		surface = cvg->output_surface;

	if (surface) {
		scale_centered(cr,
			       cairo_image_surface_get_width(surface),
			       cairo_image_surface_get_height(surface),
			       gtk_widget_get_allocated_width(widget),
			       gtk_widget_get_allocated_height(widget));
		cairo_set_source_surface(cr, surface, 0, 0);
	} else {
		cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
	}
	cairo_paint(cr);

	return FALSE;
}

static void
destroy_images(struct cvg *cvg)
{
	if (cvg->image)
		cvReleaseImage(&cvg->image);
	if (cvg->output)
		cvReleaseImage(&cvg->output);

	if (cvg->input_surface)
		cairo_surface_destroy(cvg->input_surface);
	cvg->input_surface = NULL;
	if (cvg->output_surface)
		cairo_surface_destroy(cvg->output_surface);
	cvg->output_surface = NULL;
}

static void
update_right_drawing_area(struct cvg *cvg)
{
	if (!cvg->image)
		return;

	if (cvg->output)
		cvReleaseImage(&cvg->output);

	cvg->output = cvg->filter(cvg, cvg->image);

	if (cvg->output_surface)
		cairo_surface_destroy(cvg->output_surface);
	cvg->output_surface = NULL;
	if (cvg->output)
		cvg->output_surface =
			cairo_surface_create_from_ipl_image(cvg->output);
	gtk_widget_queue_draw(cvg->drawing_area2);
}

static gboolean
capture(gpointer userdata)
{
	struct cvg *cvg = userdata;
	IplImage *image;

	image = cvQueryFrame(cvg->capture);
	if (!image) {
		fprintf(stderr, "ERROR: frame is null...\n");
		exit(EXIT_FAILURE);
	}

	if (cvg->image)
		cvReleaseImage(&cvg->image);
	if (cvg->input_surface)
		cairo_surface_destroy(cvg->input_surface);

	cvg->image = cvCreateImage(cvGetSize(image),
				   image->depth, image->nChannels);
	cvCopy(image, cvg->image, NULL);
	if (!cvg->image)
		return FALSE;
	cvg->input_surface = cairo_surface_create_from_ipl_image(cvg->image);
	gtk_widget_queue_draw(cvg->drawing_area1);
	update_right_drawing_area(cvg);

	return TRUE;
}

static void
stop_capture(struct cvg *cvg)
{
	if (cvg->idle_source) {
		g_source_remove(cvg->idle_source);
		cvg->idle_source = 0;
	}
}

gboolean
key_event(GtkWidget *widget, GdkEventKey *event, gpointer userdata)
{
	struct cvg *cvg = userdata;

	switch (event->type) {
	case GDK_KEY_RELEASE:
		switch (event->keyval) {
		case GDK_KEY_o:
			g_signal_emit_by_name(cvg->open_btn, "released", NULL);
			if (cvg->last_press == event->keyval)
				g_signal_emit_by_name(cvg->open_btn, "clicked",
						      NULL);
			break;
		}
		return FALSE;
	case GDK_KEY_PRESS:
		cvg->last_press = event->keyval;
		switch (event->keyval) {
		case GDK_KEY_o:
			g_signal_emit_by_name(cvg->open_btn, "pressed", NULL);
			break;
		case GDK_KEY_d:
			destroy_images(cvg);
			gtk_widget_queue_draw(cvg->drawing_area1);
			gtk_widget_queue_draw(cvg->drawing_area2);
			break;
		case GDK_KEY_s:
		case GDK_KEY_S:
			stop_capture(cvg);
			break;
		case GDK_KEY_c:
		case GDK_KEY_C:
			if (!cvg->idle_source)
				cvg->idle_source = g_idle_add(capture, cvg);
			break;
		case GDK_KEY_q:
			gtk_main_quit();
			break;
		}
		return FALSE;
	default:
		return TRUE;
	}
}

static void
open_file(struct cvg *cvg, const gchar *file)
{
	if (cvg->image)
		cvReleaseImage(&cvg->image);
	if (cvg->input_surface)
		cairo_surface_destroy(cvg->input_surface);

	cvg->image = cvLoadImage(file, CV_LOAD_IMAGE_COLOR);
	if (!cvg->image)
		return;
	cvg->input_surface = cairo_surface_create_from_ipl_image(cvg->image);
}

void
file_set(GtkFileChooserButton *widget, gpointer userdata)
{
	struct cvg *cvg = userdata;
	gchar *filename;

	stop_capture(cvg);

	filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
	open_file(cvg, filename);
	g_free(filename);

	gtk_widget_queue_draw(cvg->drawing_area1);
	update_right_drawing_area(cvg);
}

static IplImage *
pass_through_filter(struct cvg *cvg, IplImage *image)
{
	IplImage *out;

	out = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
	cvCopy(image, out, NULL);

	return out;
}

static IplImage *
gauss_filter(struct cvg *cvg, IplImage *image)
{
	IplImage *out;
	
	out = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
	cvSmooth(image, out, CV_GAUSSIAN, 9, 9, 0, 0);

	return out;
}

static IplImage *
sobel_filter(struct cvg *cvg, IplImage *image)
{
	IplImage *out, *tmp, *tmp2, *gray;

	gray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	tmp = cvCreateImage(cvGetSize(image), IPL_DEPTH_16S, 1);
	tmp2 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	out = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);

	cvCvtColor(image, gray, CV_BGR2GRAY);
	cvSobel(gray, tmp, 0, 1, 3);
	cvConvertScaleAbs(tmp, tmp2, 1, 0); 
	cvCvtColor(tmp2, out, CV_GRAY2BGR);

	cvReleaseImage(&gray);
	cvReleaseImage(&tmp);
	cvReleaseImage(&tmp2);

	return out;
}

static IplImage *
good_features_to_track(struct cvg *cvg, IplImage *image)
{
	IplImage *output, *gray_frame, *eig_image, *temp_image;
	CvPoint2D32f corners[100];
	int corner_count = sizeof(corners)/sizeof(corners[0]);
	double quality_level = 0.1, min_distance = 5;
	int i, eig_block_size = 3, use_harris = 0;

	output = pass_through_filter(cvg, image);
	gray_frame = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	eig_image = cvCreateImage(cvGetSize(image), IPL_DEPTH_32F, 1);
	temp_image = cvCreateImage(cvGetSize(image), IPL_DEPTH_32F, 1);
	cvCvtColor(image, gray_frame, CV_BGR2GRAY);

	memset(corners, 0, sizeof corners);
	cvGoodFeaturesToTrack(gray_frame, eig_image, temp_image,
			      corners, &corner_count,
			      quality_level, min_distance,  NULL,
			      eig_block_size, use_harris, 0.04);
	for (i = 0; i < corner_count; ++i) {
		CvPoint center = { corners[i].x, corners[i].y };
		cvCircle(output, center, 5, CV_RGB(255, 0, 0), 3, 8, 0);
	}
	cvReleaseImage(&gray_frame);
	cvReleaseImage(&eig_image);
	cvReleaseImage(&temp_image);

	return output;
}

static IplImage *
hide_filter(struct cvg *cvg, IplImage *image)
{
	return NULL;
}

void
destroy(GtkWidget *widget, gpointer userdata)
{
	gtk_main_quit();
}

static void
find_button(GtkWidget *widget, gpointer data)
{
	GtkButton **p = data;

	if (GTK_IS_BUTTON(widget))
		*p = GTK_BUTTON(widget);
}

void
method_set(GtkComboBox *widget, gpointer userdata)
{
	struct cvg *cvg = userdata;
	gchar *text;

	text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
	if (text == NULL)
		return;
	if (strcmp(text, "Gauss") == 0)
		cvg->filter = gauss_filter;
	else if (strcmp(text, "GoodFeaturesToTrack") == 0)
		cvg->filter = good_features_to_track;
	else if (strcmp(text, "Pass-Through") == 0)
		cvg->filter = pass_through_filter;
	else if (strcmp(text, "Sobel") == 0)
		cvg->filter = sobel_filter;
	else if (strcmp(text, "Hide") == 0)
		cvg->filter = hide_filter;
	update_right_drawing_area(cvg);
}

int
main(int argc, char *argv[])
{
	struct cvg cvg;
	GtkBuilder *builder;
	GObject *file_chooser;
	GObject *combobox;
	gchar *path, *file;
	int index = 0;

	memset(&cvg, 0, sizeof cvg);
	gtk_init(&argc, &argv);

	if (argc >= 2)
		open_file(&cvg, argv[argc-1]);

	cvg.capture = cvCreateCameraCapture(index);
	if (!cvg.capture) {
		fprintf(stderr, "ERROR: capture is NULL \n");
		exit(EXIT_FAILURE);
	}

#if 1
	cvSetCaptureProperty(cvg.capture, CV_CAP_PROP_FRAME_WIDTH,  10000);
	cvSetCaptureProperty(cvg.capture, CV_CAP_PROP_FRAME_HEIGHT, 10000);
#endif

	printf("width: %.f, height: %.f\n",
	       cvGetCaptureProperty(cvg.capture, CV_CAP_PROP_FRAME_WIDTH),
	       cvGetCaptureProperty(cvg.capture, CV_CAP_PROP_FRAME_HEIGHT));

	builder = gtk_builder_new();
	path = g_path_get_dirname(argv[0]);
	file = g_build_filename(path, "cvg.ui", NULL);
	gtk_builder_add_from_file(builder, file, NULL);
	g_free(path);
	g_free(file);
	gtk_builder_connect_signals(builder, &cvg);

	cvg.drawing_area1 = GTK_WIDGET(gtk_builder_get_object(builder,
							      "drawingarea1"));
	cvg.drawing_area2 = GTK_WIDGET(gtk_builder_get_object(builder,
							      "drawingarea2"));
	file_chooser = gtk_builder_get_object(builder, "file-chooser");
	gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_chooser),
					    g_get_current_dir());
	/* There is no way to retreive the GtkButton from GtkFileChooserButton,
	 * Try to find a GtkButton in the inherited GtkContainer.
	 */
	gtk_container_forall(GTK_CONTAINER(file_chooser),
			     find_button, &cvg.open_btn);
	combobox = gtk_builder_get_object(builder, "comboboxtext1");
	gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);

	cvg.idle_source = g_idle_add(capture, &cvg);
	cvg.filter = hide_filter;
	gtk_widget_show_all(GTK_WIDGET(gtk_builder_get_object(builder, "win")));
	g_object_unref(builder);
	gtk_main();

	destroy_images(&cvg);

	return 0;
}

/* FIXME: Need to overload perror so that invalid OpenCV messages are hidden. */
void
perror(const char *s)
{
	if (strcmp("VIDIOC_QUERYMENU", s) == 0)
		return;

	fprintf(stderr, "%s: %s\n", s, strerror(errno));
}