-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_program.cpp
150 lines (86 loc) · 2.3 KB
/
5_program.cpp
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
/*
Scanning a image
----------------
Instructions to run the code
-----------------------------
> g++ 5_program.cpp -o app `pkg-config --cflags --libs opencv`
> ./app sample_two.jpeg 20 [G]
*/
#include <bits/stdc++.h>
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
#include <sstream>
using namespace std;
using namespace cv;
Mat& ScanimageAndReduceIterator(Mat& I,const uchar* table);
int main(int argc,char* argv[]){
if (argc <3)
{
cout<< "Not enough parameters"<<endl;
return -1;
}
Mat I,J;
if(argc==4 && !strcmp(argv[3],"G"))
I=imread(argv[1],IMREAD_GRAYSCALE);
else
I = imread(argv[1],IMREAD_COLOR);
if(I.empty())
{
cout<<"The image"<<argv[1]<<"Could not be loaded"<<endl;
return -1;
}
int divideWith=0;
stringstream s;
s<<argv[2];
s>>divideWith;
if (!s || !divideWith)
{
cout<<"Invalid number entered for dividing "<<endl;
return -1;
}
//unsigned character
uchar table[256];
for(int i=0;i<256;++i)
table[i]=(uchar)(divideWith * (i/divideWith));
const int times=100;
double t;
t = (double)getTickCount();
for(int i=0;i<times;++i)
{
cv::Mat clone_i=I.clone();
J=ScanimageAndReduceIterator(clone_i,table);
}
t=1000*((double)getTickCount()-t)/getTickFrequency();
t/=times;
cout << "Time of reducing with the iterator (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl;
}
Mat& ScanimageAndReduceIterator(Mat& I,const uchar* const table)
{
CV_Assert(I.depth()==CV_8U);
const int channels = I.channels();
switch(channels)
{
case 1:
{
MatIterator_<uchar> it,end;
for(it=I.begin<uchar>(),end=I.end<uchar>(); it!=end;++it)
*it = table[*it];
break;
}
case 3:
{
MatIterator_<Vec3b> it,end;
for(it=I.begin<Vec3b>(), end = I.end<Vec3b>();it !=end;++it)
{
(*it)[0] = table[(*it)[0]];
(*it)[1] = table[(*it)[1]];
(*it)[2] = table[(*it)[2]];
}
}
}
return I;
}