00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00047
00048
00049
00050 #include <set>
00051 #include <string>
00052 #include "errormacros.h"
00053 #include "CImg.h"
00054 #include "Image.h"
00055 #include "cimg_dependent.h"
00056 #include "simple_tools.h"
00057
00058
00059
00060
00061 using namespace std;
00062 using namespace cimg_library;
00063 using namespace lsseg;
00064
00065
00066 namespace
00067
00068 {
00069
00070
00071 void image2cimg(const Image<double>& img, CImg<double>& target, int z = -1)
00072 {
00073 if (z < 0) {
00074 CImg<double> tmpImg(img.dimx(), img.dimy(), img.dimz(), img.numChannels());
00075 copy(img.begin(), img.end(), tmpImg.data);
00076 tmpImg.swap(target);
00077 } else {
00078 CImg<double> tmpImg(img.dimx(), img.dimy(), 1, img.numChannels());
00079 for (int c = 0; c < img.numChannels(); ++c) {
00080
00081 unsigned int offset = c * img.channelSize() + z * img.graySize2D();
00082
00083 copy(img.begin() + offset,
00084 img.begin() + offset + img.graySize2D(),
00085 tmpImg.data + c * img.graySize2D());
00086 }
00087 tmpImg.swap(target);
00088 }
00089 }
00090
00091 template<typename T>
00092 void cimg2image(const CImg<T>& img, Image<T>& target)
00093 {
00094 target.resize(img.dimx(), img.dimy(), img.dimz(), img.dimv());
00095 copy(img.data, img.data + img.size(), target.begin());
00096 }
00097
00098 };
00099
00100
00101 namespace lsseg {
00102
00103
00104 void load_image(const char* name, Image<double>& target, bool convert_to_grayscale)
00105
00106 {
00107 CImg<double> tmp(name);
00108 cimg2image(tmp, target);
00109 if (convert_to_grayscale) {
00110 to_grayscale(target);
00111 }
00112 }
00113
00114
00115 void load_image(const char* name, Image<int>& target, bool convert_to_grayscale)
00116
00117 {
00118 CImg<int> tmp(name);
00119 cimg2image(tmp, target);
00120 if (convert_to_grayscale) {
00121 to_grayscale(target);
00122 }
00123 }
00124
00125
00126 void save_image(const char* name, const Image<double>& img)
00127
00128 {
00129 CImg<double> tmp(1, 1, 1, 1);
00130 image2cimg(img, tmp);
00131 tmp.save(name);
00132 }
00133
00134
00135 void display_image(const Image<double>& img, int z)
00136
00137 {
00138
00139
00140 CImg<double> tmp(1, 1, 1, 1);
00141 image2cimg(img, tmp, z);
00142 tmp.display();
00143
00144 }
00145
00146
00147
00148
00149 void permanent_display(const Image<double>& img)
00150
00151 {
00152
00153
00154
00155
00156 CImg<double> tmp(1, 1, 1, 1);
00157 image2cimg(img, tmp);
00158 new CImgDisplay(tmp);
00159 }
00160
00161
00162 void blur_image(Image<double>& img, double rho)
00163
00164 {
00165 CImg<double> tmp(1,1,1,1);
00166 image2cimg(img,tmp);
00167 tmp.blur(rho);
00168 cimg2image(tmp, img);
00169 }
00170
00171
00172 void blur_1D(double* data, unsigned int data_size, double rho)
00173
00174 {
00175 CImg<double> tmp(data_size, 1, 1, 1);
00176 std::copy(data, data + data_size, tmp.data);
00177 tmp.blur(rho);
00178 std::copy(tmp.data, tmp.data + data_size, data);
00179 }
00180
00181
00182 void gaussian_noise(Image<double>& img, double sigma)
00183
00184 {
00185 CImg<double> tmp(1,1,1,1);
00186 image2cimg(img,tmp);
00187 tmp.noise(sigma, 0);
00188 cimg2image(tmp, img);
00189 }
00190
00192
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00212 struct Pimpl
00213
00214 {
00215 Pimpl(const Image<double>& img, const char* name) :
00216 disp_(img.dimx(), img.dimy(), name), name_(name)
00217 {
00218 update(img, false);
00219 }
00220
00221 void update(const Image<double>& img, bool reshape) {
00222 image2cimg(img, i_);
00223 if (disp_.is_resized) {
00224 resize();
00225 } else {
00226 disp_.display(i_);
00227 }
00228 if (reshape) {
00229 resize(img.dimx(), img.dimy());
00230 }
00231 disp_.show();
00232 }
00233
00234 void callbackLoop()
00235 {
00236 while(true) {
00237 int mouse_state = disp_.button;
00238 if (mouse_state & 1 ) {
00239 int x, y;
00240 getMouseXY(x, y);
00241 cout << "(" << x << ", " << y << ") : " << endl;
00242 printColorInfo(x, y);
00243 set<Pimpl*>::iterator it;
00244 for (it = connecteds_.begin(); it != connecteds_.end(); ++it) {
00245 (*it)->printColorInfo(x, y);
00246 }
00247 cout << endl;
00248 disp_.wait();
00249 }
00250 if (mouse_state & 2 || disp_.key == cimg::keySPACE) {
00251 disp_.key =0;
00252 break;
00253 }
00254 if (mouse_state & 4) {
00255 cout << "no function for the middle button yet" << endl;
00256 }
00257 if (disp_.is_resized) {
00258 resize();
00259 set<Pimpl*>::iterator it;
00260 for (it = connecteds_.begin(); it != connecteds_.end(); ++it) {
00261 (*it)->resize(disp_.window_width, disp_.window_height);
00262 }
00263 }
00264 }
00265 }
00266
00267 void connect(Pimpl* rhs) {
00268 connecteds_.insert(rhs);
00269 rhs->connecteds_.insert(this);
00270 }
00271
00272 void resize(int x = -1, int y = -1)
00273 {
00274 if (x > 0 && y > 0) {
00275 disp_.resize(x, y);
00276 } else {
00277 disp_.resize();
00278 }
00279 disp_.display(i_);
00280 }
00281
00282 private:
00283
00284 CImg<double> i_;
00285 CImgDisplay disp_;
00286 string name_;
00287 set<Pimpl*> connecteds_;
00288
00289 void getMouseXY(int& x, int& y)
00290 {
00291 x = disp_.mouse_x;
00292 y = disp_.mouse_y;
00293
00294 x *= i_.dimx();
00295 y *= i_.dimy();
00296
00297 x /= disp_.window_width;
00298 y /= disp_.window_height;
00299 }
00300
00301 void printColorInfo(int x, int y) {
00302 cout << name_ << ": ";
00303 cout << "[ ";
00304 for (int i = 0; i < i_.dimv(); ++i) {
00305 cout << i_(x, y, 0, i) << " ";
00306 }
00307 cout << "]" << endl;
00308 }
00309 };
00311
00312
00313 UpdatableImage::UpdatableImage(const Image<double>& img,
00314 const char* name) : p_(new Pimpl(img, name)) {}
00315 UpdatableImage::~UpdatableImage() { delete p_;}
00316 void UpdatableImage::update(const Image<double>& img, bool reshape) { p_->update(img, reshape);}
00317 void UpdatableImage::interact() { p_->callbackLoop();}
00318 void UpdatableImage::connect(UpdatableImage& rhs) { p_->connect(rhs.p_);}
00319
00320
00321 };