summaryrefslogtreecommitdiffstats
path: root/src/templates/base.c
blob: 32367dc8c485ec800b7972ec0e2677fa8667fe16 (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
/******************************************************************************
 *	Copyright (C) 2020	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "templates/base.h"

#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#define ALX_NO_PREFIX
#include <libalx/base/compiler.h>
#include <libalx/base/stdint.h>
#include <libalx/base/stdio.h>
#include <libalx/extra/cv/cv.h>

#include "dbg.h"
#include "symbols.h"
#include "templates/templates.h"


/******************************************************************************
 ******* macro ****************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* enum / struct / union ************************************************
 ******************************************************************************/


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* static prototypes ****************************************************
 ******************************************************************************/


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
int	match_t_base	(img_s *restrict sym, uint32_t *code, ptrdiff_t i)
{
	img_s		*base;
	img_s		*tmp;
	conts_s		*conts;
	double		match, m;
	int		status;

	/* init */
	status	= -1;
	if (alx_cv_init_img(&base))
		return	status;
	if (alx_cv_init_img(&tmp))
		goto err0;
	if (alx_cv_init_conts(&conts))
		goto err1;

	/* Find base match */
	status--;
	if (symbol_base(sym, base))
		goto err;					dbg_show(2, base);
	status--;
	match	= -INFINITY;
	BITFIELD_SET(code, CODE_BASE_POS, CODE_BASE_LEN);

	m = alx_cv_compare_bitwise(base, base_templates[i], 2);	dbg_printf(2, "match: %.5lf\n", m);
	alx_cv_clone(tmp, base);
	alx_cv_resize_2largest(tmp, base_templates[i]);
	alx_cv_xor_2ref(tmp, base_templates[i]);		dbg_show(2, tmp);
	if (m >= match) {
		BITFIELD_WRITE(code, CODE_BASE_POS, CODE_BASE_LEN, i);
		BIT_SET(code, CODE_Y_N_POS);
		match	= m;
	}

	m = alx_cv_compare_bitwise(base, base_templates_not[i], 2);	dbg_printf(2, "match: %.5lf\n", m);
	alx_cv_clone(tmp, base);
	alx_cv_resize_2largest(tmp, base_templates_not[i]);
	alx_cv_xor_2ref(tmp, base_templates_not[i]);		dbg_show(2, tmp);
	if (m >= match) {
		BITFIELD_WRITE(code, CODE_BASE_POS, CODE_BASE_LEN, i);
		BIT_CLEAR(code, CODE_Y_N_POS);
		match	= m;
	}

	if (BIT_READ(*code, CODE_Y_N_POS)) {
								dbg_printf(1, "%s\n", t_base_meaning[i]);
								dbg_show(1, base_templates[i]);
	} else {
								dbg_printf(1, "%s not\n", t_base_meaning[i]);
								dbg_show(1, base_templates_not[i]);
	}

	/* deinit */
	status	= 0;
err:	alx_cv_deinit_conts(conts);
err1:	alx_cv_deinit_img(tmp);
err0:	alx_cv_deinit_img(base);
	return	status;
}

int	load_t_base		(img_s *t, const char *fname)
{
	conts_s		*conts;
	const cont_s	*cont;
	rect_s		*rect;
	int		status;

	/* init */
	status	= -1;
	if (alx_cv_init_conts(&conts))
		return	status;
	if (alx_cv_init_rect(&rect))
		goto err0;

	status--;
	if (alx_cv_imread_gray(t, fname))
		goto err;					dbg_show(4, t);
	alx_cv_threshold(t, ALX_CV_THRESH_BINARY_INV, ALX_CV_THR_OTSU);
	alx_cv_contours(t, conts);
	status--;
	if (alx_cv_conts_largest_a(&cont, NULL, conts))
		goto err;
	alx_cv_bounding_rect(rect, cont);
	alx_cv_roi_set(t, rect);				dbg_show(4, t);

	/* deinit */
	status	= 0;
err:	alx_cv_deinit_rect(rect);
err0:	alx_cv_deinit_conts(conts);
	return	status;
}


/******************************************************************************
 ******* static function definitions ******************************************
 ******************************************************************************/


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