-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resize.cpp
177 lines (139 loc) · 4.22 KB
/
Resize.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include<algorithm>
#include <memory>
using namespace cv;
using namespace std;
//1724行
void InterNearestResize(const Mat &src, Mat &dst, Size &dsize, double fx = 0.0, double fy = 0.0)//原始图形以及缩放比例
{
Size ssize = src.size();//获取矩阵大小
CV_Assert(ssize.area() > 0);//保证矩阵的长宽都大于0
if (!dsize.area())//如果dsize为(0,0)
{
dsize = Size(saturate_cast<int>(src.cols*fx),//satureate_cast防止数据溢出
saturate_cast<int>(src.rows*fy));
CV_Assert(dsize.area());
}
else
{
fx = (double)dsize.width / src.cols;//Size中的宽高和mat中的行列是相反的
fy = (double)dsize.height / src.rows;
}
dst.create(Size(src.cols*fy,src.rows*fx), src.type());//将dst矩阵大小变为想要的尺寸
fx = 1.0 / fx;
fy = 1.0 / fy;
uchar *ps = src.data;
uchar *pd = dst.data;
int channels = src.channels(),x,y,out,in;
for (int row = 0; row < dst.rows; row++)
{
x = cvFloor(row * fx);
for (int col = 0; col < dst.cols; col++)
{
y = cvFloor(col * fy);
for (int c = 0; c < channels; c++)
{
out = (row * dst.cols + col) * channels + c;
in = (x * src.cols + y) * channels + c;
pd[out] = ps[in];
}
//dst.at<Vec3b>(row, col) = src.at<Vec3b>(x, y);
}
}
}
//(X, Y) = (1 - u)(1 - v)(x, y) + (u - 1)v(x, y + 1) + u(v - 1)(x + 1, y) + (uv)(x, y)
void InterLinerResize(const Mat &src, Mat &dst, Size &dsize, double fx = 0.0, double fy = 0.0)//双线性插值
{
Size ssize = src.size();//获取矩阵大小
CV_Assert(ssize.area() > 0);//保证矩阵的长宽都大于0
if (!dsize.area())//如果dsize为(0,0)
{
dsize = Size(saturate_cast<int>(src.cols*fx),//satureate_cast防止数据溢出
saturate_cast<int>(src.rows*fy));
CV_Assert(dsize.area());
}
else
{
fx = (double)dsize.width / src.cols;//Size中的宽高和mat中的行列是相反的
fy = (double)dsize.height / src.rows;
}
dst.create(dsize, src.type());
double ifx = 1. / fx;
double ify = 1. / fy;
uchar* dp = dst.data;
uchar* sp = src.data;
int iWidthSrc = src.cols;//宽(列数)
int iHiehgtSrc = src.rows;//高(行数)
int channels = src.channels();
short cbufy[2];
short cbufx[2];
for (int row = 0; row < dst.rows; row++)
{
float fy = (float)((row + 0.5) * ify - 0.5);
int sy = cvFloor(fy);//整数部分
fy -= sy;//小数部分
sy = std::min(sy, iHiehgtSrc - 2);
sy = std::max(0, sy);
cbufy[0] = cv::saturate_cast<short>((1.f - fy) * 2048);//1-u
cbufy[1] = 2048 - cbufy[0];//u
for (int col = 0; col < dst.cols; col++)
{
float fx = (float)((col + 0.5) * ifx - 0.5);
int sx = cvFloor(fx);
fx -= sx;
if (sx < 0)
{
fx = 0, sx = 0;
}
if (sx >= iWidthSrc - 1)
{
fx = 0, sx = iWidthSrc - 2;
}
cbufx[0] = cv::saturate_cast<short>((1.f - fx) * 2048);//1-v
cbufx[1] = 2048 - cbufx[0];//v
for (int k = 0; k < src.channels(); ++k)
{
dp[(row * dst.cols + col) * channels + k] =
(
sp[ ( sy * src.cols + sx ) * channels + k] * cbufx[0] * cbufy[0] +
sp[((sy + 1) * src.cols + sx) * channels + k] * cbufx[0] * cbufy[1] +
sp[(sy * src.cols + (sx + 1)) * channels + k] * cbufx[1] * cbufy[0] +
sp[((sy + 1) * src.cols + (sx + 1)) * channels + k] * cbufx[1] * cbufy[1]
) >> 22;//2048*2048
}
}
}
}
void Copy(Mat src, Mat &dst)//将src copy到 dst
{
for (int row = 0; row < src.rows; row++)
{
//uchar *p = dst.ptr<uchar>(row);
//uchar *s = src.ptr<uchar>(row);
for (int col = 0; col < src.cols; col++)
{
//p[col] = s[col];
dst.at<Vec3b>(row, col)[0] = src.at<Vec3b>(row, col)[0];
dst.at<Vec3b>(row, col)[1] = src.at<Vec3b>(row, col)[1];
dst.at<Vec3b>(row, col)[2] = src.at<Vec3b>(row, col)[2];
}
}
}
int main()
{
ios::sync_with_stdio(false);
Mat src = imread("src.jpg", IMREAD_UNCHANGED),out;//870*665
InterLinerResize(src, out, Size(0, 0), 0.7, 0.5);
imshow("src", src);
//Copy(src, out);
//InterNearestResize(src, out, Size(1000, 960));
//InterLinerResize(src, out, Size(1080, 960));
//resize(src, out,Size(1080,960),0);
imshow("out", out);
waitKey();
system("pause");
return 0;
}