XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
image.c
Go to the documentation of this file.
1/*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
24#include "image.h"
25
26static void
27col2im_add_pixel(double *im, const int height, const int width, int row,
28 int col, const int channel, const int pad, const double val)
29{
30 row -= pad;
31 col -= pad;
32 if (row < 0 || col < 0 || row >= height || col >= width) {
33 return;
34 }
35 im[col + width * (row + height * channel)] += val;
36}
37
38static double
39im2col_get_pixel(const double *im, const int height, const int width, int row,
40 int col, const int channel, const int pad)
41{
42 row -= pad;
43 col -= pad;
44 if (row < 0 || col < 0 || row >= height || col >= width) {
45 return 0;
46 }
47 return im[col + width * (row + height * channel)];
48}
49
62void
63col2im(const double *data_col, const int channels, const int height,
64 const int width, const int ksize, const int stride, const int pad,
65 double *data_im)
66{
67 const int height_col = (height + 2 * pad - ksize) / stride + 1;
68 const int width_col = (width + 2 * pad - ksize) / stride + 1;
69 const int channels_col = channels * ksize * ksize;
70 for (int c = 0; c < channels_col; ++c) {
71 const int w_offset = c % ksize;
72 const int h_offset = (c / ksize) % ksize;
73 const int c_im = c / ksize / ksize;
74 for (int h = 0; h < height_col; ++h) {
75 for (int w = 0; w < width_col; ++w) {
76 const int im_row = h_offset + h * stride;
77 const int im_col = w_offset + w * stride;
78 const int col_index = (c * height_col + h) * width_col + w;
79 const double val = data_col[col_index];
80 col2im_add_pixel(data_im, height, width, im_row, im_col, c_im,
81 pad, val);
82 }
83 }
84 }
85}
86
99void
100im2col(const double *data_im, const int channels, const int height,
101 const int width, const int ksize, const int stride, const int pad,
102 double *data_col)
103{
104 const int height_col = (height + 2 * pad - ksize) / stride + 1;
105 const int width_col = (width + 2 * pad - ksize) / stride + 1;
106 const int channels_col = channels * ksize * ksize;
107 for (int c = 0; c < channels_col; ++c) {
108 const int w_offset = c % ksize;
109 const int h_offset = (c / ksize) % ksize;
110 const int c_im = c / ksize / ksize;
111 for (int h = 0; h < height_col; ++h) {
112 for (int w = 0; w < width_col; ++w) {
113 const int im_row = h_offset + h * stride;
114 const int im_col = w_offset + w * stride;
115 const int col_index = (c * height_col + h) * width_col + w;
116 data_col[col_index] = im2col_get_pixel(
117 data_im, height, width, im_row, im_col, c_im, pad);
118 }
119 }
120 }
121}
void im2col(const double *data_im, const int channels, const int height, const int width, const int ksize, const int stride, const int pad, double *data_col)
Transforms an image vector to a column vector.
Definition image.c:100
static double im2col_get_pixel(const double *im, const int height, const int width, int row, int col, const int channel, const int pad)
Definition image.c:39
static void col2im_add_pixel(double *im, const int height, const int width, int row, int col, const int channel, const int pad, const double val)
Definition image.c:27
void col2im(const double *data_col, const int channels, const int height, const int width, const int ksize, const int stride, const int pad, double *data_im)
Transforms a column vector to an image vector.
Definition image.c:63
Image handling functions.