Feather / anti aliasing / remove jagged lines

Sep 5, 2014 at 9:04 AM
Edited Sep 19, 2014 at 10:07 AM
How would I remove the jagged lines in the following image? At the minute I am resizing the image but i is very CPU and RAM intensive. is there an alternative?
Coordinator
Sep 5, 2014 at 9:06 AM
I don't really understand what you are trying to do. Can you post a small piece of example code and an input/output image?
Sep 5, 2014 at 9:29 AM
Edited Sep 5, 2014 at 11:33 AM
I have an emf file with jagged lines:

Image

This is what I am looking:

Image

Here is the code I am using at the minute that uses alot of resources:
    string fileType = "png";

    int densityPercentage = 800;
    int scaleUpBy = 4;
    int resizePercentage = (int)(((double)1 / scaleUpBy) * 100);

    MagickReadSettings settings = new MagickReadSettings();
    settings.Density = new MagickGeometry(new Percentage(densityPercentage * scaleUpBy), new Percentage(densityPercentage * scaleUpBy));
    settings.ColorSpace = ColorSpace.RGB;
    using (MagickImage image = new MagickImage())
    {
        // Add all the pages of the pdf file to the collection
        image.Read(@"C:\Users\User\Desktop\8060.emf", settings);
        image.Format = (MagickFormat)Enum.Parse(typeof(MagickFormat), fileType, true);
        image.BackgroundColor = new MagickColor(255, 255, 255);

        image.Resize(new Percentage(resizePercentage));

        image.Write(@"C:\Users\User\Desktop\test1." + fileType);
    }
Coordinator
Sep 5, 2014 at 10:58 AM
Edited Sep 5, 2014 at 10:59 AM
The settings.Density property does not support percentages. You are reading the image at 1200 DPI. You probably don't need a value that high. I think you should rewrite your code to something like this:
string fileType = "png";

int inputDensity = new MagickGeometry(300);
int outputDensity = new MagickGeometry(72);

MagickReadSettings settings = new MagickReadSettings();
settings.Density = inputDensity;
settings.ColorSpace = ColorSpace.RGB;
using (MagickImage image = new MagickImage())
{
    // Add all the pages of the pdf? file to the collection
    image.Read(@"C:\Users\User\Desktop\8060.emf", settings);
    image.Format = (MagickFormat)Enum.Parse(typeof(MagickFormat), fileType, true);
    image.BackgroundColor = new MagickColor(255, 255, 255);

    image.Resample(outputDensity);

    image.Write(@"C:\Users\Cathal Colgan\Desktop\test1." + fileType);
}
Sep 5, 2014 at 11:33 AM
Edited Sep 5, 2014 at 11:34 AM
I need a large Image to zoom into. It works the same way google maps works. Thats why the settings.Density property is set so high.

I want to do somthing like this but I am not sure how to do it: www.fmwconcepts.com/imagemagick/feather, opposed to resizing the image as it takes alot of resources to resize the image.

And using the Resample method seems to change the aspect ratio of the image.
Coordinator
Sep 5, 2014 at 12:10 PM
I don't think Resample should change the ratio of the image, that might be a bug.

I don't understand the problem you are trying to solve? You don't want to resize the image but you do want a smaller image then the original?
Sep 5, 2014 at 1:20 PM
I dont know if you know much about Anti-aliasing but anti-aliasing is a software technique used for smoothing the jagged appearance of diagonal lines in a bitmapped image. The pixels that surround the edges of the line are changed to varying shades of gray or color in order to blend the sharp edge into the background. Here is an example:

Image

There are different anti-aliasing methods that you can use to remove the jagged appearance one is the resizing method that I am using, however this is very intensive on the CPU and RAM because you are creating 4 times the size of image that is needed, hence using 4 times more CPU and RAM. So I was looking for an alternative to this method.

I have fould a few link on the image magick website

Feathering

Distance Feather

I was wondering could you translate them so I could use them on .NET.

Thanks for the help
Sep 5, 2014 at 1:25 PM
Here is another solution, could you tranlate this. I have tryed, with no luck.
convert Sir_amik_varze_detail.PNG \
\( -clone 0 -alpha off \) \
\( -clone 0 -alpha extract -blur 1x65000 -level 50x100% \) \
-delete 0 -compose copy_opacity -composite Sir_amik_varze_detail1.png

convert Sir_amik_varze_detail.PNG -channel A -blur 1x65000 -level 50x100% \
-channel rgba Sir_amik_varze_detail1b.png
Coordinator
Sep 5, 2014 at 4:12 PM
Edited Sep 5, 2014 at 4:12 PM
Can you add a link to your image? I mean the original emf file (8060.emf).
Sep 8, 2014 at 9:49 AM
Coordinator
Sep 8, 2014 at 11:39 AM
It seems that there is an option to modify ImageMagick to force it to antialias (http://stackoverflow.com/questions/1422949/emf-with-forced-antialiasing). Please give me a couple of days to investigate this.
Coordinator
Sep 11, 2014 at 11:59 AM
Edited Sep 11, 2014 at 12:00 PM
The ConvertToEmfPlus method described in the stackoverflow post did not work. And writing my own enumeration sounds like too much work. I have a simpler work-around for you. You could just blur the image a bit:
MagickReadSettings settings = new MagickReadSettings()
{
    Density = new MagickGeometry(1200)
};

using (MagickImage image = new MagickImage(@"D:\images\emf\8060.emf", settings))
{
    // If you don't do this the alpha channel will also be blurred.
    image.Alpha(AlphaOption.Deactivate);

    image.Blur(1, Quantum.Max);
    image.Write(@"D:\images\emf\test.png");
}