Remove filter on image

1.4k Views Asked by At

I'm using AForge.net for my channel filtering, I have 3 button, red, blue and green. When I click on button red it will apply the filter of red channel. However, when I go on to click on the blue button, it will overlap the red and image goes dark.

Does anyone know how can I "dispose" red channel when blue is click, vice versa, so as the filter will not overlap each other? Below is a snippet of my code.

 private void redchannel_Click_1(object sender, EventArgs e)
    {
        try
        {

            pictureBox1.Image = pic;
            pictureBox2.Image = pic2;

            // create filter
            ChannelFiltering filter = new ChannelFiltering();
            // set channels' ranges to keep
            filter.Red = new IntRange(0, 255);
            filter.Green = new IntRange(255, 255);
            filter.Blue = new IntRange(255, 255);
            // apply the filter
            filter.ApplyInPlace(pic2);


        }


        catch (Exception)
        {
            throw new ApplicationException("Failed loading image");
        }
    }

    private void bluechannel_Click_1(object sender, EventArgs e)
    {
        try
        {
            pictureBox1.Image = pic;
            pictureBox2.Image = pic2;

            // create filter
            ChannelFiltering filter = new ChannelFiltering();
            // set channels' ranges to keep
            filter.Red = new IntRange(255, 255);
            filter.Green = new IntRange(255, 255);
            filter.Blue = new IntRange(0, 255);
            // apply the filter
            filter.ApplyInPlace(pic2);

        }




        catch (Exception)
        {
            throw new ApplicationException("Failed loading image");
        }
    }

    private void greenchannel_Click_1(object sender, EventArgs e)
    {
        try
        {
            pictureBox1.Image = pic;
            pictureBox2.Image = pic2;

            // create filter
            ChannelFiltering filter = new ChannelFiltering();
            // set channels' ranges to keep

            filter.Red = new IntRange(255, 255);
            filter.Green = new IntRange(0, 255);
            filter.Blue = new IntRange(255, 255);
            // apply the filter
            filter.ApplyInPlace(pic2);
        }
        catch (Exception)
        {
            throw new ApplicationException("Failed loading image");

        }
    }
2

There are 2 best solutions below

0
On

My best guess is what you are currently doing is that you initialize pic2 as a copy of the original pic. Then you keep adding filters to pic2. So what happens is that you have the clean image, then apply a 1st filter, a 2nd filter and so on.

I would change:

pictureBox1.Image = pic;
pictureBox2.Image = pic2;

for

pictureBox1.Image = pic;
pictureBox2.Image = pic.clone();

Another thing is you might want to point all 3 buttons to the same function. 90% of the current 3 functions you have is code repetition. A nightmare for maintenance.

An even better solution would be: Another way you could do it is keeping each button separate (3 btn click functions) where you call your new filtering function with the Red, Green & Blue as parameters.

2
On

You need to store the original image as well as a potentially modified display image. Perform your calculations on the original and show them with the display image. Never alter the original, only the display image.