Page 1 of 1

Set "compose:clamp" to "off"

Posted: 2017-12-22T01:12:59-07:00
by Bery0za
Hey,

I'm trying to implement photoshop-like high-pass filter. One of the steps is subtracting blurred image from it`s original, but composing operation should keep values below zero. In ImageMagick this can be done with "-set option:compose:clamp=off". How can I do this in Magick.NET?

Code: Select all

public MagickImage HighPass(MagickImage src, double radius = 2)
{
	MagickImage dst = (MagickImage)src.Clone();
	dst.Settings.SetDefine("compose:clamp", "off"); // Has no effect		
	
	dst.GaussianBlur(radius, radius * 0.3);
	dst.Composite(src, CompositeOperator.MinusDst); // Here we need to keep values below zero
	dst.Evaluate(Channels.RGB, EvaluateOperator.Add, 128);
	
	return dst;
}

Re: Set "compose:clamp" to "off"

Posted: 2017-12-22T09:58:18-07:00
by fmw42
I do not know Magick.Net, but to effectively use -compose clamp=off, you need to be working in HDRI mode so you can have negative values. You also must store the result in an image format that allows negative values and fractions, such as MPC, PFM or TIFF with -define quantum:format=value.

But I do not think you need to use HDRI and clamp=off

You can do that with

convert image -colorspace gray -bias 50% -define convolve:scale=X\! -morphology Convolve DoG:0,0,radius result

where radius is typically 1 and X is some gain factor > 1.

See the high pass filter in the edge section of JqMagick at http://jqmagick.imagemagick.org

Re: Set "compose:clamp" to "off"

Posted: 2017-12-23T10:39:44-07:00
by Bery0za
Thanks for your advice. HDRI version of Magick.NET realy solves the problem. Clamping can be set to off with SetArtifact method of MagickImage class. Final result:

Code: Select all

public MagickImage HighPass(MagickImage src, double radius = 2)
{
	MagickImage dst = (MagickImage)src.Clone();	
	
	dst.GaussianBlur(radius, radius * 0.3); // Simple "magic number" to count sigma
	dst.SetArtifact("compose:clamp", "off");
	dst.Composite(src, CompositeOperator.MinusDst);
	dst.Evaluate(Channels.RGB, EvaluateOperator.Add, new Percentage(50));
	dst.Clamp(Channels.RGB);
	
	return dst;
}
I understand that "true" high-pass filter involves convolution, so the method you offered (without arguments checks and grayscaling) looks like:

Code: Select all

public MagickImage HighPass(MagickImage src, int gain = 1, double radius = 2)
{
	MagickImage dst = (MagickImage)src.Clone();

	MorphologySettings morphology = new MorphologySettings()
	{
		Channels = Channels.RGB,
		ConvolveBias = new Percentage(50),
		ConvolveScale = new MagickGeometry(gain, 0),
		Kernel = Kernel.DoG,
		KernelArguments = $"0, 0, {radius}",
		Method = MorphologyMethod.Convolve
	};

	dst.Morphology(morphology);

	return dst;
}

Re: Set "compose:clamp" to "off"

Posted: 2017-12-23T11:13:59-07:00
by fmw42
Can you post example images -- input and both output results, so we can see how much different they are. You can upload to any free image hosting service (such as dropbox.com) and put the URLs here.

Re: Set "compose:clamp" to "off"

Posted: 2017-12-23T12:11:21-07:00
by snibgo
Another possibility is to do the subtraction, and addition of 50%, in a single step with "-compose Mathematics".

Re: Set "compose:clamp" to "off"

Posted: 2017-12-23T12:37:55-07:00
by Bery0za
So here we go (click for full res)

Original
Image

Gaussian blur and subtracting, R = 2, sigma ≈ 1/3 * R
Image

Convolution, R = 2, Gain = 1
Image

Convolution, R = 2, Gain = 5
Image