OpenCV and Python Color Detection

So, here I am. Riding the Amtrak 158 train, coming home after a long business trip.

It’s hot. The AC is barely working. A baby is screaming right next to me while the accompanying mother looks forlornly out the window, clearly questioning whether or not having a child was the right life decision.

And to top it all off, the Wi-Fi doesn’t work.

Luckily, I brought along my Game Boy and collection of Pokemon games.

As I slid my trusty Blue version into my Game Boy, I thought to myself, instead of battling Gary Oak for the thousandth time, maybe I can do a little computer vision.

Honestly, wouldn’t it be really cool to be able to segment each of the game cartridges using nothing but color?

Grab yourself a nice cool glass of water to combat the failed AC and a pair of ear plugs to block out the wailing child. Because in this post I’ll show you how to use OpenCV and Python to perform color detection.

Looking for the source code to this post?
Jump right to the downloads section.

OpenCV and Python versions:
This example will run on Python 2.7/Python 3.4+ and OpenCV 2.4.X/OpenCV 3.0+.

OpenCV and Python Color Detection

Let’s go ahead and get this started.

Open up your favorite editor and create a file named :

We’ll start by importing our necessary packages on Lines 2-4. We’ll use NumPy for numerical processing, argparse  to parse our command line arguments, and cv2  for our OpenCV bindings.

Lines 7-9 then handle parsing our command line arguments. We’ll need just a single switch, --image , which is the path to where our image resides on disk.

Then, on Line 12, we load our image off disk.

Now, here comes the interesting part.

We want to be able to detect each of the Game Boy cartridges in the image. That means we’ll have to recognize redblue, yellow, and gray colors in the image.

Let’s go ahead and define this list of colors:

All we are doing here is defining a list of boundaries in the RGB color space (or rather, BGR, since OpenCV represents images as NumPy arrays in reverse order), where each entry in the list is a tuple with two values: a list of lower limits and a list of upper limits.

For example, let’s take a look at the tuple  ([17, 15, 100], [50, 56, 200]) .

Here, we are saying that all pixels in our image that have a R >= 100, B >= 15, and G >= 17 along with R <= 200, B <= 56, and G <= 50 will be considered red.

Now that we have our list of boundaries, we can use the cv2.inRange function to perform the actual color detection.

Let’s take a look:

We start looping over our upper and lower boundaries on Line 23, then convert the upper and lower limits to NumPy arrays on Line 25 and 26. These two lines seem like they can be omitted, but when you are working with OpenCV Python bindings, OpenCV expects these limits to be NumPy arrays. Furthermore, since these are pixel values that fall within the range [0, 256] we can use the unsigned 8-bit integer data type.

To perform the actual color detection using OpenCV, take a look at Line 29 where we use the cv2.inRange function.

The cv2.inRange  function expects three arguments: the first is the image  were we are going to perform color detection, the second is the lower  limit of the color you want to detect, and the third argument is the upper  limit of the color you want to detect.

After calling cv2.inRange, a binary mask is returned, where white pixels (255) represent pixels that fall into the upper and lower limit range and black pixels (0) do not.

Note: We are performing color detection in the RGB color space. But you can easily do this in the HSV or L*a*b* color space as well. You would simply need to adjust your upper and lower limits to the respective color space.

To create the output image, we apply our mask on Line 31. This line simply makes a call to cv2.bitwise_and, showing only pixels in the image that have a corresponding white (255) value in the mask.

Finally, our output images are displayed on Lines 34 and 35.

Not bad. Only 35 lines of code, and the vast majority is imports, argument parsing, and comments.

Let’s go ahead and run our script:

If your environment is configured correctly (meaning you have OpenCV with Python bindings installed), you should see this as your output image:

Figure 1: Detecting the color red in an image using OpenCV and Python.

Figure 1: Detecting the color red in an image using OpenCV and Python.

As you can see, the Red Pokemon cartridge is easily detected!

Now let’s try the blue one:

Figure 2: Detecting the color blue in an image using OpenCV and Python.

Figure 2: Detecting the color blue in an image using OpenCV and Python.

Nope, no problem there!

And a similar story for Yellow version:

Figure 3: Detecting the color yellow in an image using OpenCV and Python.

Figure 3: Detecting the color yellow in an image using OpenCV and Python.

Lastly, the outline of the gray Game Boy cartridge is also found:

Figure 4: Detecting gray in an image using OpenCV and Python.

Figure 4: Detecting gray in an image using OpenCV and Python.


In this blog post I showed you how to perform color detection using OpenCV and Python.

To detect colors in images, the first thing you need to do is define the upper and lower limits for your pixel values.

Once you have defined your upper and lower limits, you then make a call to the cv2.inRange method which returns a mask, specifying which pixels fall into your specified upper and lower range.

Finally, now that you have the mask, you can apply it to your image using the cv2.bitwise_and function.

My train is just a few stops away from home, so I better wrap this post up. I hope you found it useful!

And if you have any questions, as always, feel free to leave a comment or shoot me a message.


If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , ,

218 Responses to OpenCV and Python Color Detection

  1. Jun November 21, 2014 at 4:02 am #

    thanks adrian! will try this out soon!

    • Adrian Rosebrock November 21, 2014 at 7:39 am #

      Cool, let me know how it goes!

      • sultan October 29, 2017 at 1:52 pm #

        this shows up when irun the code

        cv2.error: arithm.cpp:1947: error: (-209) The lower bounary is neither an array of the same size and same type as src, nor a scalar in function cv::inRange

        • Adrian Rosebrock October 30, 2017 at 2:49 pm #

          Hi Sultan — you might not have specified a proper path to your input image file. Try specifying the right path and let me know if it works.

          • Dewald December 20, 2017 at 8:34 am #

            Hi Adrian, the post is really great and elegant just one thing I keep running into the same problem as sultan and was wondering if there is alternate method to show the path of the picture in the folder


          • Adrian Rosebrock December 20, 2017 at 9:19 am #

            Hey Dewald — can you clarify what you mean by “alternate method”?

          • Divyanshi September 28, 2018 at 12:42 pm #

            Can you tell me where exactly do we have to specify the path to the image?

      • Rajashekar November 28, 2019 at 12:42 am #

        Hi Adrian how do we set that lower and upper limits I used colour palate to find lightest and darkest RGB values for each colour and written in code in BGR sequence but it detects Nothing, But all your four ranges are working perfectly fine.

        Please let me know how set them for other colours

  2. Begueradj February 4, 2015 at 6:05 am #

    How much is this efficient to segment pictures according to their colors with other famous existing segmentation algorithms ?

    • Adrian Rosebrock February 4, 2015 at 6:28 am #

      The cv2.inRange function is extremely efficient, but has the caveat that you need to know the pixel intensity boundaries prior to applying it to the image. Methods such as Otsu’s thresholding and Adaptive thresholding can be used to help determine the threshold value in traditional thresholding.

      • Txoof November 1, 2015 at 3:48 am #

        We are trying to find the ratio of red and green glowing armbands from a video stream captured by a RasPi. The images of an outdoor square where the lighting conditions will be changing through an evening. Can you suggest a good method for picking out the red and green bands?
        I’ve been playing with your sample code and I’m a bit stuck figuring out the ranges. I am clearly not defining them properly as the masked images come out barely showing the red and green.
        Ultimately the output needs to be the ratio of green:red to crowd-control a pong like game.
        Thanks for the great sample code and any advice you can offer.

        • Adrian Rosebrock November 3, 2015 at 10:20 am #

          I think this post on ball tracking will really help with your arm band project. Inside that post I make reference to a range-detector script inside the imutils package that will help you determine the appropriate color ranges for tracking.

          • Ankit April 5, 2016 at 12:57 pm #

            where is it??? I want range-detector for perticulat area not for perticular pixel.

          • Adrian Rosebrock April 6, 2016 at 9:09 am #

            You can find it on the imutils GitHub.

          • Ankit April 11, 2016 at 4:35 am #

            hey thanks for the instant reply Adrin
            you are great.

            Actually I want to know the lower limit and upper limit of any color from the live webcam.

            And one more thing
            I want to know minimum and maximum hsv value from selected portion(using mouser cursor with click) from the image.

          • Adrian Rosebrock April 13, 2016 at 7:06 pm #

            Hey Ankit — please see my previous reply. You need to use the range-detector script to determine your upper and lower limits. It’s hard to define thresholds that will always work, mainly because colors may have different HSV values depending on the lighting conditions of your environment.

  3. Begueradj February 11, 2015 at 3:50 am #

    Ok. Thank you.
    Last question, if I may: do you think that segmenting an image according to its colors is a good idea to use in order to recolor the image with given desired colors for each segment ? Could this give a good result ?
    Regards from Begueradj

    • Adrian Rosebrock February 11, 2015 at 6:27 am #

      Great question! The answer is yes, absolutely. This post on color transfer between images could be dramatically improved by performing the local color transfer within each individual segment rather than globally. The caveat is that you’ll have to “match” which segments are the most similar for the source and target image.

  4. João March 16, 2015 at 9:46 am #

    Hi Adrian,

    Great work. I’ve seen several implementations but yours is the most elegant I encountered so far. One thing though, if you wanted just to get a boolean flag like blue_is_present or orange_is_present would you find some way yo compare the current array with the previous ones or go with object tracking instead?

    • Adrian Rosebrock March 16, 2015 at 10:09 am #

      If you wanted to have a boolean flag indicating whether or not a color is present, I would loop over the color ranges, generate a mask for each one, and then count the number of non-zero pixels in the mask using cv2.countNonZero. If the number if non-zero pixels is above a given threshold, then you could say that a given color is present. I hope that helps!

      • João July 2, 2015 at 4:56 pm #

        I Adrian,

        Yes, it did. Thank you.

  5. Marc April 12, 2015 at 5:00 am #

    Hi Adrian,

    I am trying to learn color detection through this guide, but i can’t understand how did you determine the boundaries, can you point me out a good reference for this one? Thanks.

    • Adrian Rosebrock April 12, 2015 at 5:24 am #

      Basically, I end up opening the image in Photoshop (or a similar program) and examining the pixel intensity range. Any simple image editing software can help you out with that one!

      • Marc April 12, 2015 at 6:14 am #

        Thank you for the reply,

        Actually I’m using ColorPix.exe to determine the RGB value of the color, but I don’t understand how can I set the boundaries.

        For example,

        R = 255, G = 37, B = 37,

        this is a shade of RED i want to reference with, and i want to make a range (lower and upper), now I’m stuck i don’t know how to determine the lower or upper. I tried subtracting 20 to RGB for the lower, and add 20 to upper, but it doesn’t detect the color.

        • Adrian Rosebrock April 12, 2015 at 7:17 pm #

          If you are trying to define “shades” of a color, it’s actually a lot easier to use the HSV color space. I would define your colors in HSV using your color picker tool, and then convert those colors back to RGB.

  6. Francisco April 24, 2015 at 5:45 am #

    Hello Adrian . When trying to complete this exercise appears to me the following error: mask = cv2.inRange (image , lower, upper ) error : (209 ) the lower bounary is neither an array of the same size and same type the src , not in scalar function cv :: inRange … I’ve given round and round but still with the same error .
    Yours faithfully ,

    • Adrian Rosebrock April 24, 2015 at 6:36 am #

      Hi Franciso, I have actually heard about this error from one or two other PyImageSearch readers as well. The code executes without a hitch on my machine and the PyImageSearch virtual machine. The first thing I would do is make sure the path to your image is correct. You supply the image path via command line argument. If after ensuring that your path is correct, can you let me know which version of Python, OpenCV, and NumPy you are using? And it would probably help to know which OS you are using as well.

      • Tony_Smith May 23, 2017 at 9:07 am #

        On my Ubuntu virtual env I compile without any problem too, but when I port over to the RasPi3, I get this same error as above?
        Do you know what could be causing this? The image is in the working directory, so I doubt it’s a path issue?
        Also I’ve tried running in Python3, with no success but all else in the RasPi virtualEnv is compiling in Python 2.7.9, so I guess I’m forced to go with Python 2 for now.
        Will see if I can find out what the issue with Python 3 is???

        • Adrian Rosebrock May 25, 2017 at 4:27 am #

          Hi Tony — I still think this is a path issue. Try hardcoding the path to the image into cv2.imread to verify this.

  7. Francisco April 24, 2015 at 9:06 am #

    hello adrian . I have not yet tried the command line because when I click the f5 in python idle gives me error soon . already read on another website that the error may be due to this: np.uint8 instead dtype : ” uint8 ” . my version of python is 2.7.9 . opencv and numpy is recent . Greetings

    • Adrian Rosebrock April 24, 2015 at 10:26 am #

      I would definitely try to execute the script via command line instead of IDLE and see if you still get the error. Like I said, the problem could potentially be that the image is not being read of disk properly. Inspect the returned value of cv2.imread and if the value is None, then you’ll know that the image was not read properly. As for the versions of OpenCV and NumPy, it would be really beneficial if you could give the exact version numbers rather than just “recent”.

      • Francisco April 24, 2015 at 11:16 am #

        Hello , just the night I will try to run the program from the command line and give the versions of opencv and numpy . thank immense availability , you have had . another question : I’m tempted to buy the premium course bundle but at the moment I do not have to finance it. this course will be available in the near future or has to date not be available ? greetings

        • Adrian Rosebrock April 24, 2015 at 11:44 am #

          Hey Francisco, the PyImageSearch Gurus is set to open to the public in August, which is still months away. In the meantime, I definitely recommend that you pick up a copy of Practical Python and OpenCV so you can start to become familiar with OpenCV + Python. It’s definitely the best way to learn computer vision!

          • Nick September 23, 2015 at 11:47 am #

            Francisco, I was having this same error. I realized that I was using an array with 3 channels like:
            lower = np.array([1,3,3])

            For a image that was in black and white. Once I only created an array for one channel like this:
            lower = np.array([3])

            Everything worked fine!

          • Adrian Rosebrock September 24, 2015 at 7:01 am #

            Thanks for the clarification Nick!

            If you’re only using a black and white image, I would suggest using the cv2.threshold function instead.

  8. MNM April 26, 2015 at 11:53 am #

    Does it work for video stream as well? video stream off Pi camera?

    • Adrian Rosebrock May 1, 2015 at 7:07 pm #

      Absolutely. Take a look at this post on accessing the Raspberry Pi camera using OpenCV.

      • mobin August 3, 2015 at 7:07 am #

        Hi Adrian
        I want use pi camera,How can i use it color detection and different image processing?

        • Adrian Rosebrock August 3, 2015 at 10:06 am #

          Hey Mobin — if you want to use the Raspberry Pi camera, I would suggest reading this post on accessing the Raspberry Pi camera. It will only take a few small modifications to this code to take an input image from the Raspberry Pi.

  9. Iris May 6, 2015 at 8:23 am #

    Hey Adrian!

    First of all, great work on the blog and the tutorials, I really appreciate your work. This has already helped me a lot with what I am trying to achieve 🙂 Marc already wanted to know, whether there is a way of determining the boundaries from a given colour. In my application, I take a photo of lego bricks with a raspberry pi and want to determine the bricks’ positions inside a grid and what colour they are. I know which brick colours there are in the image, but I have to account for bad lighting (or at least differing lighting conditions). Do you think I could get satisfactory results using the shown approach by first automatically adjusting contrast/lighting and then calculating the boundaries in hsv with some fixed value?

    Thanks 🙂

    • Adrian Rosebrock May 6, 2015 at 8:46 am #

      Hey Iris, in general I think you are on the right track. But I would strongly encouraging you to work with your lighting conditions prior to writing a single line of code. The success of most computer vision applications starts before a single line of code is written — it all starts with the quality of your images. The higher quality they are, the easier they are to work with.

  10. PONS July 21, 2015 at 9:53 am #

    is it possible to use this through a video?

    • Adrian Rosebrock July 21, 2015 at 2:21 pm #

      It certainly is! You’ll need to make use of the cv2.VideoCapture function to obtain access to the webcam. I actually detail exactly how to perform color based object tracking inside Practical Python and OpenCV + Case Studies. Definitely take a look!

      • PONS July 22, 2015 at 10:01 am #

        thank you Adrian. this will be very helpful. can i ask, if you have another one example for shape detection, like arrows or any other shapes?? i just think, that i might easily understand youre example. than the others in the internet.

        • Adrian Rosebrock July 22, 2015 at 9:28 pm #

          If you’re interested in doing other types of shape detection, contours are always a good start. I use contours to recognize square targets in image — the same methodology can be used to identify other shapes.

          Otherwise, you might be interested in shape descriptors such as Hu Moments or Zernike Moments.

  11. Haseeb July 28, 2015 at 11:54 am #

    Hey adrian,
    Thanks for the amazing tutorials !!!
    I just have a small question. What would be the tuple value for green color? in order to only detect green color from an image.

    Thank you in advance.

    • Adrian Rosebrock July 29, 2015 at 6:34 am #

      Pure green in the RGB color space is (0, 255, 0). However, you’ll need to play with these values a bit to make them detect the particular shade of green you are interested. An alternative is to use the HSV color space where you may find it easier to define the color ranges.

  12. Tyrone August 17, 2015 at 8:06 am #

    Once again Adrian you grabbed my attention. Now its time to buy the book today. Please keep up the good work, and give us more.

    • Adrian Rosebrock August 18, 2015 at 6:47 am #

      Thanks Tyrone! 🙂

  13. Adi August 26, 2015 at 4:28 am #

    Hey there! Code works perfectly fine, but i seem to be having a rather slow fps rate(~2fps). Any solution to make it go faster? Thank you

    • Adrian Rosebrock August 26, 2015 at 6:22 am #

      What system are you running the code on? And what is the size of each frame you are processing?

  14. hyshan October 27, 2015 at 4:50 am #

    Hi Adrian, thanks for the great code! How do I detect two of the colors at the same time?

    • Adrian Rosebrock October 27, 2015 at 4:57 am #

      All you need to do now is maintain a mask from the output of Line 30. Keep updating the mask for each color, and then when you run out color boundaries, you’ll have your final solution!

  15. Hilman January 12, 2016 at 4:30 pm #

    Hey, Adrian. Good blog! The fact that I will be able to learn for free through your blog is just exceptional! You are my inspiration!

    I want to ask something. I want to track two objects in a video. The two objects can change colours between green and orange (those two colours only). Example, if Object1 is green, there will be print out ‘Object1 = On” and at the same time if Object2 is orange, there will be print out ‘Object2 = Off’.

    I am thinking of a way of doing this that is by slicing parts of the frame into two to focus on those two things differently and applying the cv2.range to both of the sliced frame. If the cv2.range detect green it will returns mask (means there is white pixels inside the sliced frame), so, from that the program will know if the object is green or orange. But how can I tell the program to check if the returns contain mask or not?

    Or do you have any other better workaround?

    Thanks in advance! Proud to say to anyone that I am learning OpenCV from you blog 🙂

    • Hilman January 12, 2016 at 5:05 pm #

      Just read your post about tracking object movement. I think I can use the function if len(cnts) > 0 to detect if there is any white pixels in the mask. Is it possible?

      Or you have anymore efficient way?

      • Adrian Rosebrock January 12, 2016 at 5:20 pm #

        The most efficient method to check if there are any white pixels in a mask is to use:

    • Adrian Rosebrock January 12, 2016 at 5:22 pm #

      It sounds like you’re on the right track. I would use this post as a starting point, and then modify it so you can track both colors.

  16. Kyle W. January 12, 2016 at 5:56 pm #

    Hello Adrian,

    Thanks for this tutorial. Similar to a few other users here, I am also getting the following error: “The lower bounary is neither an array of the same size and same type as src, nor a scalar”.

    I am using python 2.7.10 and what I believe is OpenCV 2.4.11-7 on Windows through Python(x,y) Idle. Is my set up ok for running this code? I like Python(x,y) because of all the modules it comes with in one quick install.

    I have been troubleshooting for a while but I can’t figure out what is wrong. I really just want to segment an image by color – do you know of any other simple methods to do this?

    Thank you – love the site.

    • Adrian Rosebrock January 13, 2016 at 6:41 am #

      Your Windows + OpenCV installation should be fine. As for your error message, can you double check that the image was loaded properly from disk using cv2.imread?

      If you do:

      And image is None, then you’ll know that your image was not loaded from disk properly and is causing the error.

      EDIT: I have been able to replicate the error on my machine. The error is indeed due to a valid image path not being supplied to the cv2.imread function.

      • Kyle W. January 13, 2016 at 12:29 pm #

        Hey Adrian, thank you for checking this out for me. Indeed you are correct and I am receiving NONE when I try to print the image with opencv commands. After troubleshooting the imread command for a bit, I tried loading the image with scipy instead then continuing the work with cv2 and this works! From a bit of internet browsing, it seems like others who had this problem fixed it by working around it like I did or updating to a newer version of openCV. Thanks for the help.

        • Adrian Rosebrock January 13, 2016 at 2:01 pm #

          Congrats on resolving the issue!

  17. Vinay January 16, 2016 at 8:52 pm #

    Found solution to this problem:-

    I don’t know why \\ this worked.
    If someone knows then please explain me.

    • Adrian Rosebrock January 17, 2016 at 5:25 pm #

      Both OSX and Linux systems use “/” as the path separator. However, Windows uses the “\” path separator. But in most programming languages you need to “escape” the “\” character, leading to “\\”.

  18. Rutvik January 21, 2016 at 2:06 am #

    Hi Adrian, i want to detect the colours using webcam what changes i have to do in above code.

    • Adrian Rosebrock January 21, 2016 at 4:59 pm #

      I would suggest starting by giving this post a read.

  19. Ananda February 23, 2016 at 10:48 pm #

    Works great!!! But if i want to separate the image into regions of different colors without knowing what colors will be in the image beforehand, how do I do it? For example, if I have an image that contains just three colors(I don’t know what they are), and want to draw contour around the images how do I do it? Thanks a lot!!!

    PS. I saw your other post on labeling different colors in an image but will that work if some of the colored region is inside other region?

    • Adrian Rosebrock February 24, 2016 at 4:36 pm #

      You’ll need to segment out each color individually and then label it. I would suggest starting with this post on color quantization with k-means to help segment out the colors.

  20. Douglas February 25, 2016 at 5:40 pm #

    Hello Adrian, how can i show the original and result images on two different windows ?

    • Adrian Rosebrock February 26, 2016 at 1:52 pm #

      You need two separate calls to cv2.imshow, one for each image that you want to display:

  21. Niruna Suntharalingam March 22, 2016 at 8:15 am #

    This works brilliantly! How would I modify the code so it takes multiple images instead of one, and processes them all with one execution of the script?

    Thank you!

    • Adrian Rosebrock March 22, 2016 at 4:19 pm #

      The only piece of code that needs to change is to apply a “loop” that loops over an input set of images. Take a look at how I apply the list_images function in this blog post and it will give you a good idea on how to process multiple images.

  22. Mus Sli April 8, 2016 at 5:18 am #

    Hello Adrian,
    Thanks for the amazing tutorials !!!
    I just have a small question :
    i want to detect a Picture of apps on my Smartphone Screen: exactly I have a Webcam and i have to detect apps on my Smartphone Screen und recognize where they are.
    And i want to define the Coordinate of this .
    how can i do it ? please help me 🙂
    (I use python 3 and opencv3 with Raspberry pi )

  23. Reza April 16, 2016 at 4:40 am #

    i am copy and paste your program in my desk sir , and then i get an eror , can you help me resolve this ?

    OpenCV Error: Sizes of input arguments do not match (The lower bounary is neither an array of the same size and same type as src, nor a scalar) in inRange …
    mask = cv2.inRange(image, lower, upper)
    cv2.error: /home/pi/opencv-3.0.0/modules/core/src/arithm.cpp:5696: error: (-209) The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange

    • Adrian Rosebrock April 17, 2016 at 3:33 pm #

      Please see my reply to “Francisco” above. The error is 99% likely due to the path passed to cv2.imread being invalid.

      • sumithra June 12, 2016 at 11:36 pm #

        even i got the same error as reza and path passed to cv2.imread is a valid one

        • Adrian Rosebrock June 15, 2016 at 12:53 pm #

          If you are absolutely, 100% sure that your path to the image is valid, then OpenCV likely cannot read the image type you are trying to pass in. Again, I’m pretty sure that your image paths are incorrect. But if they are, try placing a print(image) call right after cv2.imread. If you get None back AND your image path is valid, then OpenCV cannot read your image type.

  24. Guy May 30, 2016 at 9:07 am #

    Hi Adrian, thanks for this amazing tutorial!

    I’m trying to use opencv to filter text from an image (as a pre-processing stage before performing OCR)

    I wasn’t able to determine colors range for a text color that has some kind of gradient, using range-detector wasn’t really helpful in that case

    any other tools or tips you can give?

    my image is:

    • Adrian Rosebrock May 31, 2016 at 4:02 pm #

      Have you tried simple thresholding and adaptive thresholding? What about edge detection?

      • Guy June 7, 2016 at 5:20 am #

        Yes, both simple and adaptive wasn’t good enough to get tesseract to read the text, I was trying SWT but no luck there too.

        Still looking for the right way to make it easier for OCR to read it.

        Thanks !

        • Adrian Rosebrock June 7, 2016 at 2:44 pm #

          SWT is a great algorithm, although I’m unaware of a good Python implementation. Which version were you using?

          Also, I still think simple thresholding can be used one. The first threshold can be used to detect light text against dark backgrounds. And a second round of thresholding can be used to detect dark text against light backgrounds. Furthermore, since this is a scoreboard, you “know” where in the ROI the text will be, allowing you to focus on the regions of the image that are most likely to contain text.

  25. KENNETH PHANG May 31, 2016 at 2:19 pm #

    Hi Adrian,

    How can I know the result of the colour is detected without looking at the image window? Is there any programmable API from cv2 to return a boolean to tell me blue is detected ?

    • Adrian Rosebrock May 31, 2016 at 3:37 pm #

      You can use cv2.countNonZero on the returned mask. For example:

      Assuming that mask contains your detected blue pixels. I hope that helps!

  26. Scott June 1, 2016 at 10:06 am #

    Hi Adrian,
    Thank you for the great blog! I am learning Python and openCV at the same time. Because I am just learning, I am using an editor to help me with my Python syntax. Running you code within the IDE produces the same error than many have mentioned above:
    “OpenCV Error: Sizes of input arguments do not match………………”
    As you have correctly pointed out already it is a path issue to the image. I can run the code in terminal and it runs perfectly. The question I have is this, can I add a line of code that defines specifically the path so that I can run and then edit the code in my IDE? Thanks for your time!!


    • Adrian Rosebrock June 1, 2016 at 3:17 pm #

      Are you executing your Python script from within your IDE? Or via command line argument? If you’re using an IDE, I normally recommend using the IDE to write the code — but then use the terminal to execute the code so you have better control over the command line arguments.

  27. seema reka June 6, 2016 at 11:13 am #

    hello sir I m not Able to detect red color and blue color.i hv changed range also.still threshold image shows black only

    • Adrian Rosebrock June 7, 2016 at 3:23 pm #

      Defining the valid color ranges can be pretty tricky, especially if you’re just getting started. I would start by using the range-detector script in the imutils package. This will help you tune the ranges.

  28. hoang pham July 14, 2016 at 2:52 pm #

    Is there any way to set white color for background instead of black ?

    • Adrian Rosebrock July 15, 2016 at 1:31 pm #

      Can you elaborate on what you are trying to accomplish? Are you referring to the final output mask?

  29. Anonymous July 21, 2016 at 10:22 am #

    Are you busy hunting Pokemons nowdays Adrian?

    • Adrian Rosebrock July 21, 2016 at 12:37 pm #

      Believe it or not, I haven’t been sucked into Pokemon GO 😉

  30. Izrael August 3, 2016 at 4:23 am #

    Hello, Adrian nice Work i am working on plant Detection project so please Can you Tell me what is i want to detect Green Colour from my USB camera.


    • Adrian Rosebrock August 4, 2016 at 10:16 am #

      Please see my reply to “Txoof” above.

  31. Hamza Cavus August 5, 2016 at 7:03 pm #

    Thanks for the tutorial. I used it to detect stationary green objects in a game where player can hide.

    • Adrian Rosebrock August 7, 2016 at 8:17 am #

      That sounds like a perfect use-case for the cv2.inRange function. Have you tried defining the lower and upper boundaries for your green objects yet? I would suggest using the range-detector script mentioned in this blog post as a starting point.

  32. Marcos Costa Pinto August 8, 2016 at 1:29 pm #

    Excellent tutorial Adrian, Thank you!!
    But I have a question, what method you are using to define HSV color ranges, it is very hard to adjust the ranges to different images.

    • Adrian Rosebrock August 8, 2016 at 6:37 pm #

      Take a look at my reply to “Txoof” above.

  33. latha August 11, 2016 at 7:19 am #

    I would want to extract black color, range from ( 0 to 50) from the pic (honeybee colony).
    Could you help.

    • Adrian Rosebrock August 11, 2016 at 7:21 am #

      To define your own custom color range, just define the lower and upper boundary boundaries. You can do this by modifying the boundaries variable. It might take some trial and error to get exactly right.

      • latha August 12, 2016 at 1:01 am #

        Great! Worked well, able to detect black pixels. I thank you.Can you say how to change black background to white one.

        • Adrian Rosebrock August 12, 2016 at 10:47 am #

          Sure, all you need is masking. Create a new image with the same dimensions as the original one, only with a white rather than black background. Then apply bitwise masking to make the foreground show up on the background. I provide more information on masking and bitwise operations inside Practical Python and OpenCV.

  34. ozan August 30, 2016 at 3:43 pm #

    the question at the above something missing, so I will fix it from here,

    I mean you says open cv uses reverse order so it is BGR but in your example you show it

    R >= 100 B >= 15 and G >=17 so it means you used GBR

    by the way other pages sayas opencv uses BGR but in my mac GBR worked ? so whic order opencv uses

    • Adrian Rosebrock August 31, 2016 at 1:42 pm #

      Normally, we refer to pixel values in RGB order. However, OpenCV refers to them in BGR order (NOT GBR order). Therefore, the pixel values supplied in this blog post are provided in BGR order, which is what OpenCV expects.

  35. kenta November 23, 2016 at 7:07 am #

    hey adrian, ineed your help, i want to combine your work about the color detection and shape detection, adding the position of the object, and the result in show in the terminal, i already success but the color and shape detection didn’t combine in the result output, can you help me?

    • Adrian Rosebrock November 23, 2016 at 8:33 am #

      I would suggest following this tutorial where I demonstrate how to track the position of the object.

  36. binu paul November 24, 2016 at 10:39 pm #

    i need to know the boundary values for green color .

    • Adrian Rosebrock November 28, 2016 at 10:42 am #

      I would suggest using the range-detector script I mentioned in the blog post. The boundaries for each color can vary dramatically based on your lighting conditions.

  37. Himaesh December 29, 2016 at 12:23 pm #

    Hi Adrian.! can we able to detect some multiple colour combination i.e Military Camuflague colour with this thing .? what are the settings we are going to play with ,?

    • Adrian Rosebrock December 31, 2016 at 1:24 pm #

      You would want to define color threshold ranges for each color in the camouflage. Then, apply each of these thresholds to the image and construct a mask and combine the masks for each range. This will help you detect camouflage in your image.

  38. Pulkit January 1, 2017 at 11:36 pm #

    Link for the image?

    • Adrian Rosebrock January 4, 2017 at 11:03 am #

      The image + source code are included in the “Downloads” section of this blog post above.

  39. Mehdi January 19, 2017 at 5:01 am #

    Thanks but i have errors in lines 8 _ 9 .. in reading the image .. could u give me an example to correct arguments?

    • Adrian Rosebrock January 20, 2017 at 11:05 am #

      You supply the path to the image via command line arguments and your terminal:

      $ python --image pokemon_games.png

  40. Rizal WIbisono February 8, 2017 at 10:10 am #

    Hello Adrian, how can i setting resize the result images and save it to new image jpg format ?

    • Adrian Rosebrock February 10, 2017 at 2:12 pm #

      To write an image to disk you would see cv2.imwrite. If you’re just getting started with learning computer vision and OpenCV, I would suggest that you go through Practical Python and OpenCV. Your question is addressed in the first few chapters.

  41. LI February 18, 2017 at 7:32 am #

    sorry to say that i dont know how to get the correct path in line 8~9…..should i replace –image with my pictures’path?

    • Adrian Rosebrock February 20, 2017 at 7:56 am #

      No, you simply need to supply the --image switch as as command line argument when you execute your Python script. Please see this tutorial for more information.

  42. Aninda March 20, 2017 at 10:47 pm #

    Hey Adrian, i would like to use this to concept to build an app to help people with color vision deficiency (usually known as color blindness). I wanna detect whether in an image there is blue, red, green color, or any of its combination to help those who have partial color blindness. but i also need to detect any colors in an image for those who have total color blindness. Could you help me about this? what should i do to make it works?

    (fyi im a real newbie with a really basics knowledge in opencv and python, so if u could give a detailed explaination it would be a great help for me)
    Thanks in advance! 🙂

    • Adrian Rosebrock March 21, 2017 at 7:10 am #

      In order to perform this type of color balancing you would first need to calibrate your images using a color chart or a gray-level balancing card. From there you can obtain a consistent level of color representation for a given scene with your camera. This is definitely more of an advanced technique and not one I would recommend you taking on if you are just getting started with OpenCV and computer vision. If you’re just getting started in the world of computer vision and OpenCV, I would suggest you work through Practical Python and OpenCV before continuing.

      • Aninda March 21, 2017 at 10:02 am #

        Thanks for ur kind reply, but i haven’t set my goal far to do color balancing, i just meant to ask what should i do to detect all colors within an image without setting any boundaries of RGB color space? Just like the code u share, but without setting any boundaries, could u please give me the answer?
        I also wanna ask could we give a highlight or whatsoever to differentiate each region of colors in an image? and how could we give a text lable of the color names in the region of its colors? thanks before…

        • Adrian Rosebrock March 22, 2017 at 8:47 am #

          Are you looking to detect and label various colors in an image without setting any color boundaries and without color balancing? Simply put, that’s not possible, unless you train a classifier to label the colors of pixels based on real-world images.

  43. Monica V April 20, 2017 at 4:22 pm #

    Hi, your code looks interesting. I am working on a project in which I have to perform color change, not just color detection. As in, if I detect yellow, I have to display an image with white overlapped on the yellow blobs. Any suggestion on how I could do that?

    • Adrian Rosebrock April 21, 2017 at 10:53 am #

      You could use OpenCV’s cv2.drawContours function to draw the white overtop the detected yellow blobs. I discuss the basics of OpenCV and how to use cv2.drawContours inside my book, Practical Python and OpenCV — I would highly suggest starting there.

      • Monica April 23, 2017 at 9:21 pm #

        Thank you Adrian. This helped me

        • Adrian Rosebrock April 24, 2017 at 9:37 am #

          Fantastic, I’m happy to hear it Monica 🙂

  44. amir May 16, 2017 at 3:11 pm #

    I run my application in Python 2.7, but the following error
    OpenCV Error: Sizes of input arguments do not match (The lower bounary is neither an array of the same size and same type as src, nor a scalar) in inRange …
    mask = cv2.inRange(image, lower, upper)
    please help me

    • Adrian Rosebrock May 17, 2017 at 9:52 am #

      Hey Amir — it sounds like your input image was not properly read from disk. Double-check your command line arguments.

  45. sandy May 27, 2017 at 1:46 am #

    Hey Adrian,

    If I have to determine the color of a cloth. I have to manually specify all the color ranges and the corresponding color or there is some other way.

    • Adrian Rosebrock May 28, 2017 at 1:01 am #

      It depends. Normally you define a set of colors that you want to recognize and then quantize the colors in the image to their closest corresponding color in your database. I would look into k-means color clustering as well.

  46. Hadopan August 1, 2017 at 10:02 am #

    Hi Adrian, may i ask how to find the good range of the object color?, i’m new in opencv

    • Adrian Rosebrock August 4, 2017 at 7:11 am #

      Hi Hadopan — please see my reply to “Txoof” above where I mention the “range-detector” script in the “imutils” package. I’ll be doing a detailed blog post on this subject in the future.

  47. Saif August 6, 2017 at 8:43 pm #

    Hi Adrian,

    I’m doing a project to detect color. To determine the range values I’m running your range-detector script and I have the ‘orignal’, ‘thresh’ and ‘trackbars windows open and I have adjusted those to make my image black, which i assume means that provided the range values this black object will now be detected, however I’m unable to acquire the range values i.e. how are they printed? I cannot see them on the terminal or anything. Please help! 🙂

    Also is there any other way to fix the light condition problem when doing color detection?


    • Adrian Rosebrock August 10, 2017 at 9:03 am #

      You’ll want to modify the range-detector script to print the values. I would suggest after a specific key is pressed on the keyboard. I’ll be doing a tutorial on a more user friendly, easier to use range-detector script within the next couple of weeks.

  48. Madhu Oruganti August 19, 2017 at 9:51 am #

    Dear Adrian, I am working on tracking the color balls in snooker game.I can Track the objects but how can I append the numbers to the identified color ball as per the corresponding colors Like 1 for yellow , 2 for blue 3 for green 4 for red etc,.
    Please suggest me.
    Working on python opencv platform.

  49. George September 10, 2017 at 8:59 am #

    Hi Adrian,

    I think that your tutorial is great but Ι faced an error when runing the code.
    mask = cv2.inRange(image, lower, upper)

    cv2.error: C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:1947: error: (-209) The lower bounary is neither an array of the same size and same type as src, nor a scalar in function cv::inRange. What should I do?

    • George September 10, 2017 at 9:02 am #

      I forgot to mention that I have python 3.6.2 32bit version on a windows 10 64bit

      • George September 10, 2017 at 10:27 am #

        Finally I found answer. I forgot to add the .png after the image’s name

        • Adrian Rosebrock September 11, 2017 at 9:09 am #

          Congrats on resolving the issue, George!

          For anyone else who is having a similar problem:

          Be sure to double-check your input image paths!

          The cv2.imread function WILL NOT throw an error if you specify an input image path that does not exist. Instead, the function will return None.

    • Md.Harun-Ur-Rashid October 30, 2017 at 1:16 pm #

      i face the same problem.and i also give the image extension but the same error are show.

      • Adrian Rosebrock October 30, 2017 at 1:59 pm #

        Be sure to check the path to your image. Specify the directory and image filename.

  50. Jose September 17, 2017 at 7:31 pm #

    Hello Adrian,
    I have been changing the shades of red to make it the shade of red I want. I have been trying and it hasn’t returned anything. My red is a bit lighter than the one in your article, but when I use your BGR values for red, only the dark side is masked. Any ideas?

    • Adrian Rosebrock September 18, 2017 at 2:01 pm #

      Try using a different color space such as HSV or L*a*b*. These color spaces tend to be a bit more robust for detecting shades of color.

      • irfan September 28, 2017 at 6:38 am #

        Hello, sir

        i have a project for color detection, i need an explanation about the advantages of HSV from RGB. can you explain to me?

        • Adrian Rosebrock September 28, 2017 at 8:56 am #

          HSV tends to be more natural, similar to how humans perceive color. It’s easier to define color ranges in the HSV color space. The Wikipedia article actually does a very good job explaining the benefits.

  51. Jose September 18, 2017 at 2:07 pm #

    OK thank you very much.

  52. Mark October 3, 2017 at 10:15 pm #

    Hi Adrian,

    Thanks for the useful tutorial. I was wondering, is there an easy way to get box coordinates for the detected colors?


    • Adrian Rosebrock October 4, 2017 at 12:34 pm #

      Compute the bounding box via cv2.boundingRect. Take a look at this blog post for an example of using it.

      • Mark October 4, 2017 at 10:09 pm #

        Thanks, will check it out.

  53. Brendan November 1, 2017 at 4:13 am #

    Hi Adrian, I would like to ask if there is any way to combine two mask into one? Which means to say, i want blue and green color to appear on the same window. Appreciate your help!

    • Adrian Rosebrock November 2, 2017 at 2:30 pm #

      Sure. Just use the cv2.bitwise_or function on the two masks.

      • Brendan November 9, 2017 at 2:10 am #

        Hi Adrian, i am facing an issue whereby the mask detects colors that i do not need. Is there anyway where i can eliminate these unwanted colors?

        For eg, my BGR range is in the brown region(trying to detect rust) .. but it’s detecting yellow in the surroundings too. is there anyway to eliminate these colors without adjusting the boundaries as i’ve researched on the BGR range for rust and it’s a wide range. Would appreciate your help adrian! Thank you 🙂

        • Adrian Rosebrock November 9, 2017 at 6:14 am #

          I would suggest using a color space more suited for color range detection (and slightly easier to fine-tune) such as HSV or L*a*b*. I would start with the HSV color space.

  54. Dominic November 8, 2017 at 11:24 pm #

    Hi, I have a few questions regarding colour detection. Basically what I am trying to do is to detect different shades of rust colour (brown-orange kind of colour) and like what you did, mask the other colours. However, it is only about 80% accurate (not all the wanted shades of colour appear and a few of the unwanted shades of colour appear as well)

    I would like to know how did you come up with the upper and lower boundaries of the red, blue, green and yellow in the example above? Also, do you have any suggestions to what I am trying to do?

    Thank you very much for your help!

    • Adrian Rosebrock November 9, 2017 at 6:16 am #

      1. Try using a different color space such as HSV or L*a*b*. I would recommend HSV.

      2. The range-detector script inside the imutils library can be used to detect color boundaries. I’ll also be releasing a new version of the script soon, so be on the look out!

      • Dominic December 12, 2017 at 2:19 am #

        How do you use the range-detector script inside the imutils library? Do you have to make adjustments to the code or just run it straight? I tried running it straightaway, but it ask me to specify an argument.

  55. Falgun November 27, 2017 at 7:58 pm #

    Hi Adrian,

    Thank you for the wonderful blog. It really helps.

    Instead of displaying four outputs I just want one out whenever the color in the image is found just stop. For eg. I have 5 different upper and lower boundaries but there is only one (say blue) color in the image, so I need only one output with blue image,what changes do I need to make ?

    Thanks in advance.

    • Adrian Rosebrock November 28, 2017 at 2:08 pm #

      Hey Falun — assuming I understand your question, it sounds like you need to loop over each of your color ranges, one-by-one and then compute the mask. Call cv2.countNonZero on the mask to see if there are any masked pixels. And if so, break from your loop. I hope that helps!

      • Falgun November 28, 2017 at 7:18 pm #

        Thank you. It really helped me 🙂

        One last question from me.

        I am trying to display multiple colors in the result. I am trying to append the mask and result value to a numpy array if the countNonZer0 > 0 but I am not able to display the mask and result as the values are not getting append correctly. Can you help me with how should I output multiple colors ?

      • Falgun November 28, 2017 at 8:06 pm #

        Final one.

        How can I find the most dominant color in the image ?
        If its a sky it should be blue.

        Thank you once again in advance

        • Adrian Rosebrock November 30, 2017 at 3:41 pm #

          I actually address that exact question in this blog post 🙂

  56. quique January 11, 2018 at 7:29 pm #

    What mathematical operation do you use and how many types of methods are there for color detection, you know?

  57. Yimeng Zhang March 4, 2018 at 6:59 am #

    Hi. Adrain.

    Thanks for your tutorials which are really helpful. I am quite interested in how the range-detector works. Have you post any code explaintion in detail for the range-detector ?

    Waiting for your reply. Thank you so much.

    Best regards,

    • Adrian Rosebrock March 7, 2018 at 9:36 am #

      I have not created a dedicated post for range_detector — I’ve been busy with other tutorials. I’ll be writing range_detector to make it easier to use as well.

  58. Shiv March 5, 2018 at 2:57 pm #

    Hi Adrian,
    1.When I run the script on command prompt a window is popped saying that python has stopped working. Any idea?
    2. Is there any other way I can read image without having to use command prompt. I am using jupyter notebook.

    • Adrian Rosebrock March 7, 2018 at 9:19 am #

      1. I’m not sure about that. What is the exact error message you are getting?

      2. Sure, you can load the code into Jupyter, remove the command line arguments, and hardcode the image paths.

  59. varadha March 6, 2018 at 9:21 pm #

    File “”, line 4, in
    mask = cv2.inRange(image, lower, upper)
    error: (-209) The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange

    please help me to clear this error

    • Adrian Rosebrock March 7, 2018 at 9:10 am #

      Double-check the command line argument paths to your input file. It seems like the path to the input image is invalid and cv2.imread is returning “None”.

  60. varadha March 8, 2018 at 7:51 am #

    im getting a black image sir,the code has run but only black image is obtained

    • Adrian Rosebrock March 9, 2018 at 9:17 am #

      It sounds like you may need to tune the color threshold parameters. Try tuning them to whatever color object you are using for detection.

  61. safwan March 15, 2018 at 2:37 am #

    hi, is it possible to create real time video with color detection like this sample program?

    • Adrian Rosebrock March 19, 2018 at 5:58 pm #

      Yes. Take a look at this post.

  62. abs March 26, 2018 at 11:07 pm #

    hey adrian, do you have a post where you detect an object based on a certain color and then drawing a bounding box over it. maybe some additional lines to this code itself?

  63. Norb May 10, 2018 at 2:30 am #

    Hi Adrian, how can I display on the same mask more colors if it is in a for loop?

    • Adrian Rosebrock May 14, 2018 at 12:22 pm #

      Do you want one mask per color? Or a single mask that combines the masks from all colors?

      • Mu May 22, 2018 at 6:31 am #

        the command cv2.countNonZero(mask)>0 seems not giving any result, i asked to print blabla to see results at the terminal , it print it all time whenever there’s or not a red ( i change the cam view )

        • Adrian Rosebrock May 22, 2018 at 6:38 am #

          You should double-check your mask output. Even there is even a single pixel your conditional will return “True”. You may want to increase the pixel count from 0 to 10 or 100 to handle the event there is some noise in your image.

  64. Kenneth May 23, 2018 at 5:41 pm #

    Hi Adrian,

    Thank you for all of your posts. I am trying to identify different faces (Bottom / Side) of a “triangular prism” die (NO DOTS on the surface) out of a top-view color image. What will you recommend if I want to count different sides of the die in the image? Should I go for color recognition or shape recognition or both?

    • Adrian Rosebrock May 24, 2018 at 6:38 am #

      Do you have any example images of what you’re working with? I think seeing a visual representation of the images you are working with and what you are trying to accomplish would be helpful in understanding this problem.

  65. Andy June 2, 2018 at 12:27 am #

    Hello Adrian,
    Do you know know what’s the benefit of using BGR instead of HSV? I read on the OpenCV tutorial (, they used HSV instead of BGR. I tried it and couldn’t have my propgram running. All I got was just 2 black windows popped up

    • Adrian Rosebrock June 5, 2018 at 8:12 am #

      Exactly which color space you use depends on your project. HSV tends to be a bit easier, more intuitive (once you understand the basics), and provides better results once you have the threshold defined properly. IF you are getting two black windows then no pixels are falling into your defined range. You will need to further tune the color thresholds.

  66. Abanoub. G July 10, 2018 at 6:18 am #

    Hey Adrian,
    Thanks a lot for your tutorial. I am trying to do something similar except that I am trying to detect all of the contours in the picture that are not of green colour and draw squares around them. I have looked at your different tutorials like motion detection, shapes detection..etc. However I can’t manage to find the right openCV command that includes all of the colours but the range I don’t want.

    In other words in your code you ususally use mask =cv2.inrange(hsv, greenlower, greenupper)

    I am trying to find the “out of range” equivalent. What command do you reckon I use?

    Many thanks

    • Abanoub. G July 10, 2018 at 6:24 am #

      If it helps I am trying to use this to get detect target in a green environment like in a forest or so.

      • Adrian Rosebrock July 10, 2018 at 8:08 am #

        Is there a particular reason you cannot do a bitwise NOT to flip the mask? You detect your green colors (what you don’t want) and then flip the mask to get everything you do want:

        mask = cv2.bitwise_not(mask)

  67. Monochrome July 10, 2018 at 8:41 pm #

    Hello Adrian,

    I’m getting error: (-209) The lower boundary is neither an array of the same size and same type as src, nor a scalar in function cv::inRange


    • Adrian Rosebrock July 13, 2018 at 5:29 am #

      Double-check your path to the “cv2.imread” function. Your path is invalid causing “cv2.imread” to return “None”. You can learn more about NoneType errors, including how to solve them, in this post.

  68. pear August 30, 2018 at 11:00 pm #

    Hello Adrian,

    How I solve the problem “the following arguments are required: -i/–image”?

    Thank you

  69. Bishwaraj Dey September 16, 2018 at 9:21 am #

    HI Adrian,

    Great post. I have a simple question. I have a list of colours that I need to detect in an image. Could you tell me how to get boundary/range of any colour I want (say purple)?

    • Adrian Rosebrock September 17, 2018 at 2:20 pm #

      You would need to manually specify those color threshold parameters. The range-detector script in the “imutils” library may help you out.

  70. Brian Lee October 2, 2018 at 8:52 pm #

    Hi, how did you get the RGB boundaries for your colors?

    • Adrian Rosebrock October 8, 2018 at 10:30 am #

      I used the range-detector script from the imutils library.

      • Brian Lee October 9, 2018 at 7:20 pm #

        Hi Adrian, how exactly did you use your Range Detector script to determine your color ranges? I’m having trouble working with it.

        • Adrian Rosebrock October 12, 2018 at 9:23 am #

          You load up an image (or video) in the script and then adjust the sliders. You’ll want to use the sliders to determine your color range. From there you record the RGB or HSV values for the range and use them in your own script.

  71. Anwar October 17, 2018 at 11:49 am #

    I just want to ask you if there is any code for color detection using opencv and pi camera ?

    • Adrian Rosebrock October 20, 2018 at 7:53 am #

      No, but you can use this tutorial to access your Raspberry Pi camera module and from there use the code here to detect colors in a video stream. I hope that helps!

  72. mathunni October 28, 2018 at 2:35 am #

    what changes are to be made to load video instead of image

  73. manojna November 26, 2018 at 1:10 pm #

    I need the source code and the dataset used can u please send me

    • Adrian Rosebrock November 26, 2018 at 2:19 pm #

      You can use the “Downloads” section of this tutorial to download the source code and example dataset.

  74. Rohini Jadhav December 11, 2018 at 5:46 am #

    How you are deciding the values of red,green,blue colors?

    • Adrian Rosebrock December 11, 2018 at 12:34 pm #

      I am using the range-detector script in the imutils library.

  75. Sal December 14, 2018 at 7:33 am #

    Hi Adrian!

    Great stuff!

    I have a question, is there a way to inverse the output? like, display everything EXCEPT what is in the upper lower boundries?

    So suppose instead of displaying only red Pokemon, it displays everything except the red Pokemon.

    • Sal December 14, 2018 at 7:57 am #

      I read the comment above, I have tried to write this line:

      output = cv2.bitwise_and(image, image, mask=mask)

      as this line:

      output = cv2.bitwise_not(image, image, mask=mask)

      What I am getting is somewhat what I want but not exactly.
      I want two things:
      1. To display everything, except
      2. Except the color blue

      So I am getting point number one, and the color blue is not being displayed which is what I want, but it is being filled with a yellowish color, instead I want it to be colored in black.

      • Sal December 14, 2018 at 8:06 am #

        Sorry, got it to work I think:

        output = cv2.bitwise_and(image, image, mask=cv2.bitwise_not(mask))

        but if you have any other comments I’d love to hear them! 🙂

        ps, bought your book, will look into it

        • Adrian Rosebrock December 18, 2018 at 9:24 am #

          Thank you for picking up a copy, Sal! And congratulations on resolving the issue, nice job! 🙂

  76. noobnoob March 14, 2019 at 5:48 pm #

    Hi Adrian. massive work here thanks. I’m a beginner in Python and I’d like to find a way to detect colors in certain areas of an image. I can’t seem to find an easy way to deal it with. what’d you suggest. thank you.

  77. divya March 26, 2019 at 3:40 am #

    hey Adrian what are uses of this color detection?Can we display the name of the detected color also? if so can you please help me at least giving a reference link .thank you.

  78. Ramisha April 20, 2019 at 10:01 am #

    What is math behind inRange?

    • Adrian Rosebrock April 25, 2019 at 9:17 am #

      It’s simply a threshold test. If you’re new to thresholding I would recommend you read through Practical Python and OpenCV — that book will teach you the basics.

  79. Cerine May 5, 2019 at 4:11 pm #

    Hello adrian,
    Does this code work for reading from a video ?
    Also i need to get the rgb values from each frame taken by the video because i need to convert them to cie lab values.. can you help please ?

    • Adrian Rosebrock May 8, 2019 at 1:13 pm #

      1. Yes, it works when reading from a video file.

      2. You can convert color spaces via the “cv2.cvtColor” function. If you need help learning the basics of OpenCV I would suggest reading Practical Python and OpenCV.

  80. armin September 15, 2019 at 4:15 pm #

    Hey Adrian,

    Thanks for the tutorial. I wonder how can I extract all the colors of my image. For example, I have an image that contains 10 different colors, How am I going to extract all the colors at the same time instead of extracting color one by one?

  81. Jae September 30, 2019 at 10:13 pm #

    Thanks for the awesome article, Adrian! A clarification: since the upper and lower RED values are in BGR in numpy, for the tuple ([17, 15, 100], [50, 56, 200]) are the B and G values switched in the statement below, or am I missing something?

    “Here, we are saying that all pixels in our image that have a R >= 100, B >= 15, and G >= 17 along with R <= 200, B <= 56, and G <= 50 will be considered red"

  82. Pauline November 8, 2019 at 5:14 am #

    Hi Adrian,
    Thanks for your work.
    Do you know if it is possible to use HSV instead of RGB for color detection please ?

    • Adrian Rosebrock November 14, 2019 at 9:36 am #

      Yes, you would need to define your color range in HSV and then convert the frame to the HSV color space prior to using the “cv2.inRange” function. That process is covered inside the PyImageSearch Gurus course.

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply