Set "compose:clamp" to "off"

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
Bery0za
Posts: 3
Joined: 2017-12-21T23:03:06-07:00
Authentication code: 1152

Set "compose:clamp" to "off"

Post by Bery0za » 2017-12-22T01:12:59-07:00

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;
}
Last edited by Bery0za on 2017-12-22T23:30:31-07:00, edited 1 time in total.
Sorry if I've messed up something, English is not my native.

User avatar
fmw42
Posts: 23393
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2017-12-22T09:58:18-07:00

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

Bery0za
Posts: 3
Joined: 2017-12-21T23:03:06-07:00
Authentication code: 1152

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

Post by Bery0za » 2017-12-23T10:39:44-07:00

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;
}
Sorry if I've messed up something, English is not my native.

User avatar
fmw42
Posts: 23393
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2017-12-23T11:13:59-07:00

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.

snibgo
Posts: 10360
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo » 2017-12-23T12:11:21-07:00

Another possibility is to do the subtraction, and addition of 50%, in a single step with "-compose Mathematics".
snibgo's IM pages: im.snibgo.com

Bery0za
Posts: 3
Joined: 2017-12-21T23:03:06-07:00
Authentication code: 1152

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

Post by Bery0za » 2017-12-23T12:37:55-07:00

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
Sorry if I've messed up something, English is not my native.

Post Reply