图像过滤顺序

时间:2015-08-21 08:43:09

标签: c# image-processing

我正在尝试编写一个简单的照片编辑器,它允许用户将各种滤镜应用到所选图像上。 如何在AcdSee,IrfanView等中使用这些过滤器为用户操作的任何顺序实现相同的结果图像?

byte[] ApplyFilter1(byte[] img, int step)
{

    for(int i = 0; i < img.Length; i++)
    {
        img[i]+=step;//changing brightness
    }
    return img;
}

byte[] ApplyFilter2(byte[] img, int step)
{

    for(int i = 0; i < img.Length; i++)
    {
        img[i]*=step;//changing whatever
    }
    return img;
}

//1 situation - user sets filter1 and then filter2
img = ApplyFilter1(img, 10);
img = ApplyFilter2(img, 10);

//2 situation - user sets filter2 and then filter1
img = ApplyFilter2(img, 10);
img = ApplyFilter1(img, 10);

是否有任何默认的申请顺序? e.g。

  1. 亮度
  2. 对比度
  3. Gamma校正 ...
  4. 提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以通过为每个过滤器定义优先级来实现此目的,然后相应地进行处理:

您的形象:

public class Picture
{
    public int Width, Height;

    public Color[] GetPixels()
    {
        // ...
    }
}

您的过滤器,包含一些派生示例:

public abstract class Filter
{
    public virtual int Priority
    {
        get { return 0; }
    }

    public abstract Picture Apply(Picture picture);
}

public class BrightnessFilter : Filter
{
    public override int Priority
    {
        get { return 1; }
    }

    public override Picture Apply(Picture picture)
    {
        throw new NotImplementedException();
    }
}

public class ContrastFilter : Filter
{
    public override int Priority
    {
        get { return 2; }
    }

    public override Picture Apply(Picture picture)
    {
        throw new NotImplementedException();
    }
}

您的图片处理器,在应用过滤器之前先对其进行排序:

public class Processor
{
    public List<Filter> Filters { get; set; }

    public Pitcure Process(Picture picture)
    {
        var filters = Filters.OrderBy(s => s.Priority);
        var result = picture;
        foreach (var filter in filters)
        {
            result = filter.Apply(result);
        }

        return result;
    }
}

这是一种非常简单的方法,可以进一步增强:D

<强> PS:

您可以询问应用这些类型的过滤器的顺序@ https://graphicdesign.stackexchange.com/