-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_counter.cpp
More file actions
141 lines (123 loc) · 5.45 KB
/
object_counter.cpp
File metadata and controls
141 lines (123 loc) · 5.45 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "busybox.hpp"
#include "files.hpp"
#include "lib-png.hpp"
#include "tpoint.hpp"
#include "tstack.hpp"
#include "fonts.hpp"
#include "numerical.hpp"
#include <random>
#include <algorithm>
#include <iostream>
#include <map>
#include <chrono>
//shuffle (foo.begin(), foo.end(), std::default_random_engine(seed));
std::map<rcl::upoint,int> count(const rcl::image32& src,const rcl::pixel32& bg);
void floodfillcount(std::map<rcl::upoint,int>& catalog,const int& id,const rcl::upoint& sp,const rcl::image32& src,const rcl::pixel32& bg);
int object_counter_test_main(int argc,char **argv){
std::string infile,outfile;
infile = std::string("./resources/test-circles.png");
outfile = std::string("test-circles-colour.png");
if (argc>2){
infile = std::string(argv[1]);
outfile = std::string(argv[2]);
}
std::chrono::high_resolution_clock::time_point t1,t2;
std::chrono::duration<double> time_span;
std::map<rcl::upoint,int> recon;
std::vector<rcl::fpoint> centers;
std::vector<std::vector<rcl::fpoint> >coordseq;
std::vector<unsigned char> data;
rcl::image32 img,dst;
rcl::upoint sz;
std::vector<rcl::pixel32> palette;
unsigned int counter=0;
t1 = std::chrono::high_resolution_clock::now();
rcl::load_file(data,infile);
t2 = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
std::cout << "Lectura de disco: " << time_span.count() << " s." << std::endl;
t1 = std::chrono::high_resolution_clock::now();
rcl::decode_png(img,data);
t2 = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
std::cout << "Decodificacion en RAM: " << time_span.count() << " s."<< std::endl;
sz = img.GetXY();
t1 = std::chrono::high_resolution_clock::now();
std::map<rcl::upoint,int> catalog = count(img,rcl::pixel32(255,255,255));
t2 = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
std::cout << "Reconocimiento: " << time_span.count() << " s."<< std::endl;
t1 = std::chrono::high_resolution_clock::now();
for (auto itr=catalog.begin();itr!=catalog.end();itr++) if (itr->second>counter) counter=itr->second;
t2 = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
std::cout << "Cuento de puntos: " << time_span.count() << " s."<< std::endl;
t1 = std::chrono::high_resolution_clock::now();
palette.push_back(rcl::pixel32(255,255,255));
std::default_random_engine generator(counter);
std::uniform_int_distribution<int> distribution(0,255);
for (unsigned int idx=0;idx<counter;idx++)
palette.push_back(rcl::pixel32(distribution(generator),distribution(generator),distribution(generator)));
t2 = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
std::cout << "Generacion de paleta: " << time_span.count() << " s."<< std::endl;
dst.fit(sz);
t1 = std::chrono::high_resolution_clock::now();
for (auto itr=catalog.begin();itr!=catalog.end();itr++) dst[itr->first]=palette[itr->second];
t2 = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
std::cout << "Dibujado de la salida: " << time_span.count() << " s."<< std::endl;
t1 = std::chrono::high_resolution_clock::now();
data.clear();
rcl::encode_png(data,dst);
rcl::save_file(outfile,data);
t2 = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
std::cout << "Codificando en PNG la salida: " << time_span.count() << " s."<< std::endl;
return 0;
}
static rcl::reg_app reg("object_counter_test",object_counter_test_main);
std::map<rcl::upoint,int> count(const rcl::image32& src,const rcl::pixel32& bg){
int counter=1;
std::map<rcl::upoint,int> catalog;
rcl::upoint size(src.GetX(),src.GetY());
for (unsigned int idy=0;idy<size.y();idy++)
for (unsigned int idx=0;idx<size.x();idx++)
{
if (catalog.find(rcl::upoint(idx,idy))==catalog.end()){
if (src(rcl::upoint(idx,idy))==bg)
catalog[rcl::upoint(idx,idy)]=0;
else{
floodfillcount(catalog,counter,rcl::upoint(idx,idy),src,bg); counter++;
}
}
}
return catalog;
}
void floodfillcount(std::map<rcl::upoint,int>& catalog,
const int& id,
const rcl::upoint& sp,
const rcl::image32& src,
const rcl::pixel32& bg){
rcl::tstack<rcl::upoint> stk;
rcl::upoint p;
rcl::upoint size(src.GetX(),src.GetY());
stk.push(sp);
while(stk.pop(p)){
catalog[p]=id;
rcl::upoint neighbor;
neighbor=p;
if (p.x()==0) neighbor.x()=size.x()-1; else neighbor.x()--;
if (catalog.find(neighbor)==catalog.end()) if (src(neighbor)!=bg) stk.push(neighbor);
neighbor=p;
if (p.x()==size.x()-1) neighbor.x()=0; else neighbor.x()++;
if (catalog.find(neighbor)==catalog.end()) if (src(neighbor)!=bg) stk.push(neighbor);
neighbor=p;
if (p.y()==0) neighbor.y()=size.y()-1; else neighbor.y()--;
if (catalog.find(neighbor)==catalog.end()) if (src(neighbor)!=bg) stk.push(neighbor);
neighbor=p;
if (p.y()==size.y()-1) neighbor.y()=0; else neighbor.y()++;
if (catalog.find(neighbor)==catalog.end()) if (src(neighbor)!=bg) stk.push(neighbor);
}
return;
}