Finding a chessboard in an image - algorithm

So, I initially assumed that finding a chessboard in an image should be trivial because it is such an easily defined object. However it isnt so easy and I was wondering if someone knows how the chessboard finder "cvFindChessboardCorners" works in OpenCV. Ive tried googling it but havnt managed to find the algorithm. Im guessing maybe the following:
1)Binarize
2)Open and close to eliminate small clusters
A)
2)Find Harris corners
3)Create distance matrix between all points in image
4)...?
B)
2)Find hough transform
3)all significant lines are checked for where they intersect. If 4 or more lines intersect at a point then these lines are part of the chessboard. This includes the point at infinity.
4)?
Anyone know exactly?

It's pretty ... complicated :) If you want to know exactly, the source of opencv would be the place to look - in opencv 2.2 it's in modules/calib3d/src/calibinit.cpp line 219. It also has a DEBUG_CHESSBOARD compile switch to be able to see how it works.

Related

OpenCV and OsX HEAD detection

Im looking to create a program that detects HEADS with opencv, not just faces. There must be a way to do this. Besides heads, I need to identify the most high pixel of the head (the top part of the hair) and the low center point of the chin... I'm not finding any OS X OpenCV examples.. Here is a picture of what I'd like to achieve...https://pasteboard.co/GF19Fao.jpg
Looks simple enough right?
I would recommend looking into haarcascades for this.
This consists of a couple cascades not included in OpenCV.
Method 1:
The profile and frontal face ones are a good starting point.
Personally, I have not tested them yet so it is hard for me to tell you what the results look like.
If the final bounding box fits perfectly over the entire head alone, then you can make the following assumptions as a starting point
if box = (topLeft, topRight, bottomLeft, bottomRight)
then hairTop = distance(topLeft, topRight)/2
and chin = distance(bottomLeft, bottomRight)/2
If not you could do two things:
1. Make some measurement readjustments to see how far up or down to move the resulting rectangle in order to find the chin and top hair.
2. You could use a combination of other classifiers as well.
Combine the results of the front/profile face with the mouth classifier to find the chin
Combine the front/profile face with upperbody classifier to find the top hair.
Method 2
You could also use the front/profile face classifiers to find the top hair and just use mouth to find the chin.
Either methods require that you run multiple tests to find the optimal values/estimates that fulfill your task.

Algorithm to best fit a rectangle

I'm writing an application which measures boxes from pictures. A sample picture after manipulation is shown below:
My application has identified pixels that are part of the box and changed the color to red. You can see that the image is pretty noisy and therefore creates pretty rough looking edges on the rectangle.
I've been reading about edge/corner detection algorithms, but before I pursue one of them I wanted to step back and see if such a complicated algorithm is really necessary. It seems like there probably is a simpler way to go about this, considering I have a few conditions that simplify things:
The image only contains a rectangle, not any other shape.
Each image only has 1 rectangle.
I do not need to be exact, though I'd like to achieve as best fit as I can.
My first go at a simple algorithm involved finding the top most, bottom most, left most and right most points. Those are the 4 corners. That works OK, but isn't super accurate for noisy edges like this. It is easy to eye ball a much better point as the corner.
Can anyone point me towards an algorithm for this?
You have already identified the region of the image that you are interested in(red region).
Using this same logic you should be able to binarize the image. Say the red region then results in white pixels and the rest is black.
Then trace the external contour of the white region using a contour tracing algorithm.
Now you have a point set that represents the external contour of the region.
Find the minimum-area-rectangle that bounds this point set.
You can easily do this using the OpenCV library. Take a look at threshold, findContours, and minAreaRect if you are planning to use OpenCV. Hope this information helps.

Finding cross on the image

I have set of binary images, on which i need to find the cross (examples attached). I use findcontours to extract borders from the binary image. But i can't understand how can i determine is this shape (border) cross or not? Maybe opencv has some built-in methods, which could help to solve this problem. I thought to solve this problem using Machine learning, but i think there is a simpler way to do this. Thanks!
Viola-Jones object detection could be a good start. Though the main usage of the algorithm (AFAIK) is face detection, it was actually designed for any object detection, such as your cross.
The algorithm is Machine-Learning based algorithm (so, you will need a set of classified "crosses" and a set of classified "not crosses"), and you will need to identify the significant "features" (patterns) that will help the algorithm recognize crosses.
The algorithm is implemented in OpenCV as cvHaarDetectObjects()
From the original image, lets say you've extracted sets of polygons that could potentially be your cross. Assuming that all of the cross is visible, to the extent that all edges can be distinguished as having a length, you could try the following.
Reject all polygons that did not have exactly 12 vertices required to
form your polygon.
Re-order the vertices such that the shortest edge length is first.
Create a best fit perspective transformation that maps your vertices onto a cross of uniform size
Examine the residuals generated by using this transformation to project your cross back onto the uniform cross, where the residual for any given point is the distance between the projected point and the corresponding uniform point.
If all the residuals are within your defined tolerance, you've found a cross.
Note that this works primarily due to the simplicity of the geometric shape you're searching for. Your contours will also need to have noise removed for this to work, e.g. each line within the cross needs to be converted to a single simple line.
Depending on your requirements, you could try some local feature detector like SIFT or SURF. Check OpenSURF which is an interesting implementation of the latter.
after some days of struggle, i came to a conclusion that the only robust way here is to use SVM + HOG. That's all.
You could erode each blob and analyze their number of pixels is going down. No mater the rotation scaling of the crosses they should always go down with the same ratio, excepted when you're closing down on the remaining center. Again, when the blob is small enough you should expect it to be in the center of the original blob. You won't need any machine learning algorithm or training data to resolve this.

Matching a curve pattern to the edges of an image

I have a target image to be searched for a curve along its edges and a template image that contains the curve. What I need to achieve is to find the best match of the curve in the template image within the target image, and based on the score, to find out whether there is a match or not. That also includes rotation and resizing of the curve. The target image can be the output of a Canny Edge detector if that makes things easier.
I am considering to use OpenCV (by using Python or Processing/Java or if those have limited access to the required functions then by using C) to make things practical and efficient, however could not find out if I can use any functions (or a combination of them) in OpenCV that are useable for doing this job. I have been reading through the OpenCV documentation and thought at first that Contours could do this job, however all the examples show closed shapes as opposed to my case where I need to match a open curve to a part of an edge.
So is there a way to do this either by using OpenCV or with any known code or algorithm that you would suggest?
Here are some images to illustrate the problem:
My first thought was Generalized Hough Transform. However I don't know any good implementation for that.
I would try SIFT or SURF first on the canny edge image. It usually is used to find 2d areas, not 1d contours, but if you take the minimum bounding box around your contour and use that as the search pattern, it should work.
OpenCV has an implementation for that:
Features2D + Homography to find a known object
A problem may be getting a good edge image, those black shapes in the back could be distracting.
Also see this Stackoverflow answer:
Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

Separating Axis Theorem - Containment and the minimum translation vector

My code to calculate the minimum translation vector using the Separating Axis Theorem works perfectly well, except when one of the polygons is completely contained by another polygon. I have scoured the internet for the solution to this problem and everyone just seems to ignore it ( http://www.codezealot.org/archives/55#sat-contain talks about this, but doesn't give a full solution...)
The pictures below is a screenshot from my program illustrating the problem. The translucent blue triangle is the position of the rectangle before the MTV is applied, and the other triangle is with the MTV applied.
It seems to me that the link you shared does give a solution to this. In your MTV calculation, you have to test for complete containment in a projection and change the calculations accordingly. (The pseudocode is in reference to figure 9 on that page.) Perhaps if you post your code, we can comment on why it isn't working.

Resources