Skip to content

Commit

Permalink
Add gamma correction
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumix committed Mar 25, 2017
1 parent 4ab0fc1 commit 5efc654
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LDIR = /opt/local/lib/
LIBS = -lc -lpng16
CFLAGS = -Wall -march=native -Ofast -Wa,-q
OUT_DIR = build
LABS = lab1_1 lab1_2 lab1_3 lab1_4 lab1_5 lab1_6 lab1_7 lab2_1 lab2_2 lab3 lab4_2 lab5 lab6 lab7
LABS = lab1_1 lab1_2 lab1_3 lab1_4 lab1_5 lab1_6 lab1_7 lab2_1 lab2_2 lab3 lab4_1 lab4_2 lab5 lab6 lab7
BINS = $(addprefix $(OUT_DIR)/,${LABS})
OBJS = $(addsuffix .o,${BINS})

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Here are the programs that I wrote during the Digital Image Processing class in
2. [Bilinear interpolation](#bilinear-interpolation-lab2_2c)
3. [Alpha compositing](#alpha-compositing-lab3c)
4. Intensity transformations
1. [Gamma correction](#gamma-correction-lab4_1c)
2. [Linear contrast stretching](#linear-contrast-stretching-lab4_2c)
5. [Convolution](#convolution-lab5c)
6. [Median filter](#median-filter-lab6c)
Expand Down Expand Up @@ -80,6 +81,18 @@ Here are the programs that I wrote during the Digital Image Processing class in
| ----- | ------ |
| <ul><li>[Goldhill.png](images/Goldhill.png) as top</li><li>[Lena.png](images/Lena.png) as bottom</li><li>[Peppers.png](images/Peppers.png) as alpha channel</li></ul> | ![Output](lab3/Goldhill_Lena_Peppers_out.png) |

---
## Gamma correction ([lab4_1.c](lab4_1/lab4_1.c))
| Input | Output (c = 1.0, gamma = 0.6) |
| ----- | ------ |
| ![Input](images/House.png) | ![Output](lab4_1/House_out_1.0_0.6.png) |
| ![Input](lab4_1/House_in_hist.png) | ![Output](lab4_1/House_out_1.0_0.6_hist.png) |

| Input | Output (c = 1.5, gamma = 1.5) |
| ----- | ------ |
| ![Input](images/House.png) | ![Output](lab4_1/House_out_1.5_1.5.png) |
| ![Input](lab4_1/House_in_hist.png) | ![Output](lab4_1/House_out_1.5_1.5_hist.png) |

---
## Linear contrast stretching ([lab4_2.c](lab4_2/lab4_2.c))
| Input | Output (delta = 1000) |
Expand Down
Binary file added images/House.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab4_1/House_in_hist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab4_1/House_out_1.0_0.6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab4_1/House_out_1.0_0.6_hist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab4_1/House_out_1.5_1.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab4_1/House_out_1.5_1.5_hist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions lab4_1/lab4_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "png_wrapper.h"
#include "histogram.h"

static void process_image(struct Image img, double c, double gamma)
{
for (int y = 0; y < img.height; y++)
for (int x = 0; x < img.width; x++)
{
double in = img.pixels[y][x] / 255.0;
int out = c * pow(in, gamma) * 255.0 + 0.5;
if (out > 255) out = 255;
img.pixels[y][x] = out;
}
}

int main(int argc, char * const argv[])
{
if (argc != 7)
error("usage: %s <in_file> <out_file> <hist1_out_file> "
"<hist2_out_file> <c (0.0-50.0)> <gamma (0.0-50.0)>", argv[0]);

char *input_filename = argv[1];
char *output_filename = argv[2];
char *hist1_filename = argv[3];
char *hist2_filename = argv[4];
char *s_c = argv[5];
char *s_gamma = argv[6];

double c = atof(s_c);
if (c < 0.0 || c > 50.0)
error("wrong c (%f)", c);

double gamma = atof(s_gamma);
if (gamma < 0.0 || gamma > 50.0)
error("wrong gamma (%f)", gamma);

struct Image img = read_grayscale_png(input_filename);
printf("Input file \"%s\" opened (width = %u, height = %u)\n",
input_filename, img.width, img.height);

struct Image hist1 = { HIST_WIDTH, HIST_HEIGHT };
alloc_pixels(&hist1);
histogram(img, hist1);
write_grayscale_png(hist1, hist1_filename);
free_pixels(hist1);

process_image(img, c, gamma);

struct Image hist2 = { HIST_WIDTH, HIST_HEIGHT };
alloc_pixels(&hist2);
histogram(img, hist2);
write_grayscale_png(hist2, hist2_filename);
free_pixels(hist2);

write_grayscale_png(img, output_filename);
free_pixels(img);

return 0;
}

0 comments on commit 5efc654

Please sign in to comment.