-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
93 lines (80 loc) · 2.88 KB
/
main.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
#include<stdio.h>
#include<iostream>
#include "MedianFilter.h"
#include "Bitmap.h"
#include <ctime>
const int window_size = WINDOW_SIZE;
#define ITERATIONS ( 1 )
int CompareBitmaps( Bitmap* inputA, Bitmap* inputB ){
int differentpixels = 0; //Initializing the diffrerce Variable.
if((inputA->Height() != inputB->Height()) || (inputA->Width() != inputB->Width())) // Check the condition for height and width matching.
return -1;
for(int height=1; height<inputA->Height()-1; height++){
for(int width=1; width<inputA->Width()-1; width++){
if(inputA->GetPixel(width, height) != inputB->GetPixel(width, height))
differentpixels++; // increment the differences.
}
}
return differentpixels;
}
void MedianFilterCPU( Bitmap* image, Bitmap* outputImage )
{
unsigned char filterVector[9] = {0,0,0,0,0,0,0,0,0}; //Taking the filter initialization.
for(int row=0; row<image->Height(); row++){
for(int col=0; col<image->Width(); col++){
if((row==0) || (col==0) || (row==image->Height()-1) || (col==image->Width()-1)) //Check the boundry condition.
outputImage->SetPixel(col, row, 0);
else {
for (int x = 0; x < WINDOW_SIZE; x++) {
for (int y = 0; y < WINDOW_SIZE; y++){
filterVector[x*WINDOW_SIZE+y] = image->GetPixel( (col+y-1),(row+x-1)); //Fill the Filter Vector
}
}
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 9; j++) {
if (filterVector[i] > filterVector[j]) {
char tmp = filterVector[i];
filterVector[i] = filterVector[j];
filterVector[j] = tmp;
}
}
}
outputImage->SetPixel(col, row, filterVector[4]); //Finally assign value to output pixels
}
}
}
}
int main()
{
Bitmap* originalImage = new Bitmap();
Bitmap* resultImageCPU = new Bitmap();
Bitmap* resultImageGPU = new Bitmap();
Bitmap* resultImageSharedGPU = new Bitmap();
float tcpu, tgpu, tgpushared; //timing variables.
clock_t start, end;
originalImage->Load("Lab/mf512.bmp");
resultImageCPU->Load("Lab/mf512.bmp");
resultImageGPU->Load("Lab/mf512.bmp");
resultImageSharedGPU->Load("Lab/mf512.bmp");
std::cout << "Operating on a " << originalImage->Width() << " x " << originalImage->Height() << " image..." << std::endl;
start = clock(); //Stat the clock
for (int i = 0; i < ITERATIONS; i++)
{
MedianFilterCPU(originalImage, resultImageCPU);
}
end = clock();
std::cout << start << " --- " << end << std::endl;
tcpu = ((float)(end-start) + 1) * 1000 / (float)CLOCKS_PER_SEC/ITERATIONS;
std::cout<< "cputime " << tcpu<<"ms"<<std::endl;
for (int i = 0; i < ITERATIONS; i++)
{
MedianFilterGPU(originalImage, resultImageGPU, false);
}
for (int i = 0; i < ITERATIONS; i++)
{
MedianFilterGPU(originalImage, resultImageSharedGPU, true); //GPU call for median Filtering with shared Kernel.
}
resultImageCPU->Save("Lab/Lenna_cpu.bmp");
resultImageGPU->Save("Lab/Lenna_gpu.bmp");
resultImageSharedGPU->Save("Lab/Lenna_shared.bmp");
}