-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainopenmp.cpp
More file actions
95 lines (75 loc) · 2.31 KB
/
Copy pathmainopenmp.cpp
File metadata and controls
95 lines (75 loc) · 2.31 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
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include "omp.h"
using namespace cv;
int main( int argc, char** argv )
{
char* imageName = argv[1];
Mat image;
image = imread( imageName, IMREAD_COLOR );
if( !image.data )
{
printf( " No image data \n " );
return -1;
}
Mat blur = image.clone();
int radius;
int NUM_THREADS;
if(argv[2]==NULL || argc<3) {
radius = 3;
NUM_THREADS = 4;
std::cout<<"Radius and NUM_THREADS are null"<<std::endl;
}
else {
radius = atoi(argv[2]);
}
if (argv[3]==NULL || argc<4) {
NUM_THREADS = 4;
std::cout<<"NUM_THREADS is null"<<std::endl;
}
else {
NUM_THREADS = atoi(argv[3]);
}
int protect = radius + 1;
//always radius less than protect, minimun value = 2
//take the time
struct timespec start, finish;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &start);
double start_omp_time = omp_get_wtime();
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for
for (int i = protect; i < image.rows - protect; ++i)
{
for (int j = protect; j < image.cols - protect; ++j)
{
//(i+1,j) right
//(i-1,j) left
//(i,j+1) down
//(i,j-1) top
//RED
blur.at<Vec3b>(i,j)[0] = (image.at<Vec3b>(i+radius,j)[0] + image.at<Vec3b>(i-radius,j)[0] + image.at<Vec3b>(i,j+radius)[0] + image.at<Vec3b>(i,j-radius)[0])/4;
//GREEN
blur.at<Vec3b>(i,j)[1] = (image.at<Vec3b>(i+radius,j)[1] + image.at<Vec3b>(i-radius,j)[1] + image.at<Vec3b>(i,j+radius)[1] + image.at<Vec3b>(i,j-radius)[1])/4;
//BLUE
blur.at<Vec3b>(i,j)[2] = (image.at<Vec3b>(i+radius,j)[2] + image.at<Vec3b>(i-radius,j)[2] + image.at<Vec3b>(i,j+radius)[2] + image.at<Vec3b>(i,j-radius)[2])/4;
}
}
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
std::cout<<"Time: "<< elapsed << std::endl;
std::cout<<"Omp Time: "<< omp_get_wtime() - start_omp_time << std::endl;
//write in file
std::ofstream output;
output.open("mainopenmpResult.txt", std::ofstream::app | std::ofstream::out );
output<< imageName <<", "<< radius << ", " << NUM_THREADS <<", "<< elapsed << std::endl;
output.close();
imwrite( "blurimage.jpg", blur );
//namedWindow( "Blur image", WINDOW_AUTOSIZE );
//imshow( "Blur image", blur );
waitKey(0);
return 0;
}