Coverage Report

Created: 2024-08-18 16:21

/home/liu/actions-runner/_work/ccv/ccv/test/unit/basic.tests.c
Line
Count
Source
1
#include "ccv.h"
2
#include "case.h"
3
#include "ccv_case.h"
4
5
#ifdef HAVE_LIBPNG
6
TEST_CASE("sobel operation")
7
1
{
8
1
  ccv_dense_matrix_t* image = 0;
9
1
  ccv_read("../../samples/chessbox.png", &image, CCV_IO_GRAY | CCV_IO_ANY_FILE);
10
1
  ccv_dense_matrix_t* x = 0;
11
1
  ccv_sobel(image, &x, 0, 0, 1);
12
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/chessbox.sobel.x.bin", "should be sobel of partial derivative on x");
13
1
  ccv_dense_matrix_t* y = 0;
14
1
  ccv_sobel(image, &y, 0, 1, 0);
15
1
  REQUIRE_MATRIX_FILE_EQ(y, "data/chessbox.sobel.y.bin", "should be sobel of partial derivative on y");
16
1
  ccv_dense_matrix_t* u = 0;
17
1
  ccv_sobel(image, &u, 0, 1, 1);
18
1
  REQUIRE_MATRIX_FILE_EQ(u, "data/chessbox.sobel.u.bin", "should be sobel of derivative along diagonal");
19
1
  ccv_dense_matrix_t* v = 0;
20
1
  ccv_sobel(image, &v, 0, -1, 1);
21
1
  REQUIRE_MATRIX_FILE_EQ(v, "data/chessbox.sobel.v.bin", "should be sobel of derivative along the other diagonal");
22
1
  ccv_dense_matrix_t* x3 = 0;
23
1
  ccv_sobel(image, &x3, 0, 0, 3);
24
1
  REQUIRE_MATRIX_FILE_EQ(x3, "data/chessbox.sobel.x.3.bin", "should be sobel of partial derivative on x within 3x3 window");
25
1
  ccv_dense_matrix_t* y3 = 0;
26
1
  ccv_sobel(image, &y3, 0, 3, 0);
27
1
  REQUIRE_MATRIX_FILE_EQ(y3, "data/chessbox.sobel.y.3.bin", "should be sobel of partial derivative on y within 3x3 window");
28
1
  ccv_dense_matrix_t* x5 = 0;
29
1
  ccv_sobel(image, &x5, 0, 0, 5);
30
1
  REQUIRE_MATRIX_FILE_EQ(x5, "data/chessbox.sobel.x.5.bin", "should be sobel of partial derivative on x within 5x5 window");
31
1
  ccv_dense_matrix_t* y5 = 0;
32
1
  ccv_sobel(image, &y5, 0, 5, 0);
33
1
  REQUIRE_MATRIX_FILE_EQ(y5, "data/chessbox.sobel.y.5.bin", "should be sobel of partial derivative on y within 5x5 window");
34
1
  ccv_matrix_free(image);
35
1
  ccv_matrix_free(x);
36
1
  ccv_matrix_free(y);
37
1
  ccv_matrix_free(u);
38
1
  ccv_matrix_free(v);
39
1
  ccv_matrix_free(x3);
40
1
  ccv_matrix_free(y3);
41
1
  ccv_matrix_free(x5);
42
1
  ccv_matrix_free(y5);
43
1
}
44
45
TEST_CASE("resample operation of CCV_INTER_AREA")
46
1
{
47
1
  ccv_dense_matrix_t* image = 0;
48
1
  ccv_read("../../samples/chessbox.png", &image, CCV_IO_ANY_FILE);
49
1
  ccv_dense_matrix_t* x = 0;
50
1
  ccv_resample(image, &x, 0, 1.0 / 5.0, 1.0 / 5.0, CCV_INTER_AREA);
51
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/chessbox.resample.bin", "should be a image of color dot");
52
1
  ccv_matrix_free(image);
53
1
  ccv_matrix_free(x);
54
1
}
55
56
TEST_CASE("sample down operation with source offset (10, 10)")
57
1
{
58
1
  ccv_dense_matrix_t* image = 0;
59
1
  ccv_read("../../samples/chessbox.png", &image, CCV_IO_ANY_FILE);
60
1
  ccv_dense_matrix_t* x = 0;
61
1
  ccv_sample_down(image, &x, 0, 10, 10);
62
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/chessbox.sample_down.bin", "should be down sampled (/2) image with offset from source (10, 10)");
63
1
  ccv_matrix_free(image);
64
1
  ccv_matrix_free(x);
65
1
}
66
67
TEST_CASE("sample up operation with source offset (10, 10)")
68
1
{
69
1
  ccv_dense_matrix_t* image = 0;
70
1
  ccv_read("../../samples/chessbox.png", &image, CCV_IO_ANY_FILE);
71
1
  ccv_dense_matrix_t* x = 0;
72
1
  ccv_sample_up(image, &x, 0, 10, 10);
73
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/chessbox.sample_up.bin", "should be down sampled (/2) image with offset from source (10, 10)");
74
1
  ccv_matrix_free(image);
75
1
  ccv_matrix_free(x);
76
1
}
77
78
TEST_CASE("blur operation with sigma 10")
79
1
{
80
1
  ccv_dense_matrix_t* image = 0;
81
1
  ccv_read("../../samples/nature.png", &image, CCV_IO_ANY_FILE);
82
1
  ccv_dense_matrix_t* x = 0;
83
1
  ccv_blur(image, &x, 0, sqrt(10));
84
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/nature.blur.bin", "should be image applied with Gaussian filter with sigma sqrt(10)");
85
1
  ccv_matrix_free(image);
86
1
  ccv_matrix_free(x);
87
1
}
88
89
TEST_CASE("flip operation")
90
1
{
91
1
  ccv_dense_matrix_t* image = 0;
92
1
  ccv_read("../../samples/chessbox.png", &image, CCV_IO_ANY_FILE);
93
1
  ccv_dense_matrix_t* x = 0;
94
1
  ccv_flip(image, &x, 0, CCV_FLIP_X);
95
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/chessbox.flip_x.bin", "flipped x-axis (around y-axis)");
96
1
  ccv_dense_matrix_t* y = 0;
97
1
  ccv_flip(image, &y, 0, CCV_FLIP_Y);
98
1
  REQUIRE_MATRIX_FILE_EQ(y, "data/chessbox.flip_y.bin", "flipped y-axis (around x-axis)");
99
1
  ccv_dense_matrix_t* xy = 0;
100
1
  ccv_flip(image, &xy, 0, CCV_FLIP_X | CCV_FLIP_Y);
101
1
  REQUIRE_MATRIX_FILE_EQ(xy, "data/chessbox.flip_xy.bin", "flipped xy-axis (rotated 180)");
102
1
  ccv_matrix_free(image);
103
1
  ccv_matrix_free(x);
104
1
  ccv_matrix_free(y);
105
1
  ccv_matrix_free(xy);
106
1
}
107
108
TEST_CASE("canny edge detector")
109
1
{
110
1
  ccv_dense_matrix_t* image = 0;
111
1
  ccv_read("../../samples/blackbox.png", &image, CCV_IO_GRAY | CCV_IO_ANY_FILE);
112
1
  ccv_dense_matrix_t* x = 0;
113
1
  ccv_canny(image, &x, 0, 3, 36, 36 * 3);
114
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/blackbox.canny.bin", "Canny edge detector on artificial image");
115
1
  ccv_matrix_free(image);
116
1
  ccv_matrix_free(x);
117
1
}
118
119
TEST_CASE("otsu threshold")
120
1
{
121
1
  ccv_dense_matrix_t* image = ccv_dense_matrix_new(6, 6, CCV_32S | CCV_C1, 0, 0);
122
  /* the test case is grabbed from: http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html */
123
1
  image->data.i32[0] = image->data.i32[1] = image->data.i32[6] = image->data.i32[22] = image->data.i32[23] = image->data.i32[28] = image->data.i32[29] = image->data.i32[35] = 0;
124
1
  image->data.i32[2] = image->data.i32[7] = image->data.i32[12] = image->data.i32[16] = image->data.i32[21] = image->data.i32[27] = image->data.i32[34] = 1;
125
1
  image->data.i32[15] = image->data.i32[26] = 2;
126
1
  image->data.i32[8] = image->data.i32[10] = image->data.i32[13] = image->data.i32[17] = image->data.i32[20] = image->data.i32[33] = 3;
127
1
  image->data.i32[3] = image->data.i32[4] = image->data.i32[9] = image->data.i32[11] = image->data.i32[14] = image->data.i32[18] = image->data.i32[19] = image->data.i32[25] = image->data.i32[32] = 4;
128
1
  image->data.i32[5] = image->data.i32[24] = image->data.i32[30] = image->data.i32[31] = 5;
129
1
  double var;
130
1
  int threshold = ccv_otsu(image, &var, 6);
131
1
  REQUIRE_EQ(threshold, 2, "threshold should be 2 (inclusive)");
132
1
  REQUIRE_EQ_WITH_TOLERANCE(var, 2.6287, 0.0001, "between class variance should be 2.6287");
133
1
  ccv_matrix_free(image);
134
1
}
135
136
TEST_CASE("erode operation")
137
1
{
138
1
  ccv_dense_matrix_t* image = 0;
139
1
  ccv_read("../../samples/chessbox.png", &image, CCV_IO_ANY_FILE);
140
1
  ccv_dense_matrix_t* x = 0;
141
1
  ccv_erode(image, &x, 0, 3);
142
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/chessbox.erode_3x3.bin", "erode with 3x3");
143
1
  ccv_dense_matrix_t* y = 0;
144
1
  ccv_erode(image, &y, 0, 5);
145
1
  REQUIRE_MATRIX_FILE_EQ(y, "data/chessbox.erode_5x5.bin", "erode with 5x5");
146
1
  ccv_matrix_free(image);
147
1
  ccv_matrix_free(x);
148
1
  ccv_matrix_free(y);
149
1
}
150
151
TEST_CASE("dilate operation")
152
1
{
153
1
  ccv_dense_matrix_t* image = 0;
154
1
  ccv_read("../../samples/chessbox.png", &image, CCV_IO_ANY_FILE);
155
1
  ccv_dense_matrix_t* x = 0;
156
1
  ccv_dilate(image, &x, 0, 3);
157
1
  REQUIRE_MATRIX_FILE_EQ(x, "data/chessbox.dilate_3x3.bin", "dilate with 3x3");
158
1
  ccv_dense_matrix_t* y = 0;
159
1
  ccv_dilate(image, &y, 0, 5);
160
1
  REQUIRE_MATRIX_FILE_EQ(y, "data/chessbox.dilate_5x5.bin", "dilate with 5x5");
161
1
  ccv_matrix_free(image);
162
1
  ccv_matrix_free(x);
163
1
  ccv_matrix_free(y);
164
1
}
165
#endif
166
167
#include "case_main.h"