summaryrefslogtreecommitdiffstats
path: root/modules/image/src/img_cv.cpp
blob: 23592187cbd633c45a5f54600d56a82de69e99ec (plain) (blame)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
		/* fabs */
	#include <cmath>
		/* true & false */
	#include <cstdbool>
		/* snprintf() */
	#include <cstdio>

/* Packages ------------------------------------------------------------------*/
		/* opencv */
	#include <opencv2/opencv.hpp>

/* Module --------------------------------------------------------------------*/
		/* data */
	#include "img_iface.hpp"

	#include "img_cv.hpp"


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
	/* Core: The core functionality */
		/* Pixel */
static	void	img_cv_pixel_value	(class cv::Mat  *imgptr,  void  *data);
		/* ROI */
static	void	img_cv_set_ROI		(class cv::Mat  *imgptr,  void  *data);
		/* Operations on Arrays */
static	void	img_cv_and_2ref		(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_not		(class cv::Mat  *imgptr);
static	void	img_cv_or_2ref		(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_component	(class cv::Mat  *imgptr,  void  *data);
	/* Imgproc: Image processing */
		/* Image filtering */
static	void	img_cv_dilate		(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_erode		(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_smooth		(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_sobel		(class cv::Mat  *imgptr,  void  *data);
		/* Geometric image transformations */
static	void	img_cv_rotate_orto	(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_rotate		(class cv::Mat  *imgptr,  void  *data);
		/* Miscellaneous image transformations */
static	void	img_cv_adaptive_thr	(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_cvt_color	(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_distance_transform	(class cv::Mat  *imgptr);
static	void	img_cv_threshold	(class cv::Mat  *imgptr,  void  *data);
		/* Histograms */
static	void	img_cv_histogram	(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_histogram_c3	(class cv::Mat  *imgptr,  void  *data);
		/* Structural analysis and shape descriptors */
static	void	img_cv_contours		(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_contours_size	(void  *data);
static	void	img_cv_bounding_rect	(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_fit_ellipse	(class cv::Mat  *imgptr,  void  *data);
static	void	img_cv_min_area_rect	(class cv::Mat  *imgptr,  void  *data);
		/* Feature detection */
static	void	img_cv_hough_circles	(class cv::Mat  *imgptr,  void  *data);


/******************************************************************************
 ******* main *****************************************************************
 ******************************************************************************/
void	img_cv_act	(class cv::Mat  *imgptr, int action, void *data)
{
	switch (action) {
	case IMG_CV_ACT_PIXEL_VALUE:
		img_cv_pixel_value(imgptr, data);
		break;

	case IMG_CV_ACT_SET_ROI:
		img_cv_set_ROI(imgptr, data);
		break;

	case IMG_CV_ACT_AND_2REF:
		img_cv_and_2ref(imgptr, data);
		break;
	case IMG_CV_ACT_NOT:
		img_cv_not(imgptr);
		break;
	case IMG_CV_ACT_OR_2REF:
		img_cv_or_2ref(imgptr, data);
		break;
	case IMG_CV_ACT_COMPONENT:
		img_cv_component(imgptr, data);
		break;

	case IMG_CV_ACT_DILATE:
		img_cv_dilate(imgptr, data);
		break;
	case IMG_CV_ACT_ERODE:
		img_cv_erode(imgptr, data);
		break;
	case IMG_CV_ACT_SMOOTH:
		img_cv_smooth(imgptr, data);
		break;
	case IMG_CV_ACT_SOBEL:
		img_cv_sobel(imgptr, data);
		break;

	case IMG_CV_ACT_ROTATE_ORTO:
		img_cv_rotate_orto(imgptr, data);
		break;
	case IMG_CV_ACT_ROTATE:
		img_cv_rotate(imgptr, data);
		break;

	case IMG_CV_ACT_ADAPTIVE_THRESHOLD:
		img_cv_adaptive_thr(imgptr, data);
		break;
	case IMG_CV_ACT_CVT_COLOR:
		img_cv_cvt_color(imgptr, data);
		break;
	case IMG_CV_ACT_DISTANCE_TRANSFORM:
		img_cv_distance_transform(imgptr);
		break;
	case IMG_CV_ACT_THRESHOLD:
		img_cv_threshold(imgptr, data);
		break;

	case IMG_CV_ACT_HISTOGRAM:
		img_cv_histogram(imgptr, data);
		break;
	case IMG_CV_ACT_HISTOGRAM_C3:
		img_cv_histogram_c3(imgptr, data);
		break;

	case IMG_CV_ACT_CONTOURS:
		img_cv_contours(imgptr, data);
		break;
	case IMG_CV_ACT_CONTOURS_SIZE:
		img_cv_contours_size(data);
		break;
	case IMG_CV_ACT_BOUNDING_RECT:
		img_cv_bounding_rect(imgptr, data);
		break;
	case IMG_CV_ACT_FIT_ELLIPSE:
		img_cv_fit_ellipse(imgptr, data);
		break;
	case IMG_CV_ACT_MIN_AREA_RECT:
		img_cv_min_area_rect(imgptr, data);
		break;

	case IMG_CV_ACT_HOUGH_CIRCLES:
		img_cv_hough_circles(imgptr, data);
		break;
	}
}


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
/* Core: The core functionality */
/* ----- Pixel */
static	void	img_cv_pixel_value	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Pixel_Value	*data_cast;
	data_cast	= (struct Img_Iface_Data_Pixel_Value *)data;

	/* Value */
	unsigned char	*val;
	val		= data_cast->val;
	/* Position */
	int		x;
	x		= data_cast->x;
	int		y;
	y		= data_cast->y;

	/* Get value */
	*val	= imgptr->at<unsigned char>(y, x);
}

/* ----- ROI */
static	void	img_cv_set_ROI		(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_SetROI	*data_cast;
	data_cast	= (struct Img_Iface_Data_SetROI *)data;

	/* Rectangle */
	class cv::Rect_ <int>	*rect;
	rect		= &(data_cast->rect);

	/* Set ROI */
	*imgptr	= (*imgptr)(*rect);
}

/* ----- Operations on arrays */
static	void	img_cv_and_2ref		(class cv::Mat  *imgptr, void *data)
{
	class cv::Mat	*img_ref;
	img_ref	= (class cv::Mat *)data;

	cv::bitwise_and(*imgptr, *img_ref, *imgptr);
}

static	void	img_cv_not		(class cv::Mat  *imgptr)
{
	cv::bitwise_not(*imgptr, *imgptr);
}

static	void	img_cv_or_2ref		(class cv::Mat  *imgptr, void *data)
{
	class cv::Mat	*img_ref;
	img_ref	= (class cv::Mat *)data;

	cv::bitwise_or(*imgptr, *img_ref, *imgptr);
}

static	void	img_cv_component	(class cv::Mat  *imgptr, void *data)
{
	class cv::Mat	cmp_img[3];

	/* Data */
	struct Img_Iface_Data_Component	*data_cast;
	data_cast	= (struct Img_Iface_Data_Component *)data;

	/* Color component */
	int	cmp;
	cmp	= data_cast->cmp;

	/* Write components into cmp_img[] */
	cv::split(*imgptr, cmp_img);

	/* Write tmp into imgptr */
	imgptr->release();
	cmp_img[cmp].copyTo(*imgptr);

	/* clean up */
	cmp_img[0].release();
	cmp_img[1].release();
	cmp_img[2].release();
}

/* Imgproc: Image processing */
/* ----- Image filtering */
static	void	img_cv_dilate		(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Dilate_Erode	*data_cast;
	data_cast	= (struct Img_Iface_Data_Dilate_Erode *)data;

	/* Iterations */
	int	i;
	i	= data_cast->i;

	/* Dilate */
	cv::dilate(*imgptr, *imgptr, cv::Mat(), cv::Point(-1,-1), i,
							cv::BORDER_REPLICATE);
}

static	void	img_cv_erode		(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Dilate_Erode	*data_cast;
	data_cast	= (struct Img_Iface_Data_Dilate_Erode *)data;

	/* Iterations */
	int	i;
	i	= data_cast->i;

	/* Erode */
	cv::erode(*imgptr, *imgptr, cv::Mat(), cv::Point(-1,-1), i,
							cv::BORDER_REPLICATE);
}

static	void	img_cv_smooth		(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Smooth	*data_cast;
	data_cast	= (struct Img_Iface_Data_Smooth *)data;

	/* Smoothing method */
	int	method;
	method	= data_cast->method;
	/* Kernel size */
	int	ksize;
	ksize	= data_cast->ksize;
	if (!(ksize % 2)) {
		ksize++;
	}

	switch (method) {
	case IMGI_SMOOTH_MEAN:
		cv::blur(*imgptr, *imgptr, cv::Size(ksize, ksize),
					cv::Point(-1,-1), cv::BORDER_DEFAULT);
		break;
	case IMGI_SMOOTH_GAUSS:
		cv::GaussianBlur(*imgptr, *imgptr, cv::Size(ksize, ksize),
					0, 0, cv::BORDER_DEFAULT);
		break;
	case IMGI_SMOOTH_MEDIAN:
		cv::medianBlur(*imgptr, *imgptr, ksize);
		break;
	}
}

static	void	img_cv_sobel		(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Sobel	*data_cast;
	data_cast	= (struct Img_Iface_Data_Sobel *)data;

	/* Order of the derivative x */
	int	dx;
	dx	= data_cast->dx;
	/* Order of the derivative y */
	int	dy;
	dy	= data_cast->dy;
	/* Size of the extended Sobel kernel */
	int	ksize;
	ksize	= data_cast->ksize;
	if (!(ksize % 2)) {
		ksize++;
	}

	cv::Sobel(*imgptr, *imgptr, -1, dx, dy, ksize, 1, 0,
							cv::BORDER_DEFAULT);
}

/* ----- Geometric image transformations */
static	void	img_cv_rotate_orto	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Rotate_Orto	*data_cast;
	data_cast	= (struct Img_Iface_Data_Rotate_Orto *)data;

	/* Rotate n * pi/2 rad */
	int	n;
	n	= data_cast->n;

	switch (n) {
	case 1:
		/* Rotate: transpose and flip around horizontal axis: flip_mode=0 */
		cv::transpose(*imgptr, *imgptr);
		cv::flip(*imgptr, *imgptr, 0);
		break;

	case 2:
		/* Rotate: Flip both axises: flip_mode=-1 */
		cv::flip(*imgptr, *imgptr, -1);
		break;

	case 3:
		/* Rotate: transpose and flip around vertical axis: flip_mode=1 */
		cv::transpose(*imgptr, *imgptr);
		cv::flip(*imgptr, *imgptr, 1);
		break;
	}
}

static	void	img_cv_rotate		(class cv::Mat  *imgptr, void *data)
{
	class cv::Mat	map_matrix;

	/* Data */
	struct Img_Iface_Data_Rotate	*data_cast;
	data_cast	= (struct Img_Iface_Data_Rotate *)data;

	/* Angle of rotation */
	class cv::Point_ <float>	*center;
	center		= &(data_cast->center);
	double				angle;
	angle		= data_cast->angle;

	/* Don't rotate if angle is negligible */
	if (fabs(angle) > 1.0) {
		/* Get map_matrix */
		map_matrix	= cv::getRotationMatrix2D(*center, angle, 1);

		/* Rotate */
		cv::warpAffine(*imgptr, *imgptr, map_matrix, imgptr->size(),
					cv::INTER_LINEAR, cv::BORDER_CONSTANT,
					cv::Scalar(0, 0, 0));
	}

	/* clean up */
	map_matrix.release();
}

/* ----- Miscellaneous image transformations */
static	void	img_cv_adaptive_thr	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Adaptive_Thr	*data_cast;
	data_cast	= (struct Img_Iface_Data_Adaptive_Thr *)data;

	/* Threshold method */
	int	method;
	method	= data_cast->method;
	/* Threshold type */
	int	thr_typ;
	thr_typ	= data_cast->thr_typ;
	/* Neighbourhood size */
	int	ksize;
	ksize	= data_cast->ksize;
	if (!(ksize % 2)) {
		ksize++;
	}

	/* Apply adaptive threshold */
	cv::adaptiveThreshold(*imgptr, *imgptr, 255, method, thr_typ, ksize,
									0);
}

static	void	img_cv_cvt_color	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Cvt_Color	*data_cast;
	data_cast	= (struct Img_Iface_Data_Cvt_Color *)data;

	/* Conversion method */
	int	method;
	method	= data_cast->method;

	cv::cvtColor(*imgptr, *imgptr, method, 0);
}

static	void	img_cv_distance_transform	(class cv::Mat  *imgptr)
{
	class cv::Mat	imgtmp;

	/* Get transform */
	cv::distanceTransform(*imgptr, imgtmp, CV_DIST_L2, CV_DIST_MASK_PRECISE);

	/* DistanceTransform  gives CV_32F image */
	imgtmp.convertTo(*imgptr, CV_8U);

	/* Cleanup */
	imgtmp.release();
}

static	void	img_cv_threshold	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Threshold	*data_cast;
	data_cast	= (struct Img_Iface_Data_Threshold *)data;

	/* Threshold type */
	int	thr_typ;
	thr_typ	= data_cast->thr_typ;
	/* Threshold value */
	int	thr_val;
	thr_val	= data_cast->thr_val;
	if (thr_val == -1) {
		thr_typ	|= cv::THRESH_OTSU;
	}

	/* Threshold */
	cv::threshold(*imgptr, *imgptr, thr_val, 0xFF, thr_typ);
}

/* ----- Histograms */
static	void	img_cv_histogram	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Histogram	*data_cast;
	data_cast	= (struct Img_Iface_Data_Histogram *)data;

	/* Contours */
	class cv::Mat	*hist;
	hist		= data_cast->hist_c0;
	class cv::Mat	*hist_img;
	hist_img	= data_cast->hist_img;

	/* Write components into cmp_img[] */
	int		h_size		= 256;
	float		h_range_arr[]	= {0.0, 256.0};
	const float	*h_range	= {h_range_arr};
	cv::calcHist(imgptr, 1, 0, cv::Mat(), *hist, 1, &h_size, &h_range,
								true, false);

	/* Init */
	hist_img->setTo(cv::Scalar(0));

	/* Normalize the result to [0, hist_img->rows - 1] */
	cv::normalize(*hist, *hist, 0, hist_img->rows - 1, cv::NORM_MINMAX, -1,
								cv::Mat());

	/* Draw hist into hist_img */
	int	i;
	for(i = 0; i < 256; i++ ) {
		cv::line(*hist_img, cv::Point(i, hist_img->rows - 0),
				cv::Point(i, hist_img->rows - hist->at<float>(i)),
				cv::Scalar(255, 0, 0), 1, 8, 0);
	}

	/* Show histogram */
	img_iface_show_hist_c1();
}

static	void	img_cv_histogram_c3	(class cv::Mat  *imgptr, void *data)
{
	class cv::Mat	cmp_img[3];

	/* Write components into cmp_img[] */
	cv::split(*imgptr, cmp_img);

	/* Data */
	struct Img_Iface_Data_Histogram	*data_cast;
	data_cast	= (struct Img_Iface_Data_Histogram *)data;

	/* Contours */
	class cv::Mat	*hist_c0;
	hist_c0		= data_cast->hist_c0;
	class cv::Mat	*hist_c1;
	hist_c1		= data_cast->hist_c1;
	class cv::Mat	*hist_c2;
	hist_c2		= data_cast->hist_c2;
	class cv::Mat	*hist_img;
	hist_img	= data_cast->hist_img;

	/* Write components into cmp_img[] */
	int		h_size		= 256;
	float		h_range_arr[]	= {0.0, 256.0};
	const float	*h_range	= {h_range_arr};
	cv::calcHist(&cmp_img[0], 1, 0, cv::Mat(), *hist_c0, 1, &h_size,
							&h_range, true, false);
	cv::calcHist(&cmp_img[1], 1, 0, cv::Mat(), *hist_c1, 1, &h_size,
							&h_range, true, false);
	cv::calcHist(&cmp_img[2], 1, 0, cv::Mat(), *hist_c2, 1, &h_size,
							&h_range, true, false);

	/* Init */
	hist_img->setTo(cv::Scalar(0));

	/* Normalize the result to [0, hist_img->rows - 1] */
	cv::normalize(*hist_c0, *hist_c0, 0, hist_img->rows - 1,
						cv::NORM_MINMAX, -1, cv::Mat());
	cv::normalize(*hist_c1, *hist_c1, 0, hist_img->rows - 1,
						cv::NORM_MINMAX, -1, cv::Mat());
	cv::normalize(*hist_c2, *hist_c2, 0, hist_img->rows - 1,
						cv::NORM_MINMAX, -1, cv::Mat());

	/* Draw hist into hist_img */
	int	i;
	int	j;
	for(i = 0; i < 256; i++ ) {
		cv::line(*hist_img, cv::Point(3*i, hist_img->rows - 0),
				cv::Point(3*i,
				hist_img->rows - hist_c0->at<float>(i)),
				cv::Scalar(255, 0, 0), 1, 8, 0);
	}
	for(i = 0; i < 256; i++ ) {
		cv::line(*hist_img, cv::Point(3*i + 1, hist_img->rows - 0),
				cv::Point(3*i+1,
				hist_img->rows - hist_c1->at<float>(i)),
				cv::Scalar(0, 255, 0), 1, 8, 0);
	}
	for(i = 0; i < 256; i++ ) {
		cv::line(*hist_img, cv::Point(3*i + 2, hist_img->rows - 0),
				cv::Point(3*i + 2,
				hist_img->rows - hist_c2->at<float>(i)),
				cv::Scalar(0, 0, 255), 1, 8, 0);
	}

	/* Show histogram */
	img_iface_show_hist_c3();

	/* Cleanup */
	cmp_img[0].release();
	cmp_img[1].release();
	cmp_img[2].release();
}

/* ----- Structural analysis and shape descriptors */
static	void	img_cv_contours		(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Contours	*data_cast;
	data_cast	= (struct Img_Iface_Data_Contours *)data;

	/* Contours */
	std::vector <std::vector <class cv::Point_ <int>>>	*contours;
	contours	= data_cast->contours;
	class cv::Mat						*hierarchy;
	hierarchy	= data_cast->hierarchy;

	/* Get contours */
	cv::findContours(*imgptr, *contours, *hierarchy, CV_RETR_EXTERNAL,
							CV_CHAIN_APPROX_SIMPLE);

	/* Set image to black */
	imgptr->setTo(cv::Scalar(0));

	/* Draw contours in color */
	cv::drawContours(*imgptr, *contours, -1, cv::Scalar(255), 1, 8,
						*hierarchy, 1, cvPoint(0, 0));
}

static	void	img_cv_contours_size	(void *data)
{
	/* Data */
	struct Img_Iface_Data_Contours_Size	*data_cast;
	data_cast	= (struct Img_Iface_Data_Contours_Size *)data;

	/* Contours */
	std::vector <std::vector <class cv::Point_ <int>>>	*contours;
	contours	= data_cast->contours;

	/* Get area and perimeter */
	int	i;
	for (i = 0; i < contours->size(); i++) {
		data_cast->area[i]	= cv::contourArea(
							(*contours)[i], false);
		data_cast->perimeter[i]	= cv::arcLength(
							(*contours)[i], true);
	}
}

static	void	img_cv_bounding_rect	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Bounding_Rect	*data_cast;
	data_cast	= (struct Img_Iface_Data_Bounding_Rect *)data;

	/* Contours */
	std::vector <class cv::Point_ <int>>	*contour;
	contour	= data_cast->contour;
	/* Rotated rectangle */
	class cv::Rect_ <int>			*rect;
	rect	= data_cast->rect;
	/* Show rectangle ? */
	bool					show;
	show	= data_cast->show;

	/* Get rectangle */
	*rect	= cv::boundingRect(*contour);

	/* Draw rectangle */
	class cv::Point_<float>	vertices[4];
	if (show) {
		vertices[0].x	= rect->x;
		vertices[0].y	= rect->y;
		vertices[1].x	= rect->x + rect->width;
		vertices[1].y	= rect->y;
		vertices[2].x	= rect->x + rect->width;
		vertices[2].y	= rect->y + rect->height;
		vertices[3].x	= rect->x;
		vertices[3].y	= rect->y + rect->height;
		cv::line(*imgptr, cv::Point(vertices[0].x, vertices[0].y),
					cv::Point(vertices[1].x, vertices[1].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[1].x, vertices[1].y),
					cv::Point(vertices[2].x, vertices[2].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[2].x, vertices[2].y),
					cv::Point(vertices[3].x, vertices[3].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[3].x, vertices[3].y),
					cv::Point(vertices[0].x, vertices[0].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
	}
}

static	void	img_cv_fit_ellipse	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_MinARect	*data_cast;
	data_cast	= (struct Img_Iface_Data_MinARect *)data;

	/* Contours */
	std::vector <class cv::Point_ <int>>	*contour;
	contour	= data_cast->contour;
	/* Rotated rectangle */
	class cv::RotatedRect			*rect;
	rect	= data_cast->rect;
	/* Show rectangle ? */
	bool					show;
	show	= data_cast->show;

	/* Get rectangle */
	*rect	= cv::fitEllipse(*contour);

	/* Draw rectangle */
	class cv::Point_<float>	vertices[4];
	if (show) {
		rect->points(vertices);
		cv::line(*imgptr, cv::Point(vertices[0].x, vertices[0].y),
					cv::Point(vertices[1].x, vertices[1].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[1].x, vertices[1].y),
					cv::Point(vertices[2].x, vertices[2].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[2].x, vertices[2].y),
					cv::Point(vertices[3].x, vertices[3].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[3].x, vertices[3].y),
					cv::Point(vertices[0].x, vertices[0].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
	}
}

static	void	img_cv_min_area_rect	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_MinARect	*data_cast;
	data_cast	= (struct Img_Iface_Data_MinARect *)data;

	/* Contours */
	std::vector <class cv::Point_ <int>>	*contour;
	contour	= data_cast->contour;
	/* Rotated rectangle */
	class cv::RotatedRect			*rect;
	rect	= data_cast->rect;
	/* Show rectangle ? */
	bool					show;
	show	= data_cast->show;

	/* Get rectangle */
	*rect	= cv::minAreaRect(*contour);

	/* Draw rectangle */
	class cv::Point_<float>	vertices[4];
	if (show) {
		rect->points(vertices);
		cv::line(*imgptr, cv::Point(vertices[0].x, vertices[0].y),
					cv::Point(vertices[1].x, vertices[1].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[1].x, vertices[1].y),
					cv::Point(vertices[2].x, vertices[2].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[2].x, vertices[2].y),
					cv::Point(vertices[3].x, vertices[3].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
		cv::line(*imgptr, cv::Point(vertices[3].x, vertices[3].y),
					cv::Point(vertices[0].x, vertices[0].y),
					CV_RGB(0, 0, 255), 1, 8, 0);
	}
}

/* ----- Feature detection */
static	void	img_cv_hough_circles	(class cv::Mat  *imgptr, void *data)
{
	/* Data */
	struct Img_Iface_Data_Hough_Circles	*data_cast;
	data_cast	= (struct Img_Iface_Data_Hough_Circles *)data;

	/* Contours */
	std::vector <class cv::Vec <float, 3>>	*circles;
	circles		= data_cast->circles;
	/* Parameters */
	double					dist_min;
	double					param_1;
	double					param_2;
	int					radius_min;
	int					radius_max;
	dist_min	= data_cast->dist_min;
	param_1		= data_cast->param_1;
	param_2		= data_cast->param_2;
	radius_min	= data_cast->radius_min;
	radius_max	= data_cast->radius_max;

	/* Get circles */
	cv::HoughCircles(*imgptr, *circles, CV_HOUGH_GRADIENT, 1, dist_min,
				param_1, param_2, radius_min, radius_max);

	/* Set image to black */
	imgptr->setTo(cv::Scalar(0));

	/* Draw circles */
	class cv::Point_ <int>	center;
	int			radius;
	int	i;
	for (i = 0; i < circles->size(); i++) {
		center.x	= cvRound((*circles)[i][0]);
		center.y	= cvRound((*circles)[i][1]);
		radius		= cvRound((*circles)[i][2]);

		/* Draw the circle center */
/*			cv::circle(*imgptr, center, 3,
				cv::Scalar(0, 255, 0), -1, 8, 0);*/

		/* Draw the circle outline */
		cv::circle(*imgptr, center, radius,
				cv::Scalar(250), 1, 8, 0);
	}
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/