Blurring A or B channel in LAB color space

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
JoaCHIP
Posts: 37
Joined: 2004-12-21T04:22:18-07:00
Location: Copenhagen

Blurring A or B channel in LAB color space

Post by JoaCHIP »

Hello, I was just trying to remove some nasty color noise by converting my image into LAB color space, and then blurring the A and B channels, leaving the L channel intact. This is giving me some surprising results. What am I doing wrong? Here are the command lines I used and the resulting images:

The first attempt was to blur the L channel (which seems to be called "R" when using the channel command):
# convert noisy.jpg -colorspace LAB -channel R -blur 4x4 -channel RGB -colorspace RGB -quality 80 blurred_L_channel.jpg
Image
This is just what I expected. The colors are untouched, but the grayscale portion of the image is blurred.

Now trying the same command, but this time blurring the B channel instead:
# convert noisy.jpg -colorspace LAB -channel B -blur 4x4 -channel RGB -colorspace RGB -quality 80 blurred_L_channel.jpg
Image
Rather than blurring a part of the image, weird blue noise appears! But why? I would have expected the image to become only slightly more blurred, not showing entirely new features like this. The same happens when addressing the A channel "by specifying -channel G".

PS: I'm using ImageMagick 6.7.5-10 2012-05-23 Q16 compiled under FreeBSD from the official FreeBSD ports collection. The rest of the application is behaving very well, so don't think this is compile related.

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

Re: Blurring A or B channel in LAB color space

Post by fmw42 »

Post a link to your original image so others can test. Though I am not really used to working in LAB space.

Note after IM 6.7.5.5, RGB and sRGB were swapped (to make them correspond properly), though it may not have been completely changed until 6.7.6.7. So you may want to convert to sRGB rather than RGB. And you may want to upgrade to get past when the swap was complete.

Also -blur 4x4 is not really an effective blur as it does not allow the gaussian to roll-off to zero. So you are seeing only the top of the gaussian. In IM -blur radiusxsigma is usually used as radius=0 so that the radius is computed automatically as about 3xsigma. Or if you want a uniform weighted blur use radiusx65000.

None of this explains your problem, but was just meant to update you.

My guess is that you need to modify both ab channels together. So try

convert noisy.jpg -colorspace LAB -channel GB -blur 0x4 -channel RGB -colorspace sRGB -quality 80 blurred_AB_channels.jpg

In IM, except for cmyk, all channels are referred to as if RGB, so L->R, A->G, B->B (as you noted above).

JoaCHIP
Posts: 37
Joined: 2004-12-21T04:22:18-07:00
Location: Copenhagen

Re: Blurring A or B channel in LAB color space

Post by JoaCHIP »

Thanks for the update on the blur. Here's the original image, so that people can give it a try:
Image

When blurring the A and B channels you should get the following result. You can see that the tiny color variations in the noise have become less prominent, and somewhat more pleasing to the eye.
Image

Your above suggestion gave me this amusing result though. But I don't understand why it ends up like this:
Image

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

Re: Blurring A or B channel in LAB color space

Post by fmw42 »

I get pretty much the same as you. IM am on IM 6.7.7.2 Q16 (hdri) Mac OSX Snow Leopard. I don't know enough about LAB space to really comment. But it does look strange. I even tried separating channels, blurring the AB channels (with different amounts) and the combining them back together. But still got the same results. I don't know if this is a bug or not, but it does look odd. The IM developers or some expert on LAB would have to comment further.

How did you create the smoothed result that you are using above for comparison?

JoaCHIP
Posts: 37
Joined: 2004-12-21T04:22:18-07:00
Location: Copenhagen

Re: Blurring A or B channel in LAB color space

Post by JoaCHIP »

Well basically the L channel of LAB is perceptual brightness, i.e. a grayscale image. The A channel contains values ranging from green to magenta, with values around 50% being neutral gray. The B channel ranges from a cold yellow to sky blue.

Because all values are going from a minimum value to a maximum value, unlike the circular nature of the "hue" part of the HSL color space, applying effects to an image in LAB color space can be performed much like when applying the same effect to an RGB image. - Blur, sharpen etc. should work the exact same way.

A quick demonstration of the LAB color space, first the L channel, then A and then B:
Image
Image
Image
When combined you get the original image.

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

Re: Blurring A or B channel in LAB color space

Post by fmw42 »

Each channel when separate in LAB should be grayscale. You images show yellow/blue and pink/green. That should not be the case unless you combine them with black and/or white to form color images. Is that what you did?

How did you create your "properly" filtered result image from earlier above? Did you use Photoshop or some other tool? If so can you explain the steps you used. Perhaps they do something different when converting to/from LAB?

PS It works fine converting to YUV or YCbCr


convert noisy.jpg -colorspace YUV -channel GB -blur 0x4 -clamp -channel RGB -colorspace sRGB -quality 80 blurred_UV_channel.jpg

convert noisy.jpg -colorspace YCbCr -channel GB -blur 0x4 -clamp -channel RGB -colorspace sRGB -quality 80 blurred_CbCr_channel.jpg

JoaCHIP
Posts: 37
Joined: 2004-12-21T04:22:18-07:00
Location: Copenhagen

Re: Blurring A or B channel in LAB color space

Post by JoaCHIP »

I used Photoshop indeed. I didn't display them as grayscale in order to show what colors they actually are, but yes, they are represented as grayscale inside Photoshop. The exact steps in Photoshop are:

1) Convert the original to LAB color
2) Select the A and B channels (the color portion of the image)
3) Gaussian blur 3.0
4) Select all channels to view the result.

Good idae with YUV! It's pretty similar to LAB. I just tried YUV and YcBrCr color spaces, but they too look a bit weird, even though not as weird as LAB did. Still, the result is far from what I expected. The image is not supposed to become significantly brighter, the way it does with your two examples.

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

Re: Blurring A or B channel in LAB color space

Post by fmw42 »

I do not get any extra brightening in my version of IM with the code I put above.

Change the -colorspace sRGB to RGB or whatever you need to reverse it and try again. If that fails, then try some other viewer, such as in PS or something else. I displayed them in Mac Preview and they look the same brightness. They also look fine with convert image show:

original:
Image

YUV:
convert noisy.jpg -colorspace YUV -channel GB -blur 0x4 -clamp -channel RGB -colorspace sRGB -quality 80 blurred_UV_channel.jpg
Image


YIQ:
convert noisy.jpg -colorspace YIQ -channel GB -blur 0x4 -clamp -channel RGB -colorspace sRGB -quality 80 blurred_IQ_channel.jpg
Image


YCbCr:
convert noisy.jpg -colorspace YCbCr -channel GB -blur 0x4 -clamp -channel RGB -colorspace sRGB -quality 80 blurred_CbCr_channel.jpg
Image

P.S. I would suggest you report the LAB issue on the Bugs forum, since it seems to work correctly in PS and not IM and we can do it right using YUV, etc. You can point a link back to this topic as well. Show your input, your PS result and your IM result.

Note that if PS used blur 3.0, I would expect the equivalent in IM would either -blur 0x3 or -blur 0x1. The result depends upon whether PS uses the gaussian radius or sigma. If it uses sigma, then -blur 0x3, if it use radius, then it would be -blur 0x1, since the radius is approximate 3x sigma.
Last edited by fmw42 on 2012-06-25T11:04:52-07:00, edited 1 time in total.

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

Re: Blurring A or B channel in LAB color space

Post by fmw42 »

This is now fixed in IM 6.7.7.9 but you have to use IM in HDRI mode

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Blurring A or B channel in LAB color space

Post by anthony »

Due to a change in the way signed A,B values of lab is stored it should now work for non-HDRI versions of IM too.

From IM 6.7.8.2

UPDATE...

Just as a FYI, LAB channel images look a lot nicer now! They make much more sense, with no discontinuity (which was the cause of the issue.

Code: Select all

    convert rose: -colorspace LAB  -separate +append  lab_channels.png
Before...
Image

After...
Image

The above image displays 'as-is' and compares nicely with the previous image, in just about every image viewer I tried, but seems to display very light (perceptual gray) when displayed in firefox. Suggestions?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

JoaCHIP
Posts: 37
Joined: 2004-12-21T04:22:18-07:00
Location: Copenhagen

Re: Blurring A or B channel in LAB color space

Post by JoaCHIP »

Great works, guys. I'm gonna take a shot at playing around with all this as soon as I get the time.

PS: When embedding color profiles into png and jpeg images, each browser has its own surprising way to interpret it. My best work-around for this has been to convert images to sRGB and then completely remove the color profile from the image. Browsers are just silly. :-?

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Blurring A or B channel in LAB color space

Post by anthony »

The W3 Consortium insists that browsers generally treat all images, that has no other colorspace indicators, as being sRGB. That is why it works for you.

IM is now adopting this in the common image file formats, and correctly assigning sRGB to images that have no colorspace indicators.

This did not help with my previous example where I was trying to display a linear grayscale in a sRGB viewer. The older image came out dark (no gamma or sRGB correction), the newer image came out light (data is right, but browser applied a linear->sRGB perceptual correction).

I will need to figure that out, for my examples in IM examples.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

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

Re: Blurring A or B channel in LAB color space

Post by fmw42 »

Lab is a linear colorspace and not sRGB. Magick recently fixed that.

Post Reply