简体   繁体   中英

detect yellow color in image using PIL

I have taken a JPG image using a camera and wish to detect a yellow matchbox in the picture. I was trying to use the Python Imaging Library. I tried to find the RGB combination for yellow, and it turns out that R and G are always 255 for any shade of yellow. In the image however, there are no values of 255. What am I doing wrong? How can I solve this problem? This is my attempted code:

for x in range(1,2592):
    for y in range(1,1456):
        p = im.getpixel((x,y))
        if p[0]>150 and p[1]>150:
            print p

Unfortunately, no coordinates turn up.

Two issues:

  • Depending on the image format, the color may be in a 0.0 to 1.0 space.

  • (150, 150, 255) , while matching your test would not be considered yellow. In fact it's blue. You should test that red and green are large enough, but also that blue is small enough.


Putting those together:

for x in range(1,2592):
    for y in range(1,1456):
        p=im.getpixel((x,y))
        if p[0] > 150 and p[1] > 150 and p[2] < 150: # added p[2] < 150 for blue
            print p, "is yellow"
        else:
            print p, "is not yellow"                 # see what range you have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM