-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImage.cpp
More file actions
135 lines (109 loc) · 3.33 KB
/
Copy pathImage.cpp
File metadata and controls
135 lines (109 loc) · 3.33 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
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
#ifndef _IMAGE_H
#define _IMAGE_H
#include "Image.h"
#include <cmath>
#endif
using namespace std;
typedef unsigned char unchar;
Image::Image(const char* filename) {
imageData = new unchar*[HEIGHT];
filteredData = new unchar*[HEIGHT];
for (int i = 0; i < HEIGHT; i++) {
imageData[i] = new unchar[WIDTH];
filteredData[i] = new unchar[WIDTH];
}
pInFile = new ifstream;
pInFile->open(filename, ios::in | ios::binary);
pInFile->seekg(0, ios::beg);
pInFile->read(reinterpret_cast<char*>(c_vHeaderData[i]), 1078);
for (i = 0; i < HEIGHT; i++) {
pInFile->read(reinterpret_cast<char*> imageData[i]), WIDTH);
}
pInFile->close();
}
Image::~Image() {
delete pInFile;
delete pOutFile;
for (int i = 0; i < HEIGHT; i++) {
delete[] imageData[i];
delete[] filteredData[i];
}
delete[] imageData;
delete[] filteredData;
}
void Image::writeG(const char* filename) {
createGaussianFilter();
pOutFile = new ofstream;
pOutFile->open(filename, ios::out | ios::trunc | ios::binary);
pOutFile->write(reinterpret_cast<char*>(c_vHeaderData), 1078);
for (int i = 0; i < HEIGHT; i++) {
pOutFile->write(reinterpret_cast<char*>(filteredData[i]), WIDTH);
}
pOutFile->close();
}
void Image::writeM(const char* filename) {
createMedianFilter();
pOutFile = new ofstream;
pOutFile->open(filename, ios::out | ios::trunc | ios::binary);
pOutFile->write(reinterpret_cast<char*>(c_vHeaderData), 1078);
for (int i = 0; i < HEIGHT; i++) {
pOutFile->write(reinterpret_cast<char*>(filteredData[i]), WIDTH);
}
pOutFile->close();
}
void findAverage(double imageData[][]) {
double total = 0;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
total += imageData[i][j];
}
}
return (total / (HEIGHT * WIDTH));
}
void Image::createGaussianFilter() {
for (int i = 0; i < HEIGHT; i++) {
strncpy(reinterpret_cast<char*>(filteredData[i]), reinterpret_cast<char*> imageData[i]));
}
double sigma = 1.0;
double mean = findAverage(imageData);
double sum = 0.0;
for (int x = 0; x < HEIGHT; x++)
for (int y = 0; y < WIDTH; y++) {
filteredData[x][y] = exp(-0.5 * (pow( imageData[x] - mean)/sigma, 2.0)
+ pow( imageData[y] - mean)/sigma, 2.0) / (2 * M_PI * sigma * sigma);
sum += filteredData[x][y];
}
for (int x = 0; x < HEIGHT; x++)
for (int y = 0; y < WIDTH; y++)
filteredData[x][y] /= sum;
}
void insertionSort(int window[]) {
int temp, i , j;
for(i = 0; i < 9; i++) {
temp = window[i];
for(j = i - 1; j >= 0 && temp < window[j]; j--) {
window[j + 1] = window[j];
}
window[j + 1] = temp;
}
}
void Image::createMedianFilter() {
for (int i = 0; i < HEIGHT; i++) {
strcpy(reinterpret_cast<char*>(filteredData[i]), reinterpret_cast<char*> imageData[i]));
}
filteredData[][] = imageData[][];
int window[9];
for (int y = 1; y < HEIGHT - 1; y++)
for (int x = 1; y < WIDTH - 1; x++)
window[0] = imageData[y - 1, x - 1];
window[1] = imageData[y][x - 1];
window[2] = imageData[y + 1][x - 1];
window[3] = imageData[y - 1][x];
window[4] = imageData[y][x];
window[5] = imageData[y + 1][x];
window[6] = imageData[y - 1][x + 1];
window[7] = imageData[y][x + 1];
window[8] = imageData[y + 1][x + 1];
insertionSort(window);
filteredData[y][x] = window[4];
}