-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimutils.cpp
More file actions
108 lines (93 loc) · 2.99 KB
/
imutils.cpp
File metadata and controls
108 lines (93 loc) · 2.99 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
#include "imutils.h"
cv::Mat read_image(std::string img_path)
{
// read image
return cv::imread(img_path);
}
bool save_image(cv::Mat img, std::string filename)
{
return cv::imwrite(filename, img);
}
cv::Mat translate(const cv::Mat& image, int x, int y)
{
// Define the translation matrix and perform the translation
cv::Mat shifted;
cv::Mat M = (cv::Mat_<float>(2, 3) << 1, 0, x, 0, 1, y);
cv::warpAffine(image, shifted, M, cv::Size(image.size().width, image.size().height));
// Return the translated image
return shifted;
}
cv::Mat rotate(const cv::Mat& image, double angle, cv::Point2f center, double scale)
{
// Grab the dimensions of the image
int w = image.size().width;
int h = image.size().height;
// If the center is None, initialize it as the center of
// the image
if (center == cv::Point2f(0.F, 0.F)){
center = cv::Point2f(w / 2.0F, h / 2.0F);
}
// Perform the rotation
cv::Mat rotated;
cv::Mat M = cv::getRotationMatrix2D(center, angle, scale);
cv::warpAffine(image,rotated, M, cv::Size(w, h));
// Return the rotated image
return rotated;
}
cv::Mat resize(const cv::Mat& image, int width, int height, int inter)
{
// initialize the dimensions of the image to be resized and
// grab the image size
cv::Size dim (0, 0);
int w = image.size().width;
int h = image.size().height;
// if both the width and height are None, then return the
// original image
if (!width && !height){
return image;
}
// check to see if the width is None
if (!width){
// calculate the ratio of the height and construct the
// dimensions
float r = height / float(h);
dim = cv::Size(int(w * r), height);
}
// otherwise, the height is None
else{
// calculate the ratio of the width and construct the
// dimensions
float r = width / float(w);
dim = cv::Size(width, int(h * r));
}
// resize the image
cv::Mat resized;
cv::resize(image, resized, dim, 0, 0, inter);
// return the resized image
return resized;
}
cv::Mat brightness_contrast(const cv::Mat& img, double alpha, int beta)
{
/*Adjust brightness and contrast.
Args:
alpha -- contrast coefficient (1.0 - 3.0)
beta -- brightness increment (0 - 100)
Returns:
result -- image with adjustments applied
*/
cv::Mat result = cv::Mat::zeros(img.size(), CV_8UC3);
//TODO: Use cv::addweithed instead
for( int y = 0; y < img.rows; y++){
for( int x = 0; x < img.cols; x++){
for( int c = 0; c < 3; c++){
result.at<cv::Vec3b>(y,x)[c] = cv::saturate_cast<uchar>(alpha * (img.at<cv::Vec3b>(y,x)[c]) + beta);
}
}
}
return result;
}
cv::Mat kernel_constr(int size)
{
cv::Mat kernel = cv::Mat::ones(size, size, CV_8UC1);
return kernel;
}