0

Im building a DIY laser target.

So far on the software side I managed to find when is a laser shot at the target by calculating differences and threshold, and then find where did it hit in relation to the whole image.

Now the next problem I am facing is finding where it hit in relation to the target center.

I have a proper box assembled for the target, but while testing this is what I have (the inner circle should have been filled with black):

target

Its not pretty and has lots of unrelated noise but it should be enough to test. This is a best possible scenario, from within the box:

enter image description here

To find where it hit in relation to the target center I suppose I'll have to make a binary image out of those two black rings and calculate the average point. (how?)

Then (supposing the target is perfectly parallel, not even a little bit in perspective), with the center known, I would need to know how far the outer ring is from it.

With that known it should be a matter of trigonometry to tell the punctuation (11 to 0, being 0 the outside of the outer ring)

Resuming the question:

  1. Need to detect a known figure(ring), tell where its center is, and determine its radius.

I am not worried about performance since this will only be done once at each program run to calibrate.

Some sample C++ code (besides pointing to the functions I can use) would be great since I'm new to both C++ and OpenCV.

Community
  • 1
  • 1
SOMN
  • 351
  • 1
  • 3
  • 15
  • 1
    There are plenty of [examples](http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html) on opencv.org with c++ code about finding circles in images ... Have you tried any of them? – Fantastic Mr Fox Aug 18 '15 at 23:28
  • 1
    `houghCircles` will detect easily your circles. You can easily find the red blob thresholding the R channel of your BGR image. If needed (probably not), using `moments` you can get the mass center of the blobs. – Miki Aug 18 '15 at 23:30
  • @Ben , I've been learning from the official samples, but there are almost 130 c++ files there. I asked because I didn't knew where to look after in those files. The OpenCV tutorials had problems compiling (for some of the tutorials) so I gave up on them. Im inexperienced with C++ and debugging isn't my strongest skill on it :) Miki- Whats the margin of error of houghCircles? Can I assume that in the worst case scenario its something like 3-4 pixels? I know that I asked for a strictly paralel target, but does that also works with circles in perspective? I already did that threshold of the R ch. – SOMN Aug 18 '15 at 23:39
  • 1
    @CláudioPereira What environment are you working in? Visual Studio, Linux? If you can compile any examples than you should be able to run the example code listed in the link i gave. There is a pretty straight forward set up page [here](http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html) – Fantastic Mr Fox Aug 18 '15 at 23:43
  • 1
    The accuracy will depend on your image quality - TBH your first image looks like it deviates from a circle by more than 3 pixels. That said, once you do have the ring object found, you can refine the initial estimate. Plot the the inner and outer radius as a function of the angle. If these are sines, your center estimate is off, the phase of the sine tells you in which direction, and the amplitude by how much. Should be good down to subpixel accuracy. – MSalters Aug 18 '15 at 23:43
  • 1) improve you debugging skills :D 2) houghCircles in not the best if you need accurate detection, but you can refine the initial estimate later. 3) If you need perspective distorted circles, you need to search for ellipses. Your best shot is to segment the white area of the target, and then apply `fitEllipse` only on your distorted circle (you need to invert the image first in order to have a white line on black background) – Miki Aug 18 '15 at 23:47
  • @Ben Linux, Cmake, Vim. That's all of my programming environment. The code in the examples is not the same as the code in the tutorials, its similar but has changed. Im using the latest GIT OpenCV to assure everything is updated. – SOMN Aug 18 '15 at 23:58
  • @MSalters This is a test shot from a webcam. The target uses a Raspberry Pi with a camera module enclosed in a perfectly black box. The image from there is quite similar to the second image. I'll try to keep it simple to begin with. A circle estimate seems fine, but later I would prefer to have as much presision as possible. – SOMN Aug 19 '15 at 00:15
  • @Miki As it it was simple to understand C++ from one day to the next. ;)Yea, I'll leave the optimization for later. Something that works for now seems fine. Thanks you all for your tips. Can someone express that as an answer so I can tag it as a solution? – SOMN Aug 19 '15 at 00:18
  • 1
    Since you're a beginner both in C++ and OpenCV, I recommend to do one easy step at the time: load image, convert to grayscale, thresholding, hough transform, find connected components, etc... In SO there are a lot information on each individual step, or you can ask a new question. – Miki Aug 19 '15 at 00:22

0 Answers0