-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprog5.cpp
203 lines (155 loc) · 5.26 KB
/
prog5.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
/****************************
Sobel edge detection
*****************************/
#include <iostream>
#include <bits/stdc++.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <math.h>
#include <omp.h>
using namespace cv;
using namespace std;
int main()
{
string filename;
cout<<"Enter filename:";
cin>> filename;
int normalize_val=8;
cout<<"\nEnter normalization value:";
cin>> normalize_val;
// Reading image
Mat img = imread(filename);
// Displaying image
imshow("Original Image",img);
waitKey(0);
// Converting to grayscale
Mat img_gray,image_blur;
GaussianBlur( img, image_blur, Size(3,3), 3, 3);
cvtColor(image_blur,img_gray,CV_RGB2GRAY);
// Displaying grayscale image
//imshow("Original Image",img_gray);
//waitKey(0);
int cols = img_gray.cols;
int rows = img_gray.rows;
// Creating sobel operator in x direction
int sobel_x[3][3] = {-1,0,1,-2,0,2,-1,0,1};
// Creating sobel operator in y direction
int sobel_y[3][3] = {-1,-2,-1,0,0,0,1,2,1};
int radius = 1;
// Handle border issues
Mat _src;
copyMakeBorder(img_gray, _src, radius, radius, radius, radius, BORDER_REFLECT101);
// Create output matrix
Mat gradient_x = img_gray.clone();
Mat gradient_y = img_gray.clone();
Mat gradient_f = img_gray.clone();
int max=0;
// Correlation loop in x direction
// Iterate on image
int r,c,i,j,s;
cout<<_src.rows;
double start,end,diff;
start = omp_get_wtime();
for (r = radius; r < _src.rows - radius; ++r)
{
for (c = radius; c < _src.cols - radius; ++c)
{
s = 0;
// Iterate on kernel
//#pragma omp parallel for default(shared) private(r,c,i,j) num_threads(3) ordered collapse(2) schedule(static,3)
for (i = -radius; i <= radius; ++i)
{
for (j = -radius; j <= radius; ++j)
{
s += _src.at<uchar>(r + i, c + j) * sobel_x[i + radius][j + radius];
}
}
gradient_x.at<uchar>(r - radius, c - radius) = s/normalize_val;
/*if(s>200)
gradient.at<uchar>(r - radius, c - radius) = 255;
else
gradient.at<uchar>(r - radius, c - radius) = 0;
*/
}
}
end = omp_get_wtime();
diff = end - start;
cout<<"serial code time:"<<diff;
Mat absGrad_x;
convertScaleAbs( gradient_x, absGrad_x );
// Conrrelation loop in y direction
// Iterate on image
//#pragma omp parallel for default(shared) private(r,c,i,j) num_threads(800) ordered schedule(static,3) collapse(2)
for (int r = radius; r < _src.rows - radius; ++r)
{
for (int c = radius; c < _src.cols - radius; ++c)
{
int s = 0;
// Iterate on kernel
for (int i = -radius; i <= radius; ++i)
{
for (int j = -radius; j <= radius; ++j)
{
s += _src.at<uchar>(r + i, c + j) * sobel_y[i + radius][j + radius];
}
}
gradient_y.at<uchar>(r - radius, c - radius) = s/normalize_val;
/*if(s>200)
gradient.at<uchar>(r - radius, c - radius) = 255;
else
gradient.at<uchar>(r - radius, c - radius) = 0;
*/
}
}
Mat absGrad_y;
convertScaleAbs( gradient_y, absGrad_y );
Mat absGrad =img_gray.clone(); ;
for(int i=0; i<absGrad_y.rows; i++)
{
for(int j=0; j<absGrad_y.cols; j++)
{
absGrad.at<uchar>(i,j) = sqrt( pow(absGrad_x.at<uchar>(i,j),2) + pow(absGrad_y.at<uchar>(i,j),2) );
if(absGrad.at<uchar>(i,j) >250)
absGrad.at<uchar>(i,j) = 255;
else
absGrad.at<uchar>(i,j) = 0;
}
}
//Calculating gradient magnitude
for(int i=0; i<gradient_f.rows; i++)
{
for(int j=0; j<gradient_f.cols; j++)
{
gradient_f.at<uchar>(i,j) = sqrt( pow(gradient_x.at<uchar>(i,j),2) + pow(gradient_y.at<uchar>(i,j),2) );
if(gradient_f.at<uchar>(i,j) >250)
gradient_f.at<uchar>(i,j) = 150;
else
gradient_f.at<uchar>(i,j) = 0;
}
}
/*
imshow("grad x",gradient_x);
waitKey(0);
imshow("grad y",gradient_y);
waitKey(0);
*/
imshow("grad magnitude",gradient_f);
waitKey(0);
//imshow("absolute grad magnitude",absGrad);
//waitKey(0);
/*cv::Mat Gx, Gy; int ksize=3;
Mat abs_grad_x, abs_grad_y;
cv::Sobel(img_gray, Gx, CV_8U, 1, 0, ksize);
convertScaleAbs( Gx, abs_grad_x );
cv::Sobel(img_gray, Gy, CV_8U, 0, 1, ksize);
convertScaleAbs( Gy, abs_grad_y );
Mat grad;
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
*/
//imshow("Sobel Image",grad);
//waitKey(0);
return 0;
}