Page 1 of 1

Color thresholding like VLC

Posted: 2015-04-29T04:08:04-07:00
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!

Re: Color thresholding like VLC

Posted: 2015-04-29T06:00:14-07:00
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

Re: Color thresholding like VLC

Posted: 2015-04-30T02:33:59-07:00
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

Re: Color thresholding like VLC

Posted: 2015-04-30T08:13:08-07:00
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.

Re: Color thresholding like VLC

Posted: 2015-04-30T09:36:01-07:00
by fmw42
If you are on Linux/MacOSX or Windows with Cygwin, see my script isolatecolor at the link below.

Re: Color thresholding like VLC

Posted: 2015-04-30T10:59:55-07:00
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!

Re: Color thresholding like VLC

Posted: 2015-04-30T14:59:33-07:00
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
)

Re: Color thresholding like VLC

Posted: 2015-04-30T16:09:37-07:00
by snibgo
Your IM convert command contains parentheses ( and ). When inside a for (), you need to escape them: ^( and ^).