


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.įinally, our output images are displayed on Lines 34 and 35.

To create the output image, we apply our mask on Line 31. You would simply need to adjust your upper and lower limits to the respective color space. But you can easily do this in the HSV or L*a*b* color space as well. Note: We are performing color detection in the RGB color space. 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.Īfter 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. To perform the actual color detection using OpenCV, take a look at Line 29 where we use the cv2.inRange function. Furthermore, since these are pixel values that fall within the range we can use the unsigned 8-bit integer data type. 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. 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. Output = cv2.bitwise_and(image, image, mask = mask)Ĭv2.imshow("images", np.hstack()) # find the colors within the specified boundaries and apply # create NumPy arrays from the boundaries

Let’s take a look: # loop over the boundaries Now that we have our list of boundaries, we can use the cv2.inRange function to perform the actual color detection. 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. Let’s go ahead and define this list of colors: # define the list of boundariesĪll 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.įor example, let’s take a look at the tuple (, ). That means we’ll have to recognize red, blue, yellow, and gray colors in the image. We want to be able to detect each of the Game Boy cartridges in the image. Then, on Line 12, we load our image off disk. We’ll need just a single switch, -image, which is the path to where our image resides on disk. Lines 7-9 then handle parsing our command line arguments. We’ll use NumPy for numerical processing, argparse to parse our command line arguments, and cv2 for our OpenCV bindings. We’ll start by importing our necessary packages on Lines 2-4. # construct the argument parse and parse the argumentsĪp.add_argument("-i", "-image", help = "path to the image") Open up your favorite editor and create a file named detect_color.py : # import the necessary packages This example will run on Python 2.7/Python 3.4+ and OpenCV 2.4.X/OpenCV 3.0+.
Python colors code#
Looking for the source code to this post? Jump Right To The Downloads Section
Python colors how to#
