Loading...
Searching...
No Matches
SomUtils.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef SOM_UTILS_H_
32#define SOM_UTILS_H_
33
34
35/*
36 g++ drain/examples/Som-example.cpp drain/util/{Caster,Variable,Debug}.cpp drain/image/{Geometry,Coordinates,Image,FilePng}.cpp -lpng -o Som-example
37 */
38#include <drain/image/ImageFile.h>
39#include <iostream>
40#include <vector>
41#include <list>
42#include <limits>
43#include <sstream>
44
45//
46#include <stdlib.h>
47//#include <cmath>
48
49#include "drain/Variable.h"
50#include "drain/image/Image.h"
51
52
53namespace drain {
54
69// Utilities
70/*
71template <class T>
72static
73double Som<vector<T> >::defaultNeighborhoodFunction(const int & i, const int & j) const {
74 return radius2 / (radius2 + i*i + j*j);
75}
76*/
77
78/*
79template <class T2>
80void uniformRandomVector(vector<T2> & x) {
81
82 for (int k = 0; k < x.size(); ++k)
83 x[k] = static_cast<T2>(rand() & 0xff);
84 //x[k] = static_cast<T2>(rand() & 0xffff)/static_cast<T2>(0xf00);
85 //x[2] = 101;
86
87}
88*/
89
90
92/*
93 For vectors and str objects having
94
95template <class T>
96double euclideanDistance2(const T & x1, const T & x2) {
97
98 double d, result = 0.0;
99
100 for (int i = 0; i < x1.size(); ++i) {
101 d = x1[i]-x2[i];
102 result += d*d;
103 }
104
105 return result;
106
107}
108*/
109
110
111/*
112template <class T>
113void vectorMix(const T & x1, const T & x2, double coeff, T & m){ // todo iterate
114
115 for (int k = 0; k < x1.size(); ++k)
116 m[k] = (1.0-coeff)*x1[k] + coeff*x2[k];
117
118}
119*/
120
122template <class T>
123void somToImage(const Som< T > & som, drain::image::Image & image, int width=0, int height=0, const std::vector<int> & channels = std::vector<int>()){
124
125 const int depth = som.map[0][0].size();
126
127 std::vector<int> defaultChannels;
128
129 const std::vector<int> & applyChannels = channels.empty() ? defaultChannels : channels;
130
131 if (channels.empty()){
132 defaultChannels.resize(depth);
133 for (int k = 0; k < depth; ++k) {
134 defaultChannels[k] = k;
135 }
136 }
137
138 const int n = applyChannels.size();
139
140 if ((n != 1) && (n != 3)){
141 std::cerr << "Warning: som2PNM: dimension not 1 (gray) or 3 (rgb) " << std::endl;
142 return;
143 }
144
145 const int w = (width > 0) ? width : som.getWidth();
146 const int h = (height > 0) ? height : som.getHeight();
147
148 image.setGeometry(w, h, n);
149 /*
150 cout << "Writing image of " << image.getGeometry() << endl;
151 for (int k = 0; k < n; ++k)
152 cout << applyChannels[k] << ' ';
153 cout << endl;
154 */
155
156
157 int iMap, jMap;
158 for (int j = 0; j < h; ++j) {
159 jMap = (j * som.getHeight()) / h;
160 for (int i = 0; i < w; ++i) {
161 iMap = (i * som.getWidth()) / w;
162 for (int k = 0; k < n; ++k) {
163 image.put(i,j,k, som.map[jMap][iMap][applyChannels[k]]);
164 //image.put(i,j,k, som.map[jMap][iMap][k]);
165 //image.put(i,j,);
166 }
167 }
168 }
169
170}
171
172
173
174
175
176
177
178} // drain
179
180
181
182/* namespace drain */
183
184#endif /* SOM_H_ */
185
186// Drain
Definition Som.h:65
int getHeight() const
The number of rows in the map.
Definition Som.h:138
std::vector< std::vector< T > > map
The actual neural network, two-dimensional map of units.
Definition Som.h:241
int getWidth() const
The number of columns in the map.
Definition Som.h:144
void put(size_t i, T x)
Sets the intensity in location i to x. See \address.
Definition ImageFrame.h:192
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:184
virtual void setGeometry(size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0)
Resizes the image, keeps the current type.
Definition Image.h:95
Definition DataSelector.cpp:1277
void somToImage(const Som< T > &som, drain::image::Image &image, int width=0, int height=0, const std::vector< int > &channels=std::vector< int >())
Squared Euclidean distance for vectors and similar.
Definition SomUtils.h:123