Opencv for iOS Study Notes (4)-mark Detection 1

Source: Internet
Author: User

Original address: opencv for iOS Study Notes (4)-mark Detection 1

A simple tag is often a regular image consisting of a white block and a black block. Because we know these factors in advance, we can easily detect tags.

First, we need to find the closed profile and then check our mark in the Rectangular profile.

The following is the process for marking the monitoring pipeline:


1. Convert the input image into a grayscale image.

2. Perform binary threshold operations (perform binary threshold operation ).

3. Detect the image contour.

4. Search for possible tags.

5. Detect and decode the mark.

6. Simulate the 3D posture (SHAPE) of the mark ).


First, Grayscale Images

Cvcvtcolor

// Grayscale void markerdetector: prepareimage (const CV: mat & bgramat, CV: mat & grayscale) {// convert grayscale CV: cvtcolor (bgramat, grayscale, cv_bgr2gray );}

Second, image binarization)

For binarization, refer to: opencv binarization method

The binarization operation will convert each pixel of our image to Black (zero intensity) or white (intensity). First, we need to find the contour. Currently there are many ways to determine the threshold value, however, they have their own advantages and disadvantages.

The simple and quick method is the absolute threshold method. The result depends on the pixel intensity and some threshold values. That is, if the pixel intensity is greater than the threshold value, the result is white (255 ), otherwise it will be black (0 ).

But this method has one of the biggest drawbacks-it relies on soft intensity changes ). Therefore, the more desirable method is the Adaptive Threshold Value-the biggest difference is that all pixels are used within the radius with the detected pixel as the center. Use the average intensity to ensure more robust corner detection.

Opencv learning notes-Adaptive Threshold

Void markerdetector: performthreshold (const CV: mat & grayscale, CV: mat & thresholdimg) {// input image // output image // use the maximum value of cv_thresh_binary and cv_thresh_binary_inv // use the adaptive threshold algorithm: cv_adaptive_thresh_mean_c or threshold type: it must be one of the following: // cv_thresh_binary, // cv_thresh_binary_inv // The pixel neighborhood size used to calculate the threshold value: 3, 5, 7 ,... // CV: adaptivethreshold (grayscale, thresholdimg, 255, CV: adaptive_thresh_gaussian_c, CV: thresh_binary_inv, 7, 7 );}

Contour detection

The output of this function is a set of polygon. Each polygon represents a possible contour. In this method, we ignore polygon smaller than minw.spointallowed, because we think they are either not valid contours, or they are too small to be detected.

Void markerdetector: findcontours (const CV: mat & thresholdimg, STD: vector <CV: point> & contours, int mincontourspointallowed) {// all the outlines of STD: vector <CV: point> allcontours; // The input image must be a two-value single-channel image // an array of detected outlines, each profile is represented by a point-type vector // The profile retrieval mode/* cv_retr_external indicates that only the contour detected by cv_retr_list is detected. cv_retr_ccomp is used to establish a hierarchical relationship, the layer above is the outer boundary, and the layer inside is the boundary information of the inner hole. If there is another connected object in the inner hole, the boundary of this object is also on the top. Cv_retr_tree creates a hierarchical tree outline. For more information, see S. c This demo * // The approximate method of the contour/* cv_chain_approx_none stores all the contour points, the pixel location difference between the adjacent two points does not exceed 1, that is, Max (ABS (x1-x2 ), ABS (y2-y1) = 1 cv_chain_approx_simple compression horizontal direction, vertical direction, diagonal direction of the elements, only retain the end coordinate of this direction, for example, a rectangle profile only needs four points to save the profile information cv_chain_approx_tc89_l1. cv_chain_approx_tc89_kcos uses the Teh-chinl chain Approximate Algorithm offset to represent the offset of the contour point, which can be set to any value. This parameter is useful when the contour of the ROI image is identified and analyzed throughout the image. */CV: findcontours (thresholdimg, allcontours, cv_retr_list, cv_chain_approx_none); S. clear (); For (size_t I = 0; I <allcontours. size (); I ++) {int size = allcontours [I]. size (); If (size> min1_spointallowed) {S. push_back (allcontours [I]) ;}}

Below is the outline we have detected:

Threshold

The threshold is the critical value. The threshold value in PS is actually a black/white demarcation value based on the image brightness. The default value is 50% Neutral gray, that is, 128, and the brightness is higher than 128 (<50% gray) will become white, less than 128 (> 50% gray) will become black (can be retained with the other in the filter-high contrast, and then the threshold effect will be better)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.