Finding a specific RGB Value in a directory tree of images?

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
gaylorr

Finding a specific RGB Value in a directory tree of images?

Post by gaylorr »

I work in Visual Simulation. I have a directory tree of files
(a few thousand to several hundred thousand)
that are all the same size (512 pixels x 512 pixels).

All of the images are chopped up sat/aerial images and the program that generates them has a bug that at random generates a green line down one edge of the image. The color is always the same RGB value..Which edge of the image is also random.

The help I need is to have a script of some kind that will search thru the directory tree and flag, list, or write the filenames out to a text file of the files that contain this value.

Anyone? I have tried searching thru this, and have found nothing this specific. I can't find ANY tool that will do this, but I believe that IM can. Can it?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Finding a specific RGB Value in a directory tree of images?

Post by fmw42 »

Can you give us a little more information. Is it always a vertical green line? Is it always along the edge and never inside the image? What color green (what are the rgb values)? Is it a perfectly constant line of that green, never any other shade mixed in -- in other words does every pixel in the line have exactly the same color? Can that color of green be anywhere else in the image?

I don't know much about searching a tree structure, but in IM, you can test the color values for the 4 corner pixels easily.

For example, the following will return 1 if pure green is at any corner and 0 if no corner contains pure green

color="rgb(0,255,0)"
test=`convert <image> -format "%[fx: u.p{0,0}==$color||u.p{0,h-1}==$color||u.p{w-1,h-1}==$color||u.p{w-1,0}==$color?1:0]" info:`
echo $test

If it is possible that by chance a corner will contain green, but not a full row or full column (and the row or column is on the side of the image), then what you need to do is two tests. First average all the columns to one row and do the same test above but for just the first and last pixel in the rows. Then repeat by averaging all rows to one column and test the first and last pixel in the column

color="rgb(0,255,0)"
convert <image>-scale 512x1! temp1.png
test1=`convert temp1.png -format "%[fx: u.p{0,0}==$color||u.p{w-1,0}==$color?1:0]" info:`
convert <image>-scale 1x512! temp2.png
test2=`convert temp1.png -format "%[fx: u.p{0,0}==r$color||u.p{0,h-1}==$color?1:0]" info:`
test=`convert xc: -format "%[fx:($test1+$test2)==0?0:1]" info:`
echo $test

note that in fx, w=width, h=columns, so that w-1 is last x (column) and h-1 is last y (row). thus you automatically get the corners no matter how big the image is.
gaylorr

Re: Finding a specific RGB Value in a directory tree of images?

Post by gaylorr »

First off, Thanks for the reply...

>>Is it always a vertical green line?
-- No...can go either H or V, but always the direction of the edge it is on.

>>Is it always along the edge and never inside the image?
-- That is correct. Always edge, and always edge-to-edge...just never know of vertical or horiz. and sometimes the entire image...

>>What color green (what are the rgb values)?
-- R:26 G:113 B:6 (also have apha...all the images in this case are .RGBA...but I am not concerned with the alpha channel...

>>Is it a perfectly constant line of that green, never any other shade mixed in -- in other words does every pixel in the line have exactly the same color?
-- Yes...it is put in programmatically by the software to fill a gap/hole in the imagery. (That's the bug...I would prefer nothing)

>>Can that color of green be anywhere else in the image?
-- Not sure what you mean...but it is always on an edge, and never like a single pixel in the center...It would seem that checking for the four (4) corners for this color would do the trick.

actual example:
http://www.simul8d.com/img/test-image-d ... r00764.rgb
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Finding a specific RGB Value in a directory tree of images?

Post by fmw42 »

>>Can that color of green be anywhere else in the image?
-- Not sure what you mean...but it is always on an edge, and never like a single pixel in the center...It would seem that checking for the four (4) corners for this color would do the trick.
What I am asking is can the valid image data have such a color pixel anywhere or are your images such that that color is somehow excluded? If not, testing just the corners could give a false positive due to a real pixel value being this color. Thus the averaging to a row and/or column and testing the end pixels would be required. See my second solution in the previous email

Can the green stripe, if vertical, be on either the left or the right side?

Can the green stripe be horizontal, either on the top or the bottom?

Can the green stripe be interior to the image, i.e. not along the edge? If so, then you would have to do the averaging technique and test every pixel in the row or column for that color.
gaylorr

Re: Finding a specific RGB Value in a directory tree of images?

Post by gaylorr »

>> Can the green stripe, if vertical, be on either the left or the right side?
-- Yes

>> Can the green stripe be horizontal, either on the top or the bottom?
-- Yes

>> Can the green stripe be interior to the image, i.e. not along the edge? If so, then you would have to do the averaging technique and test every pixel in the row or column for that color
-- No. It can be on the edge, and into the center, but it ALWAYS starts at an edge. (like the example image I put a link for)

I think the checking the 4 corners would work, because no matter where the green line is, that image needs to be flagged for removal.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Finding a specific RGB Value in a directory tree of images?

Post by el_supremo »

What's the magick incantation to convert the rgb file to,e.g. PNG?
I tried:
convert -size 512x512 rgba:sat_raw.rgb sat_raw.png

but the resulting image is clearly not right.

Pete
Post Reply