-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpness_Enhancement.cpp
More file actions
111 lines (95 loc) · 4.12 KB
/
Sharpness_Enhancement.cpp
File metadata and controls
111 lines (95 loc) · 4.12 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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// Kernel 1: 4-connected sharpening kernel (Original + 4-neighbor Laplacian)
int laplacianKernel1[3][3] = {
{ 0, -1, 0},
{-1, 5, -1},
{ 0, -1, 0}
};
// Kernel 2: 8-connected sharpening kernel (Original + 8-neighbor Laplacian)
int laplacianKernel2[3][3] = {
{-1, -1, -1},
{-1, 9, -1},
{-1, -1, -1}
};
// Applies 3x3 convolution for image sharpness enhancement
void applyLaplacianFilter(const vector<vector<vector<int>>>& inputImage, vector<vector<vector<int>>>& outputImage, int rows, int cols, int kernel[3][3]) {
for (int i = 1; i < rows - 1; ++i) {
for (int j = 1; j < cols - 1; ++j) {
for (int channel = 0; channel < 3; ++channel) {
int newValue = 0;
// Convolution operation
for (int ka = -1; ka <= 1; ++ka) {
for (int kb = -1; kb <= 1; ++kb) {
newValue += inputImage[i + ka][j + kb][channel] * kernel[ka + 1][kb + 1];
}
}
// Clamp pixel values to the valid [0, 255] range to prevent overflow/underflow
newValue = min(max(newValue, 0), 255);
outputImage[i][j][channel] = newValue;
}
}
}
}
int main() {
ifstream inputFile("input2.bmp", ios::binary);
if (!inputFile) {
cerr << "Error: Unable to open input file - input2.bmp" << endl;
return -1;
}
// Read 54-byte BMP Header
unsigned char header[54];
inputFile.read(reinterpret_cast<char*>(header), 54);
// Extract image dimensions directly from the header
int width = *(int*)&header[18];
int height = *(int*)&header[22];
// Calculate row padding to 4-byte boundaries
int padding = (4 - (width * 3) % 4) % 4;
// Allocate image data with 1-pixel zero-padding for boundary handling (height+2, width+2)
vector<vector<vector<int>>> inputImage(height + 2, vector<vector<int>>(width + 2, vector<int>(3, 0)));
for (int i = 1; i <= height; ++i) {
for (int j = 1; j <= width; ++j) {
for (int channel = 0; channel < 3; ++channel) {
unsigned char pixel;
inputFile.read(reinterpret_cast<char*>(&pixel), 1);
inputImage[i][j][channel] = static_cast<int>(pixel);
}
}
inputFile.ignore(padding); // Skip padding bytes
}
inputFile.close();
// Buffers for storing processed images
vector<vector<vector<int>>> outputImage1(height + 2, vector<vector<int>>(width + 2, vector<int>(3, 0)));
vector<vector<vector<int>>> outputImage2(height + 2, vector<vector<int>>(width + 2, vector<int>(3, 0)));
// Apply mild sharpening (4-connected)
applyLaplacianFilter(inputImage, outputImage1, height + 2, width + 2, laplacianKernel1);
// Apply strong sharpening (8-connected)
applyLaplacianFilter(inputImage, outputImage2, height + 2, width + 2, laplacianKernel2);
// Write processed images to output files
ofstream outputFile1("output2_1.bmp", ios::binary);
ofstream outputFile2("output2_2.bmp", ios::binary);
outputFile1.write(reinterpret_cast<char*>(header), 54);
outputFile2.write(reinterpret_cast<char*>(header), 54);
// Write pixel data and restore padding
for (int i = 1; i <= height; ++i) {
for (int j = 1; j <= width; ++j) {
for (int channel = 0; channel < 3; ++channel) {
unsigned char pixel1 = static_cast<unsigned char>(outputImage1[i][j][channel]);
unsigned char pixel2 = static_cast<unsigned char>(outputImage2[i][j][channel]);
outputFile1.write(reinterpret_cast<char*>(&pixel1), 1);
outputFile2.write(reinterpret_cast<char*>(&pixel2), 1);
}
}
for (int p = 0; p < padding; ++p) {
unsigned char pad = 0;
outputFile1.write(reinterpret_cast<char*>(&pad), 1);
outputFile2.write(reinterpret_cast<char*>(&pad), 1);
}
}
outputFile1.close();
outputFile2.close();
cout << "Processing completed successfully!" << endl;
return 0;
}