I was toying with the idea of an image match but I have decided that it would be more precise if I just matched the color of an image.
Here's the deal:
I've got a screenshot of a chart with a bunch of stuff on it. Intermittently a red or green arrow will appear. The color of the arrows is unique to the chart so it would be easier if I could just write a script that would determine if one of these colors existed in a screenshot at a given time.
How can this be done?
Find Color in an Image
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Find Color in an Image
I just created a unix bash script a couple of days ago to do something like that. It needs IM 6.5.0-10 or higher. see nearestcolor script at http://www.fmwconcepts.com/imagemagick/index.html
Alternately, you can get all the unique colors in an image and print a list and filter the list for your color. See http://www.imagemagick.org/Usage/quantize/#extract (-unique-colors and txt: format output)
Alternately, you can get all the unique colors in an image and print a list and filter the list for your color. See http://www.imagemagick.org/Usage/quantize/#extract (-unique-colors and txt: format output)
Re: Find Color in an Image
convert <image> -unique-colors -depth 8 txt:- > output.txt
With this I can do a grep on output.txt for the 2 possible rgb colors and then move from there.
With this I can do a grep on output.txt for the 2 possible rgb colors and then move from there.
Re: Find Color in an Image
The problem that I'm having now is that the Red arrow is actually Red (255,0,0) which is the same color as one of the lines on the chart. This makes it impossible to do a color check since the red line will always be there. However, if I could do a saturation test then that might reveal the presence of the arrow since it is much larger than the line, so if it were there the saturation of red in the screenshot would be higher.
Any idea how to do this?
Any idea how to do this?
Re: Find Color in an Image
How can I leave only a single color on an image? For example, I have an image with Aqua text (00ffff) on it. I'd like to be left with ONLY the aqua text and nothing else.
Re: Find Color in an Image
Using GIMP I am able to accomplish what I am looking to do. I used the Threshold tool, set it to 234; this made the image semi-black-and-white so that my aqua color is sticking out. I then converted to indexed mode, converted to tiff and used tesseract for OCR. Now I just need to find out how to do this with imagemagick.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Find Color in an Image
Trymastermindg wrote:The problem that I'm having now is that the Red arrow is actually Red (255,0,0) which is the same color as one of the lines on the chart. This makes it impossible to do a color check since the red line will always be there. However, if I could do a saturation test then that might reveal the presence of the arrow since it is much larger than the line, so if it were there the saturation of red in the screenshot would be higher.
Any idea how to do this?
convert yourimage -depth 8 -unique-colors -colorspace HSL txt:-
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Find Color in an Image
convert image -fill black +opaque "#00FFFF" resultmastermindg wrote:How can I leave only a single color on an image? For example, I have an image with Aqua text (00ffff) on it. I'd like to be left with ONLY the aqua text and nothing else.
This will convert to black every color that is not aqua.
If you want to replace black with transparent, then
convert image -fill black +opaque "#00FFFF" -transparent black result
or
convert image -channel rgba -alpha on -fill none +opaque #00FFFF" result
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Find Color in an Image
I would blur the image by a very small amount, say average all 9 neighbouring pixels in a location by using -blur 1x65535
The only pure red pixel will then be one that was surrounded by 8 other pure red pixels.
It will however also increase the number of colors on the image by an enormous amount, making the seach for a pure red color a little more difficult. As such you may be better attempting to junk all the other colors from the image first, say using "-fill white +opaque red" operation to make all non-red colors white, thus simplifying the search to just pure red pixels.
If you do this AFTER the blur, you effectivally limit the search to just a 3x3 spot of pure red.
As such my solution would be..
In the output has any red in it you have an arrow, ignoring red lines.
applying this to rose: shows it has no 3x3 red spots.
applying this to netscape; show that it does have at least one 3x3 pixel red area.
This is known as a 'morphological' operation in which I 'erode' any pure-red colors in the image, using a 3x3 square kernel. See http://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm or Google on those terms for more information. It is not a true mophology though as I am faking it by using a 3x3 pixel averaging blur, that Fred Weinhaus (see post by "fmw42" above) developed. I am suprized he did not come up with this as a way to identify arrows.
The only pure red pixel will then be one that was surrounded by 8 other pure red pixels.
It will however also increase the number of colors on the image by an enormous amount, making the seach for a pure red color a little more difficult. As such you may be better attempting to junk all the other colors from the image first, say using "-fill white +opaque red" operation to make all non-red colors white, thus simplifying the search to just pure red pixels.
If you do this AFTER the blur, you effectivally limit the search to just a 3x3 spot of pure red.
As such my solution would be..
Code: Select all
convert screenshot.png -blur 1x65535 -fill white +opaque red -unique-colors txt:
applying this to rose: shows it has no 3x3 red spots.
applying this to netscape; show that it does have at least one 3x3 pixel red area.
This is known as a 'morphological' operation in which I 'erode' any pure-red colors in the image, using a 3x3 square kernel. See http://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm or Google on those terms for more information. It is not a true mophology though as I am faking it by using a 3x3 pixel averaging blur, that Fred Weinhaus (see post by "fmw42" above) developed. I am suprized he did not come up with this as a way to identify arrows.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/