Skip to content

Instantly share code, notes, and snippets.

@jameslittle230
Last active February 25, 2018 13:58
Show Gist options
  • Save jameslittle230/07aa242425deb1bfca71dea2e3326eb6 to your computer and use it in GitHub Desktop.
Save jameslittle230/07aa242425deb1bfca71dea2e3326eb6 to your computer and use it in GitHub Desktop.
HW3 Submission -- Gaussian Blur
void R2Image::
Blur(double sigma)
{
R2Image *temp = new R2Image(width, height);
// Generate 1D kernel
int k_rad = 3 * sigma;
int k_len = 6 * sigma + 1;
double *k = (double *) malloc(k_len * sizeof(double));
double sum = 0.0;
double norm = 1.0 / 2.50662 * sigma;
double coeff = 2.0 * sigma * sigma;
for(int i=-1 * k_rad; i<=k_rad; i++) {
double val = norm * std::exp(-i * i / coeff);
k[i+k_rad] = val;
sum += k[i+k_rad];
}
for(int i=0; i<k_len; i++) {
k[i] /= sum;
std::cout << "k[" << i << "] = " << k[i] << std::endl;
}
int x, y, i;
for(y=0; y<height; y++) {
for(x=0; x<width; x++) {
double r = 0.0, g = 0.0, b = 0.0;
for(i=-1*k_rad; i<=k_rad; i++) {
r += Pixel(x, y+i).Red() * k[i+k_rad];
g += Pixel(x, y+i).Green() * k[i+k_rad];
b += Pixel(x, y+i).Blue() * k[i+k_rad];
}
// std::cout << val << std::endl;
R2Pixel* p = new R2Pixel(r, g, b, 1);
temp->SetPixel(x, y, *p);
}
}
this->pixels = temp->pixels;
temp->pixels = (R2Pixel *) malloc(npixels * sizeof(R2Pixel));
for(y=k_rad; y<height-k_rad; y++) {
for(x=k_rad; x<width-k_rad; x++) {
double r = 0.0, g = 0.0, b = 0.0;
for(i=-1*k_rad; i<=k_rad; i++) {
r += Pixel(x+i, y).Red() * k[i+k_rad];
g += Pixel(x+i, y).Green() * k[i+k_rad];
b += Pixel(x+i, y).Blue() * k[i+k_rad];
}
// std::cout << val << std::endl;
R2Pixel* p = new R2Pixel(r, g, b, 1);
temp->SetPixel(x, y, *p);
}
}
this->pixels = temp->pixels;
temp->pixels = nullptr;
free(k);
delete temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment