Loading s5obj_lang_labo_6...
public Image Apply(Image input) { int width = input.GetWidth(); int height = input.GetHeight(); string name = input.GetName();
Image result = new Image(width, height, name);
int[,] gx = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
int[,] gy = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { float sumX = 0, sumY = 0;
for (int j = -1; j <= 1; j++) { for (int i = -1; i <= 1; i++) { Color c = input.GetPixel(x + i, y + j); float intensity = 0.299f * c.GetR() + 0.587f * c.GetG() + 0.114f * c.GetB();
sumX += intensity * gx[j + 1, i + 1]; sumY += intensity * gy[j + 1, i + 1]; } }
float mag = MathF.Sqrt(sumX * sumX + sumY * sumY); mag = Math.Clamp(mag, 0f, 1f);
result.SetPixel(x, y, new Color(mag, mag, mag)); } }
return result; }
0.00 0.50 1.00
0.12 → 0.00 0.42 → 0.50 0.88 → 1.00
private float Quantize(float v) { return MathF.Round(v * (levels - 1)) / (levels - 1); }
public class SharpenFilter : IImageFilter { private readonly float strength; private readonly int blurRadius;
public SharpenFilter(float strength, int blurRadius) { if (strength < 0) throw new ArgumentException("Strength must be >= 0");
if (blurRadius < 1) throw new ArgumentException("Blur radius must be >= 1");
this.strength = strength; this.blurRadius = blurRadius; }
public Image Apply(Image input) { var blur = new BlurFilter(blurRadius); var blurred = blur.Apply(input);
int width = input.GetWidth(); int height = input.GetHeight(); string name = input.GetName();
Image result = new Image(width, height, name);
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color orig = input.GetPixel(x, y); Color blurC = blurred.GetPixel(x, y);
float r = orig.GetR() + strength * (orig.GetR() - blurC.GetR()); float g = orig.GetG() + strength * (orig.GetG() - blurC.GetG()); float b = orig.GetB() + strength * (orig.GetB() - blurC.GetB());
// clamp pour rester dans [0,1] r = Math.Clamp(r, 0f, 1f); g = Math.Clamp(g, 0f, 1f); b = Math.Clamp(b, 0f, 1f);
result.SetPixel(x, y, new Color(r, g, b)); } }
return result; }
//methode Description(): TODO }
// Pipelines in Program class
static void PipeLineComicBook(Image image, Logger logger) { // --- filling --- ImageProcessor p1 = new ImageProcessor("ComicBook_Filling", logger); p1.AddFilter(new GrayFilter()); p1.AddFilter(new BlurFilter(2)); p1.AddFilter(new ThresholdFilter(0.5f));
Image fill = p1.Process(image);
// --- edges --- ImageProcessor p2 = new ImageProcessor("ComicBook_Edges", logger); p2.AddFilter(new GrayFilter()); p2.AddFilter(new EdgeFilter()); p2.AddFilter(new InvertFilter()); p2.AddFilter(new ThresholdFilter(0.3f));
Image edges = p2.Process(image);
// --- combination --- ImageProcessor p3 = new ImageProcessor("ComicBook", logger); p3.AddFilter(new MultiplyFilter(edges));
Image result = p3.Process(fill); string path = "results/" + image.GetName() + "_" + p3.GetName() + ".ppm"; result.Save(path); }
static void PipeLineCartoon(Image image, Logger logger) { // --- colors --- ImageProcessor p1 = new ImageProcessor("Cartoon_Colors", logger); p1.AddFilter(new BlurFilter(2)); p1.AddFilter(new BlurFilter(2)); p1.AddFilter(new QuantizeFilter(4)); p1.AddFilter(new ContrastFilter(1.2f));
Image smooth = p1.Process(image);
// --- edges --- ImageProcessor p2 = new ImageProcessor("Cartoon_Edges", logger); p2.AddFilter(new GrayFilter()); p2.AddFilter(new EdgeFilter()); p2.AddFilter(new InvertFilter()); p2.AddFilter(new ThresholdFilter(0.3f));
Image edges = p2.Process(image);
// --- fusion --- ImageProcessor p3 = new ImageProcessor("Cartoon", logger); p3.AddFilter(new MultiplyFilter(edges));
Image result = p3.Process(smooth); string path = "results/" + image.GetName() + "_" + p3.GetName() + ".ppm"; result.Save(path); }
static void PipeLineWaterColor(Image image, Logger logger) { // --- couleurs --- ImageProcessor p1 = new ImageProcessor("WaterCorol_Colors", logger); p1.AddFilter(new BlurFilter(3)); p1.AddFilter(new BlurFilter(2)); p1.AddFilter(new QuantizeFilter(6)); p1.AddFilter(new ContrastFilter(0.9f));
Image smooth = p1.Process(image);
// --- contours --- ImageProcessor p2 = new ImageProcessor("WaterColor_Edges", logger); p2.AddFilter(new GrayFilter()); p2.AddFilter(new BlurFilter(1)); p2.AddFilter(new EdgeFilter()); p2.AddFilter(new InvertFilter()); p2.AddFilter(new ContrastFilter(0.7f));
Image edges = p2.Process(image);
// --- fusion --- ImageProcessor p3 = new ImageProcessor("WaterColor", logger); p3.AddFilter(new BlendFilter(edges, 0.6f));
Image result = p3.Process(smooth); string path = "results/" + image.GetName() + "_" + p3.GetName() + ".ppm"; result.Save(path); }
static void PipeLinePainting(Image image, Logger logger) { ImageProcessor processor = new ImageProcessor("Painting", logger); processor.AddFilter(new BlurFilter(2)); processor.AddFilter(new QuantizeFilter(6)); processor.AddFilter(new ContrastFilter(1.3f)); processor.AddFilter(new SharpenFilter(1.5f, 2)); Image result = processor.Process(image); string path = "results/" + image.GetName() + "_" + processor.GetName() + ".ppm"; result.Save(path); }
static void PipeLinePopArt(Image image, Logger logger) { ImageProcessor processor = new ImageProcessor("PopArt", logger); processor.AddFilter(new QuantizeFilter(3)); processor.AddFilter(new ContrastFilter(1.8f)); processor.AddFilter(new InvertFilter()); processor.AddFilter(new BlendFilter(image, 0.3f)); processor.AddFilter(new ContrastFilter(2.0f)); Image result = processor.Process(image); string path = "results/" + image.GetName() + "_" + processor.GetName() + ".ppm"; result.Save(path); }
static void PipeLinePencil(Image image, Logger logger) { ImageProcessor processor = new ImageProcessor("Pencil", logger); processor.AddFilter(new GrayFilter()); processor.AddFilter(new BlurFilter(1)); processor.AddFilter(new EdgeFilter()); processor.AddFilter(new InvertFilter()); processor.AddFilter(new ContrastFilter(1.5f)); Image result = processor.Process(image); string path = "results/" + image.GetName() + "_" + processor.GetName() + ".ppm"; result.Save(path); }
// Filters implemented in this lab
public class ThresholdFilter : IImageFilter { private float threshold;
public ThresholdFilter(float threshold) { if (threshold < 0f || threshold > 1f) throw new ArgumentException("Threshold must be in [0,1]");
this.threshold = threshold; }
public Image Apply(Image input) { int width = input.GetWidth(); int height = input.GetHeight(); string name = input.GetName();
Image result = new Image(width, height, name);
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c = input.GetPixel(x, y); float i = c.Gray(); float v = i > threshold ? 1f : 0f;
result.SetPixel(x, y, new Color(v, v, v)); } }
return result; }
public string Description() { return $"Threshold(threshold={threshold})"; } }
public class EdgeFilter : IImageFilter { public Image Apply(Image input) { int width = input.GetWidth(); int height = input.GetHeight(); string name = input.GetName();
Image result = new Image(width, height, name);
int[,] gx = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
int[,] gy = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { float sumX = 0, sumY = 0;
for (int j = -1; j <= 1; j++) { for (int i = -1; i <= 1; i++) { var c = input.GetPixel(x + i, y + j); float intensity = 0.299f * c.GetR() + 0.587f * c.GetG() + 0.114f * c.GetB();
sumX += intensity * gx[j + 1, i + 1]; sumY += intensity * gy[j + 1, i + 1]; } }
float mag = MathF.Sqrt(sumX * sumX + sumY * sumY); mag = Math.Clamp(mag, 0f, 1f);
result.SetPixel(x, y, new Color(mag, mag, mag)); } }
return result; }
public string Description() { return "Edge"; } }
public class MultiplyFilter : IImageFilter { private Image other;
public MultiplyFilter(Image other) { this.other = other; }
public Image Apply(Image input) { int width = input.GetWidth(); int height = input.GetHeight();
if (width != other.GetWidth() || height != other.GetHeight()) throw new ArgumentException("Images must have same size");
string name = input.GetName();
Image result = new Image(width, height, name);
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color a = input.GetPixel(x, y); Color b = other.GetPixel(x, y);
result.SetPixel(x, y, new Color( a.GetR() * b.GetR(), a.GetG() * b.GetG(), a.GetB() * b.GetB() )); } }
return result; }
public string Description() { return $"Multiply(other={other.GetName()}"; } }
public class QuantizeFilter : IImageFilter { private int levels;
public QuantizeFilter(int levels) { if (levels <= 1) throw new ArgumentException("Levels must be > 1");
this.levels = levels; }
public Image Apply(Image input) { int width = input.GetWidth(); int height = input.GetHeight(); string name = input.GetName();
Image result = new Image(width, height, name);
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c = input.GetPixel(x, y);
result.SetPixel(x, y, new Color( Quantize(c.GetR()), Quantize(c.GetG()), Quantize(c.GetB()) )); } }
return result; }
private float Quantize(float v) { return MathF.Round(v * (levels - 1)) / (levels - 1); }
public string Description() { return $"Quantize(levels={levels}"; } }
public class BlendFilter : IImageFilter { private Image other; private float alpha;
public BlendFilter(Image other, float alpha) { if (alpha < 0f || alpha > 1f) throw new ArgumentException("Alpha must be in [0,1]");
this.other = other; this.alpha = alpha; }
public Image Apply(Image input) { int width = input.GetWidth(); int height = input.GetHeight();
if (width != other.GetWidth() || height != other.GetHeight()) throw new ArgumentException("Images must have same size");
string name = input.GetName();
Image result = new Image(width, height, name);
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color img1 = input.GetPixel(x, y); Color img2 = other.GetPixel(x, y);
float r = alpha * img1.GetR() + (1 - alpha) * img2.GetR(); float g = alpha * img1.GetG() + (1 - alpha) * img2.GetG(); float b = alpha * img1.GetB() + (1 - alpha) * img2.GetB();
result.SetPixel(x, y, new Color(r, g, b)); } }
return result; }
public string Description() { return $"Blend(alpha={alpha}, other={other.GetName()})"; } }
public class SharpenFilter : IImageFilter { private float strength; private int blurRadius;
public SharpenFilter(float strength, int blurRadius) { if (strength < 0) throw new ArgumentException("Strength must be >= 0");
if (blurRadius < 1) throw new ArgumentException("Blur radius must be >= 1");
this.strength = strength; this.blurRadius = blurRadius; }
public Image Apply(Image input) { BlurFilter blur = new BlurFilter(blurRadius); Image blurred = blur.Apply(input);
int width = input.GetWidth(); int height = input.GetHeight(); string name = input.GetName();
Image result = new Image(width, height, name);
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color orig = input.GetPixel(x, y); Color blurC = blurred.GetPixel(x, y);
float r = orig.GetR() + strength * (orig.GetR() - blurC.GetR()); float g = orig.GetG() + strength * (orig.GetG() - blurC.GetG()); float b = orig.GetB() + strength * (orig.GetB() - blurC.GetB());
// clamp pour rester dans [0,1] r = Math.Clamp(r, 0f, 1f); g = Math.Clamp(g, 0f, 1f); b = Math.Clamp(b, 0f, 1f);
result.SetPixel(x, y, new Color(r, g, b)); } }
return result; }
public string Description() { return $"Sharpen(strength={strength}, blurRadius={blurRadius})"; } }