-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdc_omp.cpp
337 lines (249 loc) · 9.48 KB
/
pdc_omp.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//g++ -o pdc_omp pdc_omp.cpp `pkg-config --cflags --libs opencv4` -fopenmp
// ./pdc_omp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
#include <cctype>
#include <omp.h>
#include <unistd.h>
using namespace std;
using namespace cv;
const int MAX_IMAGES = 10000;
vector<omp_lock_t> guardians;
int operationSpecify[2];
Mat images[MAX_IMAGES];
int numLoadedImages = 0;
void loadImage(int index, string filePath) {
Mat img = imread(filePath);
if (!img.empty()) {
#pragma omp critical
{
if (numLoadedImages < MAX_IMAGES) {
images[numLoadedImages++] = img;
}
}
cout << "Loaded image: " << filePath << endl;
} else {
cerr << "Failed to load image: " << filePath << endl;
}
}
void displayMenu() {
cout << "\n\nWelcome to Image Processing Program!" << endl;
cout << "Select the operations you want to perform: " << endl;
cout << "g: Grayscale\n";
cout << "f: Flip\n";
cout << "r: Rotate\n";
cout << "hc: High Contrast | lc: Low Contrast\n";
cout << "hb: High Brightness | lb: Low Brightness\n";
cout << "gb: Gaussian Blur | br: Background Remover | ed: Edge Detector\n\n";
}
vector<string> getUserOperations(int numOptions) {
vector<string> selectedOperations;
int index = 0;
while (selectedOperations.size() < numOptions) {
string option;
cout << "Enter option " << selectedOperations.size() + 1 << ": ";
cin >> option;
transform(option.begin(), option.end(), option.begin(), ::tolower);
if (option == "g" || option == "f" || option == "r" || option == "hc" || option == "br" || option == "gb" || option == "ed" || option == "hb" || option == "lc" || option == "lb") {
bool isDuplicate = false;
for (auto iter = 0; iter < index; ++iter) {
if (option == selectedOperations[iter]) {
isDuplicate = true;
break;
}
}
if (isDuplicate) {
cout << "\nOption already selected. Please try another one." << endl;
continue;
}
if (option == "f") {
int reqFlip;
cout << "Enter flip code (0 for vertical, 1 for horizontal, -1 for both): ";
cin >> reqFlip;
operationSpecify[1] = reqFlip;
}
if (option == "r") {
int reqAngle;
cout << "Enter angle to rotate all your images by (Anticlockwise): ";
cin >> reqAngle;
operationSpecify[0] = reqAngle;
}
selectedOperations.push_back(option);
index++;
} else {
cout << "Invalid option! Please select from g, f, r, hc, lc, hb, lb, ed, br, gb" << endl;
}
}
return selectedOperations;
}
void imgRotater(int angle, int i) {
int height = images[i].rows;
int width = images[i].cols;
Point2f image_center(width / 2.f, height / 2.f);
Mat rotation_mat = getRotationMatrix2D(image_center, angle, 1.0);
double abs_cos = abs(rotation_mat.at<double>(0, 0));
double abs_sin = abs(rotation_mat.at<double>(0, 1));
int new_width = int(height * abs_sin + width * abs_cos);
int new_height = int(height * abs_cos + width * abs_sin);
rotation_mat.at<double>(0, 2) += new_width / 2.0 - image_center.x;
rotation_mat.at<double>(1, 2) += new_height / 2.0 - image_center.y;
Mat rotated_mat;
warpAffine(images[i], rotated_mat, rotation_mat, Size(new_width, new_height));
images[i] = rotated_mat;
}
// Flip image
void imgFlipper(int flipCode, int i) {
flip(images[i], images[i], flipCode);
}
// Grayscale image
void imgGrayer(int i) {
cvtColor(images[i], images[i], COLOR_BGR2GRAY);
}
// High contrast image
void histogramContraster(int i) {
cvtColor(images[i], images[i], COLOR_BGR2YCrCb);
vector<Mat> channels;
split(images[i], channels);
equalizeHist(channels[0], channels[0]);
merge(channels, images[i]);
cvtColor(images[i], images[i], COLOR_YCrCb2BGR);
}
// Low contrast image
void lowContraster(int i) {
images[i].convertTo(images[i], -1, 0.5, 0);
}
// High and Low Brightness image
void brightnesser(int i, string req) {
if (req == "lb") {
images[i].convertTo(images[i], -1, 1, -100);
} else { // high brightness
images[i].convertTo(images[i], -1, 1, 100);
}
}
// Edge Detection image
void edger(int i) {
Mat resultCanny;
Canny(images[i], resultCanny, 80, 240);
images[i] = resultCanny;
}
// Gaussian Blur image
void imgGaussBlur(int i) {
int kernel = 5;
int sigMax = 13;
GaussianBlur(images[i], images[i], Size(kernel, kernel), sigMax);
}
// Background Remove image
void imgBGRemover(int i) {
Mat hsvImage;
cvtColor(images[i], hsvImage, COLOR_BGR2HSV);
Scalar lowerBound(0, 100, 100);
Scalar upperBound(10, 255, 255);
Mat mask;
inRange(hsvImage, lowerBound, upperBound, mask);
Mat foreground;
images[i].copyTo(foreground, mask);
images[i] = foreground;
}
int getOptimalChunkSize(int numImages) {
int optimal, minimum = 2;
int coreCount = omp_get_max_threads();
optimal = max(minimum, (numImages / coreCount));
if (numImages < (coreCount * 2)) return 1;
return optimal;
}
void runner(int id, vector<string> Op, int num, int startIndex, int endIndex) {
// Iterate through each image in the chunk and apply each operation
#pragma omp parallel for
for (int i = startIndex; i < endIndex; i++) {
for (int j = 0; j < num; j++) {
string task = Op[j];
omp_set_lock(&guardians[i]);
if (task == "r") {
imgRotater(operationSpecify[0], i);
} else if (task == "f") {
imgFlipper(operationSpecify[1], i);
} else if (task == "g") {
imgGrayer(i);
} else if (task == "hc") {
histogramContraster(i);
} else if (task == "lc") {
lowContraster(i);
} else if (task == "lb" || task == "hb") {
brightnesser(i, task);
} else if (task == "ed") {
edger(i);
} else if (task == "br") {
imgBGRemover(i);
} else if (task == "gb") {
imgGaussBlur(i);
}
omp_unset_lock(&guardians[i]);
}
}
}
int main() {
displayMenu();
int num;
cout << "Enter the number of options you want to perform: ";
cin >> num;
if (num < 0 || num > 10) {
cout << "Please enter correct number of options\n";
return 0;
}
vector<string> selectedOperations = getUserOperations(num);
cout << "Selected operations: " << endl;
for (string op : selectedOperations) {
cout << op << " ";
}
cout << endl;
string folderPath = "images/";
vector<string> fileList;
glob(folderPath, fileList); // Fill the fileList with image files from the folder
int numImages = min((int)fileList.size(), MAX_IMAGES);
guardians.resize(numImages);
for (int i = 0; i < numImages; ++i) {
omp_init_lock(&guardians[i]);
}
double processStartload = omp_get_wtime();
// Load images concurrently
#pragma omp parallel for
for (int i = 0; i < numImages; ++i) {
loadImage(i, fileList[i]);
}
double processEndload = omp_get_wtime();
int optimalChunkSize = getOptimalChunkSize(numImages);
double processStart = omp_get_wtime();
// Process images in parallel
#pragma omp parallel
{
int numThreads = omp_get_num_threads();
int chunkSize = max(optimalChunkSize, numImages / numThreads);
#pragma omp for
for (int i = 0; i < numImages; i += chunkSize) {
runner(omp_get_thread_num(), selectedOperations, num, i, min(i + chunkSize, numImages));
}
}
double processEnd = omp_get_wtime();
string outputPath = "output/";
double processStartsave = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < numImages; i++) {
string outputFilePath = outputPath + "image_" + to_string(i) + ".jpg";
// Save the image to the output path
if (imwrite(outputFilePath, images[i])) {
cout << "Saved image: " << outputFilePath << endl;
} else {
cerr << "Error saving image: " << outputFilePath << endl;
}
}
double processEndsave = omp_get_wtime();
cout << "Number of images processed: " << numImages << endl;
cout << "All images saved to output directory." << endl;
cout << "Total time taken for processing images: " << (processEnd - processStart) << " seconds" << endl;
cout << "Total time taken for saving images to output file: " << (processEndsave - processStartsave) << " seconds" << endl;
cout << "Total time taken for loading images from images file to array: " << (processEndload - processStartload) << " seconds" << endl;
cout << "\n\nThank You for choosing our application!\n";
return 0;
}