Formula to insert a red cross if there are color pixels

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
troller
Posts: 12
Joined: 2014-04-21T14:52:51-07:00
Authentication code: 6789

Formula to insert a red cross if there are color pixels

Post by troller »

Hello all.

I am trying to figure out a quick way to do this:
I have a series of images (1920x1080 pixels each) that contain a set of green, yellow and sometimes also red dots. Each dot is a circle with a diameter of roughly 15-16 pixels. I would like to write a formula/script that does this:

1) It looks if in the image there is at least one red pixel (red with a certain fuzz factor, since they are jpg compressed and it is not always pure red)
2) If there is, then it creates a big red cross and position it in a given location on the same image. If there are no red pixels at all, then it does nothing.

How can I do this?

Thanks a lot!
Andrea
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Formula to insert a red cross if there are color pixels

Post by snibgo »

1) You could make non-red pixels transparent, and see if "sparse-colors:" emits anything. If it does, you have at least one red pixel.

Code: Select all

convert in.jpg -fuzz 15% +transparent red sparse-color:
2) You could use "-draw".

The rest is merely scripting.
snibgo's IM pages: im.snibgo.com
troller
Posts: 12
Joined: 2014-04-21T14:52:51-07:00
Authentication code: 6789

Re: Formula to insert a red cross if there are color pixels

Post by troller »

snibgo wrote:1) You could make non-red pixels transparent, and see if "sparse-colors:" emits anything. If it does, you have at least one red pixel.

Code: Select all

convert in.jpg -fuzz 15% +transparent red sparse-color:
2) You could use "-draw".

The rest is merely scripting.
Thanks but how do I automate that I create the red cross if the sparse-color tells me that there is at least one red pixel?

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

Re: Formula to insert a red cross if there are color pixels

Post by snibgo »

Your script would test whether sparse-colour produced any output. If there is output, the script would execute the convert command that draws the cross.

If you need help writing the script, it might be useful if you said what language you would use. But this is an ImageMagick forum, not a scripting forum. There are many sites where you can learn how to script.
snibgo's IM pages: im.snibgo.com
troller
Posts: 12
Joined: 2014-04-21T14:52:51-07:00
Authentication code: 6789

Re: Formula to insert a red cross if there are color pixels

Post by troller »

I am using Windows, Batch file.

Yes actually I'm not very good at all in scripting and compiling.
troller
Posts: 12
Joined: 2014-04-21T14:52:51-07:00
Authentication code: 6789

Re: Formula to insert a red cross if there are color pixels

Post by troller »

What I have now very simply is this:

Code: Select all

composite -geometry +1325+30 scartologo.bmp Screenshot.jpg Final.jpg
The pictures "scartologo" and "screenshot" are already saved in the folder. I just need to create a trigger to do this line; otherwise, this line is just skipped in the overall program.

Thanks!
Andrea
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Formula to insert a red cross if there are color pixels

Post by snibgo »

Code: Select all

for /F "usebackq" %%L in (`convert ^
  Screenshot.jpg -fuzz 15%% +transparent red ^
  sparse-color:`) do set RED_PX=%%L

if not "RED_PX"=="" composite ^
  -geometry +1325+30 scartologo.bmp Screenshot.jpg Final.jpg
Adjust the 15%% as required.
snibgo's IM pages: im.snibgo.com
troller
Posts: 12
Joined: 2014-04-21T14:52:51-07:00
Authentication code: 6789

Re: Formula to insert a red cross if there are color pixels

Post by troller »

Thanks,
but it seems that no matter what color I choose as condition, it will always run the command -> merge the two pictures, even if I chose a color that does not even exist in the picture.

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

Re: Formula to insert a red cross if there are color pixels

Post by snibgo »

What version of IM are you using?

Please paste the exact commands you are using. Please put a sample input image somewhere like dropbox.com and paste the URL here.
snibgo's IM pages: im.snibgo.com
troller
Posts: 12
Joined: 2014-04-21T14:52:51-07:00
Authentication code: 6789

Re: Formula to insert a red cross if there are color pixels

Post by troller »

I am using IM 6.8.8-9 Q16 64 bit on Windows 7.

The code I am using now is:

Code: Select all

:loop
@echo off

cd "C:\4300 Results\Screenshot\"

for /F "usebackq" %%L in (`convert ^
  Screenshot.jpg -fuzz 1%% +transparent blue ^
  sparse-color:`) do set RED_PX=%%L

if not "RED_PX"=="" composite ^
  -geometry +94+36 "C:\4300 Results\Screenshot\IM-ACo screenshot script\scartologo1.bmp" Screenshot.jpg Screenshot2.jpg

timeout /t 10
goto loop
where as you can see I am looping the command every 10 seconds and the 2 pictures to be merged are in two different folders; the "screenshot.jpg" picture is automatically updated every 5-6 seconds from another source program and saved in the folder...

At this link you can download both screenshot.jpg and scartologo1.bmp. : http://we.tl/j3A0LW8N5C

Note that in the script above I purposely wrote +transparent blue with 1% fuzz, and even if there is no blue color on the screenshot.jpg, it would still output the red cross over...

thanks for help!!!
Andrea
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Formula to insert a red cross if there are color pixels

Post by snibgo »

Sorry. In my code, I omitted the % signs in ...

Code: Select all

if not "RED_PX"=="" composite ^
... should be ...

Code: Select all

if not "%RED_PX%"=="" composite ^
snibgo's IM pages: im.snibgo.com
Post Reply