-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
266 lines (224 loc) · 7.6 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
Mat equalizeIntensity(const Mat& inputImage)
{
if(inputImage.channels() >= 3)
{
Mat ycrcb;
cvtColor(inputImage,ycrcb,CV_BGR2YCrCb);
vector<Mat> channels;
split(ycrcb,channels);
equalizeHist(channels[0], channels[0]);
Mat result;
merge(channels,ycrcb);
cvtColor(ycrcb,result,CV_YCrCb2BGR);
return result;
}
return Mat();
}
bool energy_map(Mat& src, Mat& grad){
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Mat dst = src.clone();
// GaussianBlur( src, dst, Size(11,11), 0, 0, BORDER_DEFAULT );
// dst = equalizeIntensity(dst);
// dst = equalizeIntensity(dst);
// dst = equalizeIntensity(dst);
//Scharr( dst, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( src, grad_x, ddepth, 1, 0, 3, BORDER_DEFAULT );
//Scharr( dst, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( src, grad_y, ddepth, 0, 1, 3, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
convertScaleAbs( grad_y, abs_grad_y );
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
//Mat channel[3];
//split(grad, channel);
cvtColor( grad, grad, COLOR_BGR2GRAY );
//grad = (channel[0] + channel[1] + channel[2])/3;
return true;
}
int diff(int a,int b){
if(a-b >= 0) return a -b;
return -1 * (a-b);
}
bool seam_dp(Mat& grad, Mat& seamed_image){
if(grad.empty()) return -1;
seamed_image = grad.clone();
int rows = grad.rows;
int cols = grad.cols;
for(int i=0;i<cols;i++) seamed_image.at<uchar>(0,i) = grad.at<uchar>(0,i);
uchar left,right,middle;
for(int i=1;i<rows;i++){
for(int j=0;j<cols;j++){
if(j > 0) left = seamed_image.at<uchar>(i-1,j-1)+diff(grad.at<uchar>(i,j-1),grad.at<uchar>(i,j+1))+diff(grad.at<uchar>(i,j-1),grad.at<uchar>(i-1,j));
else left = 255;
if(j < cols-1) right = seamed_image.at<uchar>(i-1,j+1)+diff(grad.at<uchar>(i,j-1),grad.at<uchar>(i,j+1))+diff(grad.at<uchar>(i,j+1),grad.at<uchar>(i-1,j));
else right = 255;
middle = seamed_image.at<uchar>(i-1,j)+diff(grad.at<uchar>(i,j-1),grad.at<uchar>(i,j+1));
seamed_image.at<uchar>(i,j) = seamed_image.at<uchar>(i,j) + min(middle,min(left,right));
}
}
return true;
}
bool remove_col(Mat& output_image,Mat& seamed_image,Mat& colored_image){
int rows = output_image.rows;
int cols = output_image.cols;
Mat reduce_image = Mat(rows, cols-1, CV_8UC3);
uchar min_point = seamed_image.at<uchar>(rows-1,0);
int k=0;
for(int j=1;j<cols;j++){
if(min_point > seamed_image.at<uchar>(rows-1,j)){
min_point = seamed_image.at<uchar>(rows-1,j);
k = j;
}
}
//cout<<k<<endl;
colored_image.at<Vec3b>(rows-1,k) = Vec3b(255,255,255);
for(int j=0;j<k;j++) reduce_image.at<Vec3b>(rows-1,j)=output_image.at<Vec3b>(rows-1,j);
for(int j=k+1;j<cols;j++) reduce_image.at<Vec3b>(rows-1,j-1)=output_image.at<Vec3b>(rows-1,j);
uchar left,right,middle;
for(int i=rows-2;i>=0;i--){
if(k > 0) left = seamed_image.at<uchar>(i,k-1);
else left = 255;
if(k < cols-1) right = seamed_image.at<uchar>(i,k+1);
else right = 255;
middle = seamed_image.at<uchar>(i,k);
uchar new_min = min(middle,min(left,right));
//cout<<int(left)<<" "<<int(right)<<" "<<int(middle)<<" "<<int(new_min)<<endl;
if(left == new_min) k = k-1;
else if(right == new_min) k = k+1;
colored_image.at<Vec3b>(i,k) = Vec3b(255,255,255);
// colored_image.at<Vec3b>(i,k)[1] = 255;
// colored_image.at<Vec3b>(i,k)[2] = 255;
for(int j=0;j<k;j++) reduce_image.at<Vec3b>(i,j)=output_image.at<Vec3b>(i,j);
for(int j=k+1;j<cols;j++) reduce_image.at<Vec3b>(i,j-1)=output_image.at<Vec3b>(i,j);
}
output_image = reduce_image.clone();
return true;
}
bool reduce_image(Mat& in_image,Mat& output_image,int new_width,int new_height,Mat& colored_image){
if(new_width>in_image.cols){
cout<<"Invalid request!!! new_width has to be smaller than the current size!"<<endl;
return false;
}
if(new_height>in_image.rows){
cout<<"Invalid request!!! ne_height has to be smaller than the current size!"<<endl;
return false;
}
if(new_width<=0){
cout<<"Invalid request!!! new_width has to be positive!"<<endl;
return false;
}
if(new_height<=0){
cout<<"Invalid request!!! new_height has to be positive!"<<endl;
return false;
}
output_image = in_image.clone();
colored_image = in_image.clone();
int counter = 0,count = 20;
int even = 1;
while(output_image.cols!=new_width || output_image.rows!=new_height){
if(output_image.cols > new_width){
Mat seamed_image,grad;
if(even%2==0){
rotate(output_image, output_image, 1);
rotate(colored_image, colored_image, 1);
// imshow("rotated",output_image);
// waitKey(0);
}
energy_map(output_image,grad);
if(counter==count){
//imshow("grad",grad);
//waitKey(0);
if(count==20) count = 21;
else count = 20;
}
seam_dp(grad,seamed_image);
// if(counter==20){
// imshow("seamed_image",seamed_image);
// waitKey(0);
// }
remove_col(output_image,seamed_image,colored_image);
// if(counter==20){
// imshow("output_image",output_image);
// waitKey(0);
// }
if(even%2==0){
rotate(output_image, output_image, 1);
rotate(colored_image, colored_image, 1);
// imshow("rotated",output_image);
// waitKey(0);
}
counter++;
}
if(output_image.rows > new_height){
Mat seamed_image,grad,transposed;
transpose(output_image,transposed);
transpose(colored_image,colored_image);
if(even%2==0){
rotate(transposed, transposed, 1);
rotate(colored_image, colored_image, 1);
// imshow("rotated",output_image);
// waitKey(0);
}
energy_map(transposed,grad);
seam_dp(grad,seamed_image);
remove_col(transposed,seamed_image,colored_image);
if(even%2==0){
rotate(transposed, transposed, 1);
rotate(colored_image, colored_image, 1);
// imshow("rotated",output_image);
// waitKey(0);
}
transpose(transposed,output_image);
transpose(colored_image,colored_image);
}
even++;
if(counter > 21) counter = 0;
}
return true;
}
int main( int, char** argv )
{
Mat src,src_gray;
Mat grad;
const char* window_name = "Sobel Demo - Simple Edge Detector";
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
//GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
//cvtColor( src, src_gray, COLOR_BGR2GRAY );
//if(!energy_map(src_gray,grad)) return -1;
//imshow( window_name, grad );
//Mat seamed_image;
//seam_dp(grad,seamed_image);
//imshow( "image gradient scored", seamed_image );
int new_width = atoi(argv[2]);
int new_height = atoi(argv[3]);
Mat output_image,colored_image;
cout<<src.cols<<" "<<src.rows<<endl;
//imshow( "gray image", src_gray );
imshow( "input image", src );
//rotate(src, src, 1);
//imshow("rotated",src);
// transpose(src,src);
// imshow("transposed",src);
// rotate(src, src, 1);
// imshow("rotated",src);
if(!reduce_image(src,output_image,new_width, new_height,colored_image)){
cout<<"unsuccessful"<<endl;
return -1;
}
cout<<output_image.cols<<endl;
imshow( "reduced image", output_image );
imshow( "seamed image", colored_image);
waitKey(0);
return 0;
}