|
| 1 | +#include "util.h" |
| 2 | +#include <iostream> |
| 3 | +#include <cmath> |
| 4 | + |
| 5 | +// for 2d images |
| 6 | +template<typename T> |
| 7 | +T get_pixel(const T * data, int height, int width, int h, int w) |
| 8 | +{ |
| 9 | + return data[h * width + w]; |
| 10 | +} |
| 11 | + |
| 12 | +template<typename T> |
| 13 | +std::vector<T> get_pixel_vector(const T * data, int height, int width, int channel, int h, int w) |
| 14 | +{ |
| 15 | + std::vector<T> pixel_vector(channel); |
| 16 | + for (int c = 0; c < channel; c++){ |
| 17 | + pixel_vector[c]= data[h * width * channel + w * channel + c]; |
| 18 | + } |
| 19 | + return pixel_vector; |
| 20 | +} |
| 21 | + |
| 22 | +template<typename T> |
| 23 | +void set_pixel(T * data, int height, int width, int h, int w, T value) |
| 24 | +{ |
| 25 | + data[h * width + w] = value; |
| 26 | +} |
| 27 | + |
| 28 | +template<typename T> |
| 29 | +float get_l2_distance(std::vector<T> p1, std::vector<T> p2) |
| 30 | +{ |
| 31 | + T sq_sum = 0.0; |
| 32 | + for(int d = 0; d < p1.size(); d++) |
| 33 | + { |
| 34 | + sq_sum = sq_sum + (p1[d] - p2[d]) * (p1[d] - p2[d]); |
| 35 | + } |
| 36 | + float dis = sqrt(sq_sum); |
| 37 | + return dis; |
| 38 | +} |
| 39 | + |
| 40 | +template |
| 41 | +float get_pixel<float>(const float * data, int height, int width, int h, int w); |
| 42 | + |
| 43 | +template |
| 44 | +int get_pixel<int>(const int * data, int height, int width, int h, int w); |
| 45 | + |
| 46 | +template |
| 47 | +std::vector<float> get_pixel_vector<float>(const float * data, int height, int width, int channel, int h, int w); |
| 48 | + |
| 49 | +template |
| 50 | +unsigned char get_pixel<unsigned char>(const unsigned char * data, int height, int width, int h, int w); |
| 51 | + |
| 52 | + |
| 53 | +template |
| 54 | +void set_pixel<float>(float * data, int height, int width, int h, int w, float value); |
| 55 | + |
| 56 | +template |
| 57 | +void set_pixel<int>(int * data, int height, int width, int h, int w, int value); |
| 58 | + |
| 59 | +template |
| 60 | +void set_pixel<unsigned char>(unsigned char * data, int height, int width, int h, int w, unsigned char value); |
| 61 | + |
| 62 | +template |
| 63 | +float get_l2_distance(std::vector<float> p1, std::vector<float> p2); |
| 64 | + |
| 65 | + |
| 66 | +// for 3d images |
| 67 | +template<typename T> |
| 68 | +T get_pixel(const T * data, int depth, int height, int width, int d, int h, int w) |
| 69 | +{ |
| 70 | + return data[(d*height + h) * width + w]; |
| 71 | +} |
| 72 | + |
| 73 | +template<typename T> |
| 74 | +std::vector<T> get_pixel_vector(const T * data, int depth, int height, int width, int channel, int d, int h, int w) |
| 75 | +{ |
| 76 | + std::vector<T> pixel_vector(channel); |
| 77 | + for (int c = 0; c < channel; c++){ |
| 78 | + pixel_vector[c]= data[d*height*width*channel + h * width * channel + w * channel + c]; |
| 79 | + } |
| 80 | + return pixel_vector; |
| 81 | +} |
| 82 | + |
| 83 | +template<typename T> |
| 84 | +void set_pixel(T * data, int depth, int height, int width, int d, int h, int w, T value) |
| 85 | +{ |
| 86 | + data[(d*height + h) * width + w] = value; |
| 87 | +} |
| 88 | + |
| 89 | +template |
| 90 | +float get_pixel<float>(const float * data, int depth, int height, int width, int d, int h, int w); |
| 91 | + |
| 92 | +template |
| 93 | +std::vector<float> get_pixel_vector<float>(const float * data, int depth, int height, int width, int channel, int d, int h, int w); |
| 94 | + |
| 95 | +template |
| 96 | +int get_pixel<int>(const int * data, int depth, int height, int width, int d, int h, int w); |
| 97 | + |
| 98 | +template |
| 99 | +unsigned char get_pixel<unsigned char>(const unsigned char * data, |
| 100 | + int depth, int height, int width, |
| 101 | + int d, int h, int w); |
| 102 | + |
| 103 | + |
| 104 | +template |
| 105 | +void set_pixel<float>(float * data, int depth, int height, int width, int d, int h, int w, float value); |
| 106 | + |
| 107 | +template |
| 108 | +void set_pixel<int>(int * data, int depth, int height, int width, int d, int h, int w, int value); |
| 109 | + |
| 110 | +template |
| 111 | +void set_pixel<unsigned char>(unsigned char * data, |
| 112 | + int depth, int height, int width, |
| 113 | + int d, int h, int w, unsigned char value); |
| 114 | + |
0 commit comments