-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smoothing.cpp
125 lines (88 loc) · 3.53 KB
/
Smoothing.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
//header files
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
//#include <iostream>
#include <stdio.h>
//using namespaces
using namespace cv;
//using namespace std;
int main()
{
//create two empty windows
namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("Smoothed Image", CV_WINDOW_AUTOSIZE);
namedWindow("Gaussian Smooth", CV_WINDOW_AUTOSIZE);
namedWindow("Median Smooth", CV_WINDOW_AUTOSIZE);
namedWindow("Bilateral Smooth", CV_WINDOW_AUTOSIZE);
//Load an image from file
Mat src = imread("test.jpg");
//show the loaded image
imshow("Original Image", src);
Mat dst, gdst, mdst, bdst;
char zBuffer[35];
for(int i=1;i<31;i=i+2)
{
//copy the text to the "zBuffer"
snprintf(zBuffer, 35, "Kernel Size: %d x %d",i,i);
//smooth the image in the "src" and save it to "dst"
blur(src, dst, Size(i,i));
//smooth the image using Gaussian kernel in the "src" and save it to "dst"
GaussianBlur(src, gdst, Size(i,i),0,0);
//Median Blur
medianBlur(src, mdst, i);
//Bilateral Blur
bilateralFilter(src, bdst, i,i,i);
//put the text in the "zBuffer" to the "dst" image
putText(dst, zBuffer, Point(src.cols/4, src.rows/8), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255));
//show the blurred image with the text
imshow("Smoothed Image", dst);
//put the text in the "zBuffer" to the "gdst" image
putText(gdst, zBuffer, Point(src.cols/4, src.rows/8), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255));
//show the Gaussian Smooth image
imshow("Gaussian Smooth", gdst);
//put the text in the "zBuffer" to the "mdst" image
putText(mdst, zBuffer, Point(src.cols/4, src.rows/8), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255),2);
//show the Median Smooth
imshow("Median Smooth", mdst);
//put the text in the "zBuffer" to the "bdst" image
putText(bdst, zBuffer, Point(src.cols/4, src.rows/8), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255));
//show the Bilateral Smooth
imshow("Bilateral Smooth", bdst);
//wait for 2 seconds
int c = waitKey(3000);
//if the "esc" key is pressed during the wait, return
if(c==27)
{
return 0;
}
}
//make the "dst" image, black
dst = Mat::zeros(src.size(), src.type());
//make the "gdst" image, black
gdst = Mat::zeros(src.size(), src.type());
//make the "mdst" image, black
mdst = Mat::zeros(src.size(), src.type());
//make the "bdst" image, black
bdst = Mat::zeros(src.size(), src.type());
//copy the text to the "zBuffer"
snprintf(zBuffer, 35, "Press any key to Exit");
//put the text in the "zBuffer" to the "dst" image
putText(dst, zBuffer, Point(src.cols/4, src.rows/2), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255));
//show the black image with the text
imshow("Smoothed Image", dst);
//put the text in the "zBuffer" to the "gdst" image
putText(gdst, zBuffer, Point(src.cols/4, src.rows/2), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255));
//show the image where gaussian blur is applied
imshow("Gaussian Smooth", gdst);
//put the text in the "zBuffer" to the "mdst" image
putText(mdst, zBuffer, Point(src.cols/4, src.rows/2), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255),2);
//show the image where Median blur is applied
imshow("Median Smooth", mdst);
//put the text in the "zBuffer" to the "bdst" image
putText(bdst, zBuffer, Point(src.cols/4, src.rows/2), CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,255,255),2);
//show the image where Median blur is applied
imshow("Bilateral Smooth", bdst);
//wait for a key press infinitely
waitKey(0);
return 0;
}