Highlights, shadows, midtones of an image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
mallik
Posts: 5
Joined: 2017-12-19T04:20:50-07:00
Authentication code: 1152

Highlights, shadows, midtones of an image

Post by mallik »

Hi All,

My imagemagick verison is 7.0.7-8 Q16 x64 on windows platform.

I am looking for a script or set of commands to select the highlights, shadows, midtones regions of a given image so that I can apply effects like brightness/contrast on the selected regions.

Can somebody help me on this?

Best regards
Mallikharjuna
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Highlights, shadows, midtones of an image

Post by snibgo »

IM doesn't have the concept of "selecting" regions. Instead, it uses masking.

You can apply an effect to a copy of the entire image, then apply a different effect to another copy, then combine those with a mask that defines where you want either effect. You can repeat this as many times as you want.

For example, define masks that are white where we want the effect. We create two masks, for shadows and highlights. (Mid-tones are everywhere else.) Windows BAT syntax.

Code: Select all

%IM%convert ^
  rose: ^
  -colorspace Gray ^
  -threshold 25%% -negate ^
  ms.png

%IM%convert ^
  rose: ^
  -colorspace Gray ^
  -threshold 75%% ^
  mh.png
Now create three effects.

Code: Select all

%IM%convert ^
  rose: ^
  -fill Blue ^
  -colorize 50 ^
  e1.png

%IM%convert ^
  rose: ^
  -colorspace gray ^
  e2.png

%IM%convert ^
  rose: ^
  -fill Yellow ^
  -colorize 50 ^
  e3.png
Now apply effect 1 to the shadows, effect 3 to the highlights, and effect 2 to everywhere else.

Code: Select all

%IM%convert ^
  e2.png ^
  e1.png ^
  ms.png ^
  -compose Over -composite ^
  e3.png ^
  mh.png ^
  -compose Over -composite ^
  out.png
snibgo's IM pages: im.snibgo.com
mallik
Posts: 5
Joined: 2017-12-19T04:20:50-07:00
Authentication code: 1152

Re: Highlights, shadows, midtones of an image

Post by mallik »

Hi snibgo,

Thank you for the information.

We will check these commands and update you.

However, we have one more requirement to apply the fuzz operation on the highlights/shadows/midtones. Below is the requirement:

1. Select/create masks for highlights/shadows/midtones based on the threshold value.

2. Apply fuzz operator for the above step to expand the color range.

3. Apply different effects for the above step.

We are to trying to develop a feature similar to photoshop's "Select -> Color Range" command.

Can you please let us know Imagemagick commands for the above steps?

Thanks in advance.

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

Re: Highlights, shadows, midtones of an image

Post by fmw42 »

To add fuzz, I presume you mean you want a ramped mask. So add -blur radiusxsigma (typically 0xsigma for gaussian blurred ramp or radiusx65000 for linear blurred ramp) -level 50x100% after the threshold operation in snibgo's command.

You have not specified what effects you want. Please be more specific. Most IM users are not familiar with nor have Photoshop. So you may need to be more specific about what "Select -> Color Range" means or show examples and dialog windows.
mallik
Posts: 5
Joined: 2017-12-19T04:20:50-07:00
Authentication code: 1152

Re: Highlights, shadows, midtones of an image

Post by mallik »

Hi Fred,

Thank you for the information.

Below is youtube link for Photoshop color range feature; I am not sure whether I can post yourtube link in this forum; if not allowed, my apologies for posting the link.

https://www.youtube.com/watch?v=Xk8nJfwOqJc

We need to use fuzz operation on highlights/shadows/midtones regions/masks in the image.

The effects are apply brightness/contrast, Hue/Saturation on the selected regions.

Please let us know for any clarifications.

Best regards
Mallikharjuna
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Highlights, shadows, midtones of an image

Post by snibgo »

I have internet problems, and can't watch videos.
mallik wrote:2. Apply fuzz operator for the above step to expand the color range.
I showed a simple threshold for selection, so the mask pixels were either black or white, and each pixel was either in or out.

Instead, you can use a ramp:

Code: Select all

%IM%convert ^
 rose: ^
 -colorspace Gray ^
 -level 20,30%% -negate ^
 ms.png
This is a linear ramp, which is usually good enough.
snibgo's IM pages: im.snibgo.com
mallik
Posts: 5
Joined: 2017-12-19T04:20:50-07:00
Authentication code: 1152

Re: Highlights, shadows, midtones of an image

Post by mallik »

Hi Snibgo,

Thank you for the information.

We will check these commands and let you know.

We uploaded the youtube video in google drive and please download from below link:

https://drive.google.com/open?id=1xLRd3 ... 5LDTBtrxOI

Best regards
Mallikharjuna
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Highlights, shadows, midtones of an image

Post by snibgo »

My internet problem is pathetically low speeds (eg 8000 bits per second) when it works at all.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Highlights, shadows, midtones of an image

Post by fmw42 »

Here is one approach. For shadows, midtowns and highlights, extract the appropriate gray or color channel image. Then clone it and threshold it in the shadow (dark 33% or as desired), or highlights (bright 33% or as desired), or midtowns (middle 33% or as desired). Multiply the thresholded mask by the channel image to get a final mask. Then process the whole input image with -modulate or -brightness-contrast. Then composite the input with the modified image using the mask.

For selected colors, just use -fuzz XX% with -fill ... -opaque to make a binary mask. Then process the whole image and do a similar composite of the original with modified image using the mask.

For example (Unix syntax):

Input:
Image


Increase brightness and contrast in the midtones:

Code: Select all

convert monet2.jpg \
\( -clone 0 -colorspace gray \) \
\( -clone 1 -white-threshold 67% -black-threshold 33% \
-fill black -opaque white -fill white +opaque black \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 \
\( -clone 0 -brightness-contrast 20,20 \) \
+swap -compose over -composite \
monet_midtones_bc_20_20.png
Image

Modulate the Yellow channel highlights to change yellow colors to greenish colors:

Code: Select all

convert monet2.jpg \
\( -clone 0 -colorspace CMYK -channel Y -separate +channel \) \
\( -clone 1 -threshold 67% \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 \
\( -clone 0 -modulate 100,100,150 \) \
+swap -compose over -composite \
monet_highlights_yellow_hue_150.png
Image


Select the orange color at the base of flowers and modulate to greenish colors:

Code: Select all

convert monet2.jpg \
\( -clone 0 -fuzz 10% -fill white -opaque "rgb(200,118,47)" \
-fill black +opaque white \) \
\( -clone 0 -modulate 100,100,150 \) \
+swap -compose over -composite \
monet_selected_color_hue_150.png
Image


If on windows, remove the \ before \( ... \) to become ( ... ) and replace the end of line \ with ^
mallik
Posts: 5
Joined: 2017-12-19T04:20:50-07:00
Authentication code: 1152

Re: Highlights, shadows, midtones of an image

Post by mallik »

Hi Fred,

Thank you for sending the commands.

We will execute these commands and update you.

Best regards
Mallikharjuna
Post Reply