0

I am using open CV and C++. I have a completely dark image which has 3 colored points on it. I need their center coordinates. If I have only one colored point in the dark image, it will automatically display its center coordinate. However,if I take as input the dark image with the 3 colored points,my program will make an average if those 3 coordinates and return the center of the 3 colored points together,which is my exact problem. I need their individual center coordinates.

enter image description here

Can anyone suggest a method to do that please. Thanks

Here is the code http://pastebin.com/RM7chqBE

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Steph
  • 609
  • 3
  • 13
  • 32
  • Maybe if you share the code or at least the important parts related to the algorithm we'll be able to help. – Colin D Bennett Jan 21 '14 at 05:04
  • Yes I pasted the code in paste bin and edited the above. – Steph Jan 21 '14 at 05:07
  • I see at least three easy methods to do it (but there might be easier ones): 1. use `cv::findContours` method and compute the center of gravity of each single found contour (if every contour in the image is such a point) 2. use `cv::HoughCircles` method to find each single circle (if there can be other points in the image) 3. use clustering methods (k-means for example) and compute the center of gravity of each cluster (if the number of points/cluster is known) (4.) use some kind of blob detection - similar to circle detection but might work for smaller circles, too. – Micka Jan 21 '14 at 07:29
  • As suggested by @Micka I've tested the blob detection approach (method 4) with [QuickBlob](http://kmkeen.com/quickblob/) and [it works nicely](http://i.imgur.com/AxIcrQ8.png). See [this answer](http://stackoverflow.com/a/16143094/1688185) for more details. – deltheil Jan 21 '14 at 08:34
  • thanks a lot for all the good ideas!@deltheil : is your code in C++?I have been trying to use blob detection to detect those points but I was not getting the parameters right unfortunately. Could you please post the code? – Steph Jan 21 '14 at 09:25
  • @Steph QuickBlob is written in C. It comes with a command-line tool called `csv-blobs` that directly outputs the coordinates in CSV format, plus a `show-blobs.py` utility to visualize the blobs. Here is what I've done: https://gist.github.com/deltheil/8537114. As stated there, you can easily use the library into your own program. – deltheil Jan 21 '14 at 09:39
  • okay!!but unfortunately I am using C++. Since I was having problems using blob detection,I wanted to use findContours. What i wanted the program to do is that,scan the image from top,as soon as it encounters a contour it finds its center coordinate then it continues to scan until it finds the other contour and in the end it stores all the center coordinates in an array and finally displays them on screen – Steph Jan 21 '14 at 09:44
  • But what is the problem...? You can perfectly [use this C library in your C++ program](http://stackoverflow.com/a/12066342/1688185) plus QuickBlob lets you do whatever you want via the [`log_blob_hook`](https://github.com/keenerd/quickblob/blob/master/quickblob.h#L55-L58) user-defined callback. – deltheil Jan 21 '14 at 09:52
  • i never knew this could be done. Could u give more details on how to proceed with that. – Steph Jan 21 '14 at 09:55

2 Answers2

3

Found a solution!

  1. load original image to grayscale
  2. convert original image to gray
  3. set range of intensity value depending on color that needs to be detected
  4. vector of contours and hierarchy
  5. findContours
  6. vector of moments and point
  7. iterate through each contour to find coordinates
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Steph
  • 609
  • 3
  • 13
  • 32
0

One of the ways to do this easily is to use the findContours and drawContours function. In the documentation you have a bit of code that explains how to retrieve the connected components of an image. Which is what you are actually trying to do.

For example you could draw every connected component you will find (that means every dot) on it's own image and use the code you already have on every image. This may not be the most efficient way to do this however but it's really simple.

Here is how I would do it http://pastebin.com/y1Ae3e2V

I'm not sure this works however as I don't have time to test it but you can try it.

  • i tried it again but still not working. It runs and exits without any error and does not return the center coordinates. Here is the code http://pastebin.com/jb7YDvai – Steph Jan 21 '14 at 12:39