-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOCVWrapper.hpp
129 lines (127 loc) · 2.73 KB
/
OCVWrapper.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
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
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <vector>
#define OCVWrapper_HPP
class OCVWrapper
{
private:
cv::Mat img;
public:
OCVWrapper(std::string path, bool color)
{
img = cv::imread(path, color);
if(!img.rows && !img.cols)
{
std::cout << "Image cannot be loaded, exiting...\n";
exit(1);
}
}
OCVWrapper(int width, int height, bool color)
{
if(color)
img = cv::Mat(height, width, CV_8UC3, cv::Scalar(0,0,0));
else
img = cv::Mat(height, width, CV_8UC1, cv::Scalar(0));
}
bool inBounds(cv::Point p) const
{
if(p.y >= img.rows || p.y < 0)
return false;
else if(p.x >= img.cols || p.x < 0)
return false;
else
return true;
}
~OCVWrapper()
{
img.release();
}
void loadImg(std::string path, bool color)
{
img = cv::imread(path, color);
if(!img.rows && !img.cols)
{
std::cout << "Image cannot be loaded, exiting...\n";
exit(1);
}
}
void showImg(std::string windowName) const
{
cv::imshow(windowName, img);
}
void update(int wait) const
{
cv::waitKey(wait);
}
void clear()
{
if(img.channels() == 1)
img = cv::Scalar(0);
else if(img.channels() == 3)
img = cv::Scalar(0,0,0);
}
template <class T>
T getPixel(cv::Point p) const
{
if(!inBounds(p))
return -1;
return img.at<T>(p.y, p.x);
/*else if(img.channels() == 1)
return img.at<uchar>(p.y, p.x);
else if(img.channels() == 3)
return img.at<cv::Vec3b>(p.y, p.x);*/
}
template <class T>
void setPixel(cv::Point p, T pixelValue)
{
if(!inBounds(p))
return;
img.at<T>(p.y, p.x) = pixelValue;
/*else if(img.channels() == 1)
img.at<uchar>(p.y, p.x) = pixelValue;
else if(img.channels() == 3)
img.at<cv::Vec3b>(p.y, p.x) = pixelValue;*/
}
int getWidth() const
{
return img.cols;
}
int getHeight() const
{
return img.rows;
}
void scaleImg(float scale)
{
cv::Size sz(img.cols*scale, img.rows*scale);
cv::resize(img, img, sz);
}
void getContours(std::vector<cv::Vec4i> &hierarchy, std::vector< std::vector<cv::Point> > &contours)
{
findContours(img, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
}
template <class T>
void drawPoly(std::vector< std::vector<cv::Point> > polygons, T color)
{
cv::fillPoly(img, polygons, color);
}
template <class T>
void drawPolyLines(std::vector< std::vector<cv::Point> > polygons, T color)
{
cv::polylines(img, polygons, true, color);
}
template <class T>
void drawContours(std::vector<std::vector<cv::Point> > contours, T color, int lineType)
{
for(int i = 0; i < contours.size(); ++i)
cv::drawContours(img, contours, i, color, lineType);
}
void toGray()
{
cv::cvtColor(img, img, CV_BGR2GRAY);
}
int getChannels()
{
return img.channels();
}
};