Skip to content

Commit 4845305

Browse files
author
Michael Bassili
committed
Add gaussian kernal and gaussian blur to model
1 parent 683aca8 commit 4845305

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

tgaimage.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <fstream>
3+
#include <cmath>
34
#include <string.h>
45
#include <time.h>
56
#include <math.h>
@@ -354,3 +355,50 @@ bool TGAImage::scale(int w, int h) {
354355
return true;
355356
}
356357

358+
float* gaussian_kernel(const int radius) {
359+
int size = (radius*2)+1;
360+
float norm = 1.f/(std::sqrt(2.f*M_PI)*radius);
361+
float* gaussian_kernel = new float[size];
362+
float coeff = -1.f/(2.f*radius*radius);
363+
364+
float sum = 0.f;
365+
for (int i=0; i<size; i++) {
366+
gaussian_kernel[i] = norm * exp(powl(i-radius,2)*coeff);
367+
sum += gaussian_kernel[i];
368+
}
369+
for (int i=size; i--; gaussian_kernel[i] /= sum);
370+
return gaussian_kernel;
371+
}
372+
373+
void TGAImage::gaussian_blur(const int radius) {
374+
float *kernel = gaussian_kernel(radius);
375+
TGAImage tmp(*this);
376+
int size = (radius*2)+1;
377+
for (int j=size; j<get_height(); j++) {
378+
for (int i=0; i<get_width(); i++) {
379+
float BGRA[4] = {0,0,0,0};
380+
for (int k=0; k<size; k++){
381+
TGAColor c = get(i, j-size+k);
382+
for (int d=0; d<bytespp; d++) BGRA[d] += float(c[d])*kernel[k];
383+
}
384+
unsigned char cBGRA[4];
385+
for (int i=4; i--; cBGRA[i] = (unsigned char)BGRA[i]);
386+
tmp.set(i, j, TGAColor(cBGRA, bytespp));
387+
}
388+
}
389+
for (int j=0; j<get_height(); j++) {
390+
for (int i=size; i<get_width(); i++) {
391+
float BGRA[4] = {0,0,0,0};
392+
for (int k=0; k<size; k++){
393+
TGAColor c = tmp.get(i-size+k, j);
394+
for (int d=0; d<bytespp; d++) BGRA[d] += float(c[d])*kernel[k];
395+
}
396+
unsigned char cBGRA[4];
397+
for (int i=4; i--; cBGRA[i] = (unsigned char)BGRA[i]);
398+
set(i, j, TGAColor(cBGRA, bytespp));
399+
}
400+
}
401+
delete [] kernel;
402+
}
403+
404+

tgaimage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class TGAImage {
9292
int get_bytespp();
9393
unsigned char *buffer();
9494
void clear();
95+
void gaussian_blur(const int radius);
9596
};
9697

9798
#endif //__IMAGE_H__

0 commit comments

Comments
 (0)