'm trying to detect all the post-it in an image and get them into an arrayList. I tried many alternatives (removing backgroud -> detecting contours, haar Cascade classifier, detecting rectangular objects...) but none of them gave me good results. this is an example of input image.

Any Idea how to proceed? Any help will be appreciated.

  • $\begingroup$ Have you tried shifting it to the HSV spectrum and then looking for specific colors? $\endgroup$ – HighVoltage Feb 24 '17 at 22:41
  • $\begingroup$ thanks @aul12 for your response, it was really useful. (sorry i can't comment or upvote your response because i'm new). I tried your code but i can only detect some colors. Here is what i've got as result: ![result](i.stack.imgur.com/36y4F.jpg) $\endgroup$ – Med Aziz Feb 27 '17 at 14:42
  • $\begingroup$ Have you tried lowering the threshold a little bit more (that's the 127 in the "inRange(...)" Line)? My code only accepts colors with a minimum saturation of 127 (50%). After playing a little bit with gimp i would say you can choose a value as low as 60 or 70 (if there are no false positives you can reduce the value even lower). $\endgroup$ – aul12 Feb 27 '17 at 16:04
  • $\begingroup$ Welcome to Robotics medAziz, but I'm afraid that it is not clear what you are asking. We prefer practical, answerable questions based on actual problems that you face, so it's a good idea to include details of what you want to achieve, what you tried, what you saw & what you expected to see. Please take a look at How to Ask & tour for more information on how stack exchange works and work through the Robotics question checklist to edit your question to make it clearer. $\endgroup$ – Mark Booth Mar 2 '17 at 10:54
  • $\begingroup$ Also, note that even though you don't yet have up vote privileges, you can always mark an answer as accepted. $\endgroup$ – Mark Booth Mar 2 '17 at 10:59

As HighVoltage already suggested this should be an easy task using the HSV spectrum.

Your code could look something like this:

// img is a Mat containing your image

// Convert your image into HSV
Mat imgHSV = img.clone();
cvtColor(img, imgHSV, CV_BGR2HSV);

// Blur to reduce noise
blur(imgHSV, imgHSV, Size(3,3), Point(-1,-1));

// Threshold image to only accept a certain saturation
inRange(imgHSV, Scalar(0, 127, 0), Scalar(255, 255, 255)), imgHSV);

// If you have to much noise you can erode and dilate at this point

// Find contours in your image
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(imgHSV, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

The contours contain a list of a list of points, each inner list represents the contour of one object.

You can for example use contourArea(contour[n]) to get the area of the nth contour.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.