-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimageFunction.cpp
More file actions
165 lines (147 loc) · 4.88 KB
/
imageFunction.cpp
File metadata and controls
165 lines (147 loc) · 4.88 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
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
/**
* @file imageFunction.cpp
* @brief Image processing class implementation
* @author Maxim Tetuchin
* @date 2025
* @version 1.0
*/
#include "imageFunction.hpp"
/**
* @brief Constructs an image object with specified parameters
* @param width Image width in pixels
* @param height Image height in pixels
* @param path Path to source image file
*/
image::image(int width, int height, std::string path):
_width(width),
_height(height),
_path(path){
_imageVector = std::vector<uint8_t>(_width * _height);
_imageMatrix = std::vector<std::vector<uint8_t>>(_height, std::vector<uint8_t>(_width, 0));
}
/**
* @brief Destructor clears allocated memory
*/
image::~image() {
_imageVector.clear();
_imageMatrix.clear();
}
/**
* @brief Reads image data from binary file into 1D vector
* @throws std::runtime_error if file cannot be opened
*/
void image::readImage(){
std::ifstream openedImage(_path, std::ios::binary);
if (!openedImage){
std::cerr << "Check your filepath" << std::endl;
throw std::runtime_error("Failed to open image file");
}
openedImage.read(reinterpret_cast<char*>(_imageVector.data()),_width*_height);
openedImage.close();
}
/**
* @brief Saves processed image data to RAW binary file
* @param outputFileName Path for output file
*/
void image::saveToRaw(std::string outputFileName){
std::ofstream matrixOutput (outputFileName,std::ios::binary);
matrixOutput.write(reinterpret_cast<char*>(_imageVector.data()),(_height*_width));
_imageVector.clear();
}
/**
* @brief Converts 1D vector representation to 2D matrix
*
* After conversion, the vector memory is cleared to optimize memory usage.
*/
void image::vecToMat(){
for (int i = 0; i < _height; ++i) {
for (int j = 0; j < _width; ++j) {
_imageMatrix[i][j] = _imageVector[i * _width + j];
}
}
_imageVector.clear();
_imageVector.shrink_to_fit();
}
/**
* @brief Converts 2D matrix representation back to 1D vector
*
* Resizes the vector to accommodate all pixel data before conversion.
*/
void image::matToVec(){
_imageVector.resize(_height * _width);
for (int i = 0; i < _height; ++i) {
for (int j = 0; j < _width; ++j) {
_imageVector[i * _width + j] = _imageMatrix[i][j];
}
}
}
/**
* @brief Rotates image 90 degrees clockwise using matrix transformation
*
* Swaps image dimensions after rotation. Uses move semantics for efficiency.
*/
void image::matRotate(){
std::vector<std::vector<uint8_t>> _rotatedMat =
std::vector<std::vector<uint8_t>>(_width, std::vector<uint8_t>(_height, 0));
for (int i = 0; i < _height; ++i){
for (int j = 0; j < _width; ++j){
_rotatedMat[j][_height - 1 - i] = _imageMatrix[i][j];
}
}
_imageMatrix = std::move(_rotatedMat);
std::swap(_height,_width);
}
/**
* @brief Applies Gaussian blur filter with specified kernel size (single-threaded)
* @param kernelSize Size of Gaussian kernel (must be odd number)
*
* Creates Gaussian kernel, normalizes it, and applies convolution to image matrix.
* Uses sigma value stored in class member _sigma.
*/
void image::gauss(int kernelSize) {
if (kernelSize % 2 == 0) {
std::cerr << "Kernel size must be an odd number." << std::endl;
return;
}
int halfSize = kernelSize / 2;
std::vector<std::vector<double>> kernel(kernelSize, std::vector<double>(kernelSize, 0));
double sum = 0.0;
// Fill kernel with Gaussian values
for (int x = -halfSize; x <= halfSize; ++x) {
for (int y = -halfSize; y <= halfSize; ++y) {
double value = exp(-(x * x + y * y) / (2 * _sigma * _sigma)) / (2 * M_PI * _sigma * _sigma);
kernel[x + halfSize][y + halfSize] = value;
sum += value;
}
}
// Normalize kernel
for (int i = 0; i < kernelSize; ++i) {
for (int j = 0; j < kernelSize; ++j) {
kernel[i][j] /= sum;
}
}
std::vector<std::vector<uint8_t>> blurredImage =
std::vector<std::vector<uint8_t>>(_height, std::vector<uint8_t>(_width, 0));
for (int i = 0; i < _height; ++i) {
for (int j = 0; j < _width; ++j) {
double pixelValue = 0.0;
for (int ki = -halfSize; ki <= halfSize; ++ki) {
for (int kj = -halfSize; kj <= halfSize; ++kj) {
int ni = i + ki;
int nj = j + kj;
if (ni >= 0 && ni < _height && nj >= 0 && nj < _width) {
pixelValue += _imageMatrix[ni][nj] * kernel[ki + halfSize][kj + halfSize];
}
}
}
blurredImage[i][j] = static_cast<uint8_t>(std::round(pixelValue));
}
}
_imageMatrix = std::move(blurredImage);
}
/**
* @brief Debug method to print matrix dimensions
*/
void image::getLen(){
std::cout << _imageMatrix.size() << "\n";
}