The second chapter describes how to implement the calibration-based Augmented reality on the iOS platform, including the following four aspects:
1. Build opencv project on iOS platform
2. Marker Detection and Recognition
3. Camera Calibration and marker pose estimation
4. rendering a 3D virtual object based on marker
The first part is the development of the iOS platform. I am not too concerned about it. The fourth part is the establishment of a 3D virtual object based on OpenGL, which is also based on the iOS platform, because OpenGL is also used in Chapter 3, we will keep Chapter 3 for anatomy. So here we mainly analyze the second part and the third part. This article introduces the second part. It seems like QR code recognition. I don't know how the QR code works.
Markerdetection class task processframe: Figure 1
Convert to grayscale image and binarization (fixed threshold method threshold: significantly affected by illumination, etc.; Adaptive Threshold Method adaptivethreshold: Better) (I used the adaptivethreshold function in opencv243 to implement the adaptive filtering effect. The effect is like the edge detection algorithm .. Finally, use the threshold function instead. This problem cannot be solved. I hope you can give some advice. PS: There are many online experts. This problem has been solved.) The detected result is threshold (on the left of Figure 1)
Findmarkercontours function for contour detection findcontours (it is best to use the vertices of a polygon to remove vertices smaller than the threshold value (not interested in small outlines) and sort the points of each Contour in a counter-clockwise order, and remove the contour that is too close. The result is contours (in Figure 1)
Next, the findmarkercandidates function filters the contour. First, use approxpolydp to obtain the polygon with the approximate contour. For filtering, if the polygon is a convex polygon and the vertex is 4, it is possible to be a marker and detect the side length of the Four Edges. If the Minimum side length is smaller than 10 pixel, it is not considered a marker. Then, sort the possible marker contour points in a counter-clockwise order. Check whether duplicate marker is detected. If duplicate marker is detected, remove the shorter perimeter. After this step, the effect is as follows: markercandidate (bottom left of Figure 1)
The detectmarkers function has three tasks to remove the influence of the projection transformation (getperspectivetransform gets the projection matrix and warpperspective gets the front view image) and get the front view of marker.
Decode the front graph of this marker, and threshold evaluates marker using thresh_otsu. :
Next, identify the marker. Decode. The detection code marker (decoded to the marker, the marker code is a 7*7 raster, the Center 5*5 is the ID, and the circle around it is a black boundary, during the detection, check whether a circle is a black boundary before decoding the Center 5*5 (note that only 5*5 has a unique code that is rotated without deformation ), it is a 5bit * 5 word. The 5bit in each word has the ID (2 digits and 4 digits) and the third digit is the verification code (used to ensure rotation ), therefore, 5word has a total of 2 ^ 10 = 1024 different IDs, and the first digit must be reversed to prevent a word from being completely black and difficult to detect. For example, the five word IDs of the marker I use here are 10, 01, 11, 11, and 11, respectively. Then, how can we extract the 2-value grid of 7*7 from the obtained marker chart? Here we use a mat (rect) to retrieve the small square in the marker and use countnonzero to determine that this square is 0or1. Because the marker has four directions, and which direction is the ID of the corresponding marker, here we use the ID and the Hamming distance of the ID used for my verification as the basis, the smallest Hamming distance is the direction of marker.
After you confirm it as a marker and then obtain the detailed corner of the contour, you can use cornersubpix to refine it because this function is relatively time-consuming. If you refine each corner before, because there are many candidate targets, will increase the computing burden .) Marker (bottom right of Figure 1 ).
Finally, I tried other marker codes to correctly decode the ID information, as follows: the ID of the first image is 0011010101, and the second image is the rotation of the first image, with the same ID, the ID of the third image is 0011000110, and the fourth image is not a marker, so it is not detected.
The: http://download.csdn.net/detail/yang_xian521/5040634 of the Code
In the next article, we will introduce how to create a 3D virtual object on the marker with the contour position of the marker and the direction (yellow) of the contour (red.