How to detect the color of a person's shirt:

Last updated on 13th September, 2010 by Shervin Emami. Posted originally on 31st May, 2010.

The task of "Face Recognition", where you want a computer to figure out who a person is from a photo of them, is a very difficult function to do well. But in some cases such as robotics, it may not be so important to figure out exactly who it is, you might just want to get an idea of where the people are, or to notice when the camera sees the same person again after the camera has moved around in the room. These are cases when Shirt Detection can be used as a simple method to keep track of who is in the room. For example, a robot could just keep track that there is a person wearing a red shirt to its left, a person wearing a blue shirt in front, and a person wearing a yellow shirt on its right side, so that it can track where these people are when they move around or when the robot moves around.

Shirt Detection can be performed quite easily compared to Face Recognition, by using OpenCV's very reliable Face Detection. Once the program knows where a person's face is, it can look in a small region below the face (where a person's shirt would be), and determine the approximate color of their shirt in that shirt region. This can be performed even without a complex method of determining the exact contour region of a person's shirt, since all you need to know is the color of a region below their face, as opposed to the color of their entire shirt or body.

Here you can find a program I have created (named "ShirtDetection") that detects the shirt color of multiple people in a photo. As a demo, here is the input photo:



The program will convert each pixel into an approximate color type (eg: Green or Orange or Purple or Black, etc), based on its HSV color components. In the image below, you can see that some pixels are converted to red, some to orange, some to green, etc.



It will then perform Face Detection to find where the faces are within the original image. For each face that it finds, it will look at the approximate color types in a small rectangle region below the detected face. It will then see if there is a certain color type that is most dominant in that small region, such as if 60% of the pixels in the shirt rectangle are detected as purple pixels, then it will classify the person's shirt as Purple, with a 60% confidence rating. You can see the final image below, where the detected shirt colors are overlayed on top of the person's shirt.


In most photos, it is reliable enough for simple uses such as Human-Robot-Interaction, where a false detection is not such a problem. Note that even though it appears as if image segmentation was performed, there was no segmentation or contour detection being done. It is simply the result of converting each pixel into an approximate color type.

Note that if the person is so close to the camera that the shirt region goes below the bottom of the image, you should either ignore that face or try a smaller region (with less confidence) in the hope of atleast seeing the top of the shirt. Remember that this program is trying to do shirt color detection, on the assumption that most of their shirt is in the photo, so don't expect good results if only a tiny part of their shirt is visible, and since the shirt color is converted to just a few possible colors, don't expect good results with more than 3 or 4 people!

Update on 30th Aug 2010:

ShirtDetection v1.1 does try a smaller region (with less confidence) if most of the shirt is cut from the image. For example, if only the collar of the shirt is visible in the image, then instead of looking for the region in the chest area, it will look in the lower-neck area.

 

ShirtDetection:

The source-code to my program is available here (opensource freeware), to use as you wish for personal or commercial use but NOT for criminal-detection or military projects.

The shirt detection process only takes a few milliseconds to execute, but also does face detection, which normally takes about 100 milliseconds (depending on the image size). Also, it generates a "Color Image" window so you can see the detected colors of all pixels, which is useful for debugging but is very slow to generate. You can disable that feature by commenting out "SHOW_DEBUG_IMAGE" in the source-code, to run faster.

Click here to download ShirtDetection v1.1: shirtDetection.zip

(0.2MB Zip file, including C/C++ source code, VS2008 project files, the compiled Win32 program and Haar frontal face detector).

If you dont have the OpenCV 2.0 SDK then you can just get the Win32 DLLs for running this program here: openCV200_Win32DLLs.7z (1.3MB 7-Zip file).

And if you dont have the freeware 7-Zip program (better than WinZip and WinRar in my opinion), you can download it HERE.