Color thresholding like VLC

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
AlexK
Posts: 4
Joined: 2015-04-29T04:00:15-07:00
Authentication code: 6789

Color thresholding like VLC

Post by AlexK »

VLC media player has a filter option called Color Threshold where it can convert a video to greyscale except colors similar to a particular value (in my case red or FF0000). The result can be a bit like the effects in the movie Sin City but not as clean, which doesn't really matter to me. Is something like this possible to do in ImageMagick? What I'm doing currently is filtering the video using VLC, exporting it, making it into frames using MPlayer, further editing them using ImageMagick, and ultimately converting them into a gif. Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color thresholding like VLC

Post by snibgo »

We can create a greyscale version of the colour image.

We can compose the grey version over the colour version, using a mask. The mask should be white where the original is red, and grey down to black otherwise.

In the following, colours that are within 30% of red will be shown in colour. More than 40% away from red will be monochrome. Between 30% and 40% will be varied saturation.

Creating the clut needs doing once only. Then do the second command for each frame. I use the in-built "rose:" image as the sample input.

Windows BAT syntax.

Code: Select all

%IM%convert ^
  -size 1x30 xc:white ^
  -size 1x10 gradient:white-black ^
  -size 1x60 xc:black ^
  -append ^
  clut.png

%IM%convert ^
  rose: ^
  ( -clone 0 ^
    -colorspace Gray ^
  ) ^
  +swap ^
  ( -clone 1 ^
    -fill Red -colorize 100 ^
  ) ^
  ( -clone 1,2 ^
    -compose Difference -composite ^
    -grayscale RMS ^
    clut.png -clut ^
  ) ^
  -delete 2 ^
  -alpha off ^
  -compose Over -composite ^
  roseColBw.png
snibgo's IM pages: im.snibgo.com
AlexK
Posts: 4
Joined: 2015-04-29T04:00:15-07:00
Authentication code: 6789

Re: Color thresholding like VLC

Post by AlexK »

Thank you very much for your response! The result definitely looks like what I'm going for. I'm pretty new to ImageMagick so sorry for asking, but how to I change the source image? I'd like to mogrify a few hundred images for an animation and everything I change gives me unexpected results
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color thresholding like VLC

Post by snibgo »

Instead of "rose:", put the name of a file. Or put %1 and call the BAT file with the filename as parameter. Or write a script that loops through the files.

I don't use mogrify. I would use convert in a loop, although a loop is slower.

If you use Windows, you will see plenty of examples of this on my website.
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: Color thresholding like VLC

Post by fmw42 »

If you are on Linux/MacOSX or Windows with Cygwin, see my script isolatecolor at the link below.
AlexK
Posts: 4
Joined: 2015-04-29T04:00:15-07:00
Authentication code: 6789

Re: Color thresholding like VLC

Post by AlexK »

snibgo wrote:Instead of "rose:", put the name of a file. Or put %1 and call the BAT file with the filename as parameter. Or write a script that loops through the files.

I don't use mogrify. I would use convert in a loop, although a loop is slower.

If you use Windows, you will see plenty of examples of this on my website.
Thanks! I guess I didn't wait long enough. That's what I first tried but it didn't do anything. I just tried again and it took about a full minute to convert the image I was testing it with. Thank you all for your helpful responses!
AlexK
Posts: 4
Joined: 2015-04-29T04:00:15-07:00
Authentication code: 6789

Re: Color thresholding like VLC

Post by AlexK »

I thought I would be able to put it into a loop with no problems but I guess I was wrong. I can do commands within the loop, but when I try the code given in the first response it has a problem with +swap saying it wasn't expected at this time. I can do any other ImageMagick operation I've tried in the loop without issue. Any ideas? Here's what I tried to do:

Code: Select all

for %%i in (C:\Users\Alex\Desktop\test\*.png) do (
%IM%convert ^
  -size 1x30 xc:white ^
  -size 1x10 gradient:white-black ^
  -size 1x60 xc:black ^
  -append ^
  clut.png

%IM%convert ^
  %%i ^
  ( -clone 0 ^
    -colorspace Gray ^
  ) ^
  +swap ^
  ( -clone 1 ^
    -fill Red -colorize 100 ^
  ) ^
  ( -clone 1,2 ^
    -compose Difference -composite ^
    -grayscale RMS ^
    clut.png -clut ^
  ) ^
  -delete 2 ^
  -alpha off ^
  -compose Over -composite ^
 %%i
)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color thresholding like VLC

Post by snibgo »

Your IM convert command contains parentheses ( and ). When inside a for (), you need to escape them: ^( and ^).
snibgo's IM pages: im.snibgo.com
Post Reply