-
Notifications
You must be signed in to change notification settings - Fork 0
/
blur-effect.cpp
183 lines (157 loc) · 4.82 KB
/
blur-effect.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
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/mpi/collectives.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <opencv2/opencv.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/vector.hpp>
#include <omp.h>
namespace mpi = boost::mpi;
using namespace std;
/* FOR SERIALIZATION OF MAT OPENCV */
BOOST_SERIALIZATION_SPLIT_FREE(::cv::Mat)
namespace boost {
namespace serialization {
/** Serialization support for cv::Mat */
template<class Archive>
void save(Archive & ar, const ::cv::Mat& m, const unsigned int version)
{
size_t elem_size = m.elemSize();
size_t elem_type = m.type();
ar & m.cols;
ar & m.rows;
ar & elem_size;
ar & elem_type;
const size_t data_size = m.cols * m.rows * elem_size;
ar & boost::serialization::make_array(m.ptr(), data_size);
}
/** Serialization support for cv::Mat */
template<class Archive>
void load(Archive & ar, ::cv::Mat& m, const unsigned int version)
{
int cols, rows;
size_t elem_size, elem_type;
ar & cols;
ar & rows;
ar & elem_size;
ar & elem_type;
m.create(rows, cols, elem_type);
size_t data_size = m.cols * m.rows * elem_size;
ar & boost::serialization::make_array(m.ptr(), data_size);
}
}
}
//Global variables on each process
int KERNEL,THREADS;
int width,height;
cv::Mat myImage,myImageOut;
// aplies the blur effect on the pixel (x,y) and stores the result on the output matrix
void aplyBlur(int x, int y){
// collect the average data of neighbours
int blue,green,red;
blue=green=red=0;
int n=0;
cv::Vec3b pixel;
for(int i = x - (KERNEL/2); i < x+(KERNEL/2); i++)
{
for (int j = y-(KERNEL/2); j < y+(KERNEL/2); j++)
{
//check if the point is in the image limits
if(0<=i && i<width-1 && 0<=j && j<height-1){
pixel = myImage.at<cv::Vec3b>(cv::Point(i,j));
blue += pixel.val[0];
green += pixel.val[1];
red += pixel.val[2];
n++;
}
}
}
if(n!=0){
//write the average on the output image
cv::Vec3b pixelBlur = cv::Vec3b(blue/n, green/n, red/n);
myImageOut.at<cv::Vec3b>(cv::Point(x,y))= pixelBlur;
}
}
int main(int argc, char* argv[]) {
//Read the arguments
KERNEL=atoi(argv[3]);
THREADS = atoi(argv[4]);
string oFile = argv[2];
string iFile = argv[1];
//set the mpi environment
mpi::environment env(argc, argv);
mpi::communicator world;
//local variables
cv::Mat input;
cv::Mat output;
vector<cv::Mat> mats;
vector<cv::Mat> matsReceived;
input = cv::imread(iFile);
if ( !input.data )
{
printf("No image data \n");
return -1;
}
/* if(world.rank() == 0){
//slice image and stores in the vector
int w = input.cols;
int h = input.rows/world.size();
cv::Mat sliced;
for(int i =0;i<world.size();i++){
sliced = input(cv::Rect(0,i*h,w,h));
mats.push_back(sliced);
}
}
//scatter on the processes
scatter(world,mats,myImage,0); */
// Do the blur on the slice
int w = input.cols;
int h = input.rows/world.size();
if(world.rank()==0){
myImage = input(cv::Rect(0,(world.rank()*h),w,h+15));
}else if(world.rank()==world.size()-1){
myImage = input(cv::Rect(0,(world.rank()*h)-15,w,h+15));
}else{
myImage = input(cv::Rect(0,(world.rank()*h)-15,w,h+30));
}
width = myImage.cols;
height = myImage.rows;
myImageOut=myImage.clone();
if ( !myImageOut.data ){
printf("No image data on node %d \n",world.rank());
return -1;
}
#pragma omp parallel num_threads(THREADS)
{
int tn = omp_get_thread_num();
int ini = (int)(width/THREADS)*(tn);
int fin = (int)(width/THREADS)+ini;
//printf("[%d]: thread : %d , Inicio: %d , Fin: %d \n",world.rank(),tn,ini,fin);
for (int i = ini; i < fin; i++)
{
for (int j = 0; j < height; j++)
{
aplyBlur(i,j);
}
}
//printf("I'm the thread %d on process %d \n",tn,world.rank());
}
if(world.rank()==0){
myImageOut = myImageOut(cv::Rect(0,0,w,h));
}else if(world.rank()==world.size()-1){
myImageOut = myImageOut(cv::Rect(0,15,w,h));
}else{
myImageOut = myImageOut(cv::Rect(0,15,w,h));
}
//gather the result
gather(world,myImageOut,matsReceived,0);
if(world.rank()==0){
cv::vconcat(matsReceived,output);
imwrite( oFile, output );
}
return 0;
}