-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_image.hpp
50 lines (41 loc) · 1.22 KB
/
load_image.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "matvec.hpp"
#include "tensor.hpp"
#include <iostream>
#define cimg_display 0
#include "CImg/CImg.h"
#include <vector>
std::vector<std::vector<int>> vect;
using namespace cimg_library;
template< typename ComponentType >
Matrix< ComponentType > load_image(const std::string& filename)
{
CImg<ComponentType>image(filename.c_str());
size_t image_w = image.width();
size_t image_h = image.height();
//std::cout <<"load image" << image_w << "*" << image_h;
Matrix< ComponentType > loaded_matrix(image_h, image_w, 0);
for (size_t i = 0;i < image_w;i++) {
for (size_t j = 0;j < image_h;j++) {
loaded_matrix(j, i) = image(i, j)/255;
}
}
return loaded_matrix;
};
template< typename ComponentType >
void save_image(Matrix<ComponentType>& input_matrix ,std::string filename)
{
size_t image_h = input_matrix.rows();
size_t image_w = input_matrix.cols();
CImg<unsigned char>image(image_w, image_h,1,1,255);
for (size_t i = 0;i < image_w; i++) {
for (size_t j = 0;j < image_h; j++) {
image(i, j) = input_matrix(j,i)*255;
}
}
//std::cout <<"save Image" << image_h << "*" << image_w;
CImgDisplay main_disp(image, "Click a point");
image.save(filename.c_str());
while (!main_disp.is_closed()) {
main_disp.wait();
}
};