Image target recognition and counting method
By employing dual-template matching and adaptive binarization strategies, the adaptability problem of image target recognition and counting under different background brightness conditions is solved, achieving efficient and accurate image target recognition and counting.
Patent Information
- Application Number
- CN202611391921.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-09
- Publication Date
- 2026-11-06
AI Technical Summary
Existing technologies have poor adaptability and high false detection rates when processing images with different background brightness, and they are almost ineffective in dark scenes, making it impossible to effectively identify and count targets in images.
A dual-template matching strategy is adopted. By acquiring template images under different background brightness, and combining multi-scale normalized cross-correlation matching and adaptive binarization, the background style is automatically identified and accurately counted.
It improves the matching success rate and counting accuracy of image target recognition, reduces the false detection rate, and is suitable for large-scale batch processing on ordinary CPUs.
Smart Images

Figure CN123052433A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to a method for image target recognition and counting. Background Technology
[0002] In automated image processing scenarios, it is often necessary to extract specific visual elements from a large number of screenshots and perform quantity statistics. For example, in the content review, data statistics and automated operation of e-commerce platforms, it is necessary to identify specific icons and their associated elements in the screenshots to determine the interactive popularity or reward eligibility of the content.
[0003] Existing technical solutions typically employ the following methods:
[0004] One method is the single-template fixed threshold method, which prepares only one template image and calculates the similarity by sliding the template across the entire image. If the similarity is higher than the fixed threshold, it is determined to be the target. The drawback of this method is that when the screenshot has different background brightness styles, a single template cannot be adapted to them at the same time. The edge contrast of the target icon under the dark theme is significantly different from that under the light theme, resulting in a very high matching failure rate.
[0005] The second method is single-threshold binarization, which converts the image to grayscale and then uses a single fixed pixel value for binarization segmentation. The drawback of this method is that the same fixed threshold cannot be applied to both bright and dark background images at the same time. If the threshold is set too high, the dark background image will be completely black, and if the threshold is set too low, the bright background image will be completely white. No matter how it is adjusted, there will be a large number of missed detections.
[0006] Thirdly, there are deep learning detection methods, such as object detection models based on YOLO and Faster-RCNN. The drawback of this method is that it requires a large number of manually labeled training samples, resulting in high training costs, slow inference speed, and unfriendly performance on edge devices or in batch processing scenarios.
[0007] Fourth is the global brightness statistical diversion method, which determines brightness by calculating the average grayscale value of the entire image and then selecting different thresholds. The drawback of this method is that the global average value cannot reflect the local contrast characteristics of the target area. When there are large areas of neutral color blocks in the screenshot, the global average value will deviate significantly from the true brightness distribution of the target area, resulting in diversion errors.
[0008] In summary, existing technologies suffer from poor adaptability, high false detection rates, and near-failure in dark background scenes when processing batch screenshots with varying background brightness styles. Summary of the Invention
[0009] To address the aforementioned technical problems, this invention provides an image target recognition and counting method that directly drives the selection of a binarization strategy through dual-template matching results, thereby achieving adaptive switching of target recognition under different background brightness levels.
[0010] An image target recognition and counting method of the present invention includes the following steps:
[0011] S1. Search area extraction:
[0012] Obtain the image to be processed and convert it into a grayscale pixel matrix:
[0013] ;
[0014] According to the preset anchor point coordinates ( , and tolerance along the first direction (X-axis) And tolerance along the second direction (Y-axis) Extract a rectangular search region R from the grayscale pixel matrix:
[0015]
[0016] ;
[0017] in( , (W,H) represents the template size, and (W,H) represents the image width and height. This step reduces the search range for subsequent matching from the entire image to a local region, thus reducing the amount of computation.
[0018] S2, Dual Template Acquisition:
[0019] Get the first style template image Second style template image The first style template image corresponds to the target icon style under the first type of background brightness, such as the target icon under the dark theme. The second style template image corresponds to the target icon style under the second type of background brightness, such as the target icon under the light theme. The two types of templates capture the appearance features of the target icon under different background brightness.
[0020] S3, Multi-scale Normalized Cross-correlation Matching:
[0021] right and Perform multi-scale normalized cross-correlation matching separately, and the scaling coefficient sequence is generated by the following formula:
[0022] ;
[0023] in =0.8, =0.1,
[0024] ;
[0025] Preferred =4; at all scales Next, after scaling the template to this scale, the normalized cross-correlation coefficient between the template and the search region is calculated using the TM_CCOEFF_NORMED algorithm:
[0026] ;
[0027] in The grayscale value of the template pixel. This represents the average grayscale value of the template. is the grayscale value corresponding to the search area, and ō is the average grayscale value of the search area;
[0028] S4. Shape Validation and Best Match Update:
[0029] During the traversal at each scale, when the current matching degree of any template... Greater than the highest recorded match When the matching degree is reached, calculate the aspect ratio of the bounding rectangle of the matching region:
[0030] ;
[0031] Only when (Preferred) =0.85, When the match score is 1.25, update the match score accordingly. And record the corresponding matching position, scale, and template affiliation;
[0032] This step utilizes the geometric feature that the target icon is close to a square to eliminate matching results with inconsistent aspect ratios, thereby reducing the false detection rate.
[0033] S5. Anchor point confirmation and background mode determination:
[0034] like (Preferred) =0.5), then The matching position corresponding to the highest matching degree is determined as the target anchor point. , And the template corresponding to this matching degree is determined as the background pattern:
[0035] ;
[0036] The key to this step is that the background mode is not determined by global brightness statistics or manual settings, but by the template matching results. The template matching score is automatically determined, which indicates which background style the current screenshot belongs to. This is more accurate than global brightness average distribution because template matching directly perceives the local contrast characteristics of the target area.
[0037] S6. Dynamic Region of Interest (ROI) Clipping:
[0038] With target anchor point ( , Based on this, the strip-shaped region of interest is cropped from the image to be processed according to the following offset rules:
[0039]
[0040]
[0041] ;
[0042] in The width of the anchor point. The anchor point height is; the coordinates of the clipping start point are ( +Δx, +Δy), the width of the clipping region is W−( +Δx), the cutting height is This step utilizes prior knowledge of interface layout, with related elements located on the right side of the target icon in the same row and with a relatively fixed height;
[0043] S7. Background adaptive binarization:
[0044] Select the binarization segmentation direction based on the background pattern, and use the first brightness threshold. Second brightness threshold Determined by the local grayscale distribution in the corresponding mode:
[0045]
[0046] ;
[0047] in (·) represents the grayscale matrix The p-th quantile of (the grayscale matrix of the striped region of interest);
[0048] When the background mode is the first mode (dark background), forward binarization (THRESH_BINARY) is used:
[0049] ;
[0050] When the background mode is mode 2 (bright background), inverse binarization (THRESH_BINARY_INV) is used:
[0051] ;
[0052] Preferred, Corresponding to 60, Corresponding to 230;
[0053] The key innovation of this step is that the binarization strategy is not determined by manual judgment or global brightness statistics, but is automatically determined by the template matching results. The template matching score with the highest score will be automatically selected to select the corresponding binarization parameters and quantile strategy.
[0054] S8. Vertical projection and gap identification counting:
[0055] For binarized images Perform column-direction pixel projection:
[0056] ;
[0057] The gap threshold G is determined by the following formula:
[0058] ;
[0059] Preferred =5, traverse the projection signal Use a state machine to identify valid segments:
[0060] when > (Preferred) When it enters the effective segment state; when Gap verification is initiated when =0, and only when the verification window... The projection values of each column are not greater than When the gap is confirmed as a valid dividing point, the width is calculated to fall within the specified range. Number of valid segments ( For the target standard width, the preferred option is... ), as the target count value .
[0061] Preferably, an image target recognition system includes a search region cropping module, which performs the steps of acquiring the image to be processed and converting it into a grayscale pixel matrix, and cropping a rectangular search region according to preset anchor point coordinates and tolerance.
[0062] The dual-template multi-scale matching module is used to execute the steps of the method, including obtaining the first template image and the second template image, performing multi-scale normalized cross-correlation matching, and determining the target anchor point and background pattern.
[0063] The Region of Interest (ROI) trimming module is used to execute the method's step of trimming a strip-shaped ROI based on the target anchor point and according to a preset offset rule.
[0064] The background adaptive binarization module is used to execute the method's step of selecting the binarization segmentation direction based on the background pattern to generate a binarized image;
[0065] The projection counting module is used to execute the method's steps of performing column-direction pixel projection on the binarized image and counting the number of valid segments as the target count value;
[0066] Preferably, the method further includes a computer-readable storage medium having a computer program stored thereon, characterized in that the program, when executed by a processor, implements the steps of the method.
[0067] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0068] 1. By matching the first and second templates in parallel, the background style of the current screenshot is automatically identified, improving the matching success rate;
[0069] 2. The binarization strategy is automatically determined by the template matching results, rather than being manually set or simply switching brightness thresholds. The first mode uses forward binarization. Preferred mode 60, second mode uses inverse binarization ( The preferred strategy 230 precisely adapts to the respective background styles, increasing the recall rate of dark background scenes from approximately 10% with the single-template fixed threshold solution to 85%.
[0070] 3. (Preferred) =0.85, =1.25), which eliminates visually similar but misshapen elements such as flat bubbles and thin decorative strips, resulting in a lower false detection rate compared to pure template matching;
[0071] 4. By using an adaptive gap threshold G and a forward verification mechanism, closely spaced targets can be effectively distinguished, thus improving counting accuracy.
[0072] 5. The entire solution is based on classic computer vision algorithms, requiring no labeled data or GPU computing power. On a regular CPU, the processing time for a single image is approximately 20 milliseconds, meeting the needs of large-scale batch processing. Attached Figure Description
[0073] Figure 1 This is a schematic flowchart of the image target recognition and counting method of the present invention;
[0074] Figure 2 This is a schematic diagram of an image target recognition system. Detailed Implementation
[0075] To facilitate understanding of the present invention, a more complete description will be given below with reference to the accompanying drawings. The present invention can be implemented in many different forms and is not limited to the embodiments described herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete.
[0076] Example 1
[0077] An image target recognition and counting method of the present invention includes the following steps:
[0078] S1. Search area extraction:
[0079] Obtain the image to be processed and convert it into a grayscale pixel matrix:
[0080] ;
[0081] According to the preset anchor point coordinates ( , and tolerance along the first direction (X-axis) And tolerance along the second direction (Y-axis) Extract a rectangular search region R from the grayscale pixel matrix:
[0082]
[0083] ;
[0084] in( , (W,H) represents the template size, and (W,H) represents the image width and height. This step reduces the search range for subsequent matching from the entire image to a local region, thus reducing the amount of computation.
[0085] S2, Dual Template Acquisition:
[0086] Get the first style template image Second style template image The first style template image corresponds to the target icon style under the first type of background brightness, such as the target icon under the dark theme. The second style template image corresponds to the target icon style under the second type of background brightness, such as the target icon under the light theme. The two types of templates capture the appearance features of the target icon under different background brightness.
[0087] S3, Multi-scale Normalized Cross-correlation Matching:
[0088] right and Perform multi-scale normalized cross-correlation matching separately, and the scaling coefficient sequence is generated by the following formula:
[0089] ;
[0090] in =0.8, =0.1,
[0091] ;
[0092] Preferred =4; at all scales Next, after scaling the template to this scale, the normalized cross-correlation coefficient between the template and the search region is calculated using the TM_CCOEFF_NORMED algorithm:
[0093] ;
[0094] in The grayscale value of the template pixel. This represents the average grayscale value of the template. is the grayscale value corresponding to the search area, and ō is the average grayscale value of the search area;
[0095] S4. Shape Validation and Best Match Update:
[0096] During the traversal at each scale, when the current matching degree of any template... Greater than the highest recorded match When the matching degree is reached, calculate the aspect ratio of the bounding rectangle of the matching region:
[0097] ;
[0098] Only when (Preferred) =0.85, When the match score is 1.25, update the match score accordingly. And record the corresponding matching position, scale, and template affiliation;
[0099] S5. Anchor point confirmation and background mode determination:
[0100] like (Preferred) =0.5), then The matching position corresponding to the highest matching degree is determined as the target anchor point. , And the template corresponding to this matching degree is determined as the background pattern:
[0101] ;
[0102] S6. Dynamic Region of Interest (ROI) Clipping:
[0103] With target anchor point ( , Based on this, the strip-shaped region of interest is cropped from the image to be processed according to the following offset rules:
[0104]
[0105]
[0106] ;
[0107] in The width of the anchor point. The anchor point height is; the coordinates of the clipping start point are ( +Δx, +Δy), the width of the clipping region is W−( +Δx), the cutting height is ;
[0108] S7. Background adaptive binarization:
[0109] Select the binarization segmentation direction based on the background pattern, and use the first brightness threshold. Second brightness threshold Determined by the local grayscale distribution in the corresponding mode:
[0110]
[0111] ;
[0112] in (·) represents the grayscale matrix The p-th quantile of (the grayscale matrix of the striped region of interest);
[0113] When the background mode is the first mode (dark background), forward binarization (THRESH_BINARY) is used:
[0114] ;
[0115] When the background mode is mode 2 (bright background), inverse binarization (THRESH_BINARY_INV) is used:
[0116] ;
[0117] In the preferred example, Corresponding to 60, Corresponding to 230;
[0118] S8. Vertical projection and gap identification counting:
[0119] For binarized images Perform column-direction pixel projection:
[0120] ;
[0121] The gap threshold G is determined by the following formula:
[0122] ;
[0123] Preferred =5, traverse the projection signal Use a state machine to identify valid segments:
[0124] when > (Preferred) When it enters the effective segment state; when Gap verification is initiated when =0, and only when the verification window... The projection values of each column are not greater than When the gap is confirmed as a valid dividing point, the width is calculated to fall within the specified range. Number of valid segments ( For the target standard width, the preferred option is... ), as the target count value .
[0125] Example 2
[0126] The present invention provides an image target recognition and counting method, which, based on Embodiment 1, includes search region cropping and dual-template multi-scale matching:
[0127] The system reads the image to be processed, I, converts it into a grayscale pixel matrix, and obtains the preset anchor point coordinates. =(50,1400), first direction tolerance =100, second direction tolerance =400, the system uses the formula:
[0128] ,
[0129]
[0130]
[0131] ;
[0132] Calculate the boundary of the search region, extract the region from the grayscale pixel matrix, and read the first style template image. Second style template image (The target icon styles corresponding to the light-colored theme) are all converted to grayscale images, and the system processes them according to the formula. Generate a scaling factor sequence [0.8, 0.9, 1.0, 1.1, 1.2], and perform TM_CCOEFF_NORMED matching on both templates respectively.
[0133] Example 3
[0134] The present invention provides an image target recognition and counting method, which, based on Embodiment 1, includes shape verification and background pattern determination:
[0135] At each scale, when the matching degree is greater than the current highest value, it is calculated according to the formula:
[0136] ;
[0137] Calculate the aspect ratio of the scaled template. If the ratio is between 0.85 and 1.25, update the highest matching degree, optimal position, optimal scale, and template affiliation. After traversal, determine whether the highest matching degree is greater than or equal to the threshold. If so, then follow the formula:
[0138]
[0139] ;
[0140] Calculate the absolute coordinates of the anchor point in the original image and record the background mode: If If the winner is selected, then Mode = First Mode. If you win, then Mode = Second Mode.
[0141] Example 4
[0142] The present invention provides an image target recognition and counting method, which, based on Embodiment 1, dynamically crops the region of interest:
[0143] According to the formula Calculate the cutting parameters, with the cutting starting point being... The cutting width is The cutting height is The rectangular region is cropped from the original image and designated as the region of interest for subsequent processing.
[0144] Example 5
[0145] The present invention provides an image target recognition and counting method, which, based on Embodiment 1, incorporates adaptive background binarization:
[0146] The binarization strategy is selected based on the background pattern. If it is the first pattern (dark background), the system calculates... The preferred value is 60. Forward binarization is used to set pixels with a grayscale value greater than 60 to 255 (white foreground) and the rest to 0 (black background). If it is the second mode (bright background), the system calculates... The preferred value is 230. Inverse binarization is used to set pixels with grayscale values less than 230 to 255 (white foreground) and the rest to 0 (black background).
[0147] Example 6
[0148] The present invention provides an image target recognition and counting method, which, based on Embodiment 1, incorporates vertical projection and gap recognition counting:
[0149] To sum the white pixel values column-wise in a binarized image, use the following formula:
[0150] ;
[0151] Generate the projection array. The system uses the formula:
[0152] ;
[0153] Calculate the gap threshold, preferably =5, the system iterates through the projection array and uses a state machine to identify the valid segments:
[0154] when When it enters the effective segment state, when At time 0, gap verification is initiated. Looking forward at column G, if the projected values within column G are all no greater than 2, the gap is confirmed as valid. The system records the start and end positions of each valid segment, and then calculates the gap according to the formula. Check if the segment width is valid. If valid, increment the count by 1. After traversing all segments, output the total count. .
[0155] Example 7
[0156] The present invention provides an image target recognition and counting method. Based on Embodiment 1, in a specific application scenario of the present invention, the image to be processed is a screenshot of an e-commerce client. The first template image is a like icon style under a dark theme, and the second template image is a like icon style under a light theme. The striped region of interest contains the row of user avatars in the same row as the like icon. The target count value is the number of avatars in the same row. Based on the relationship between the target count value and a preset effective threshold, the image to be processed can be divided into an undetected class, an insufficient number class, or a successfully matched class for subsequent business judgment.
[0157] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for image target recognition and counting, comprising the following steps: S1. Obtain the image to be processed and convert it into a grayscale pixel matrix. Based on the preset anchor point coordinates and the tolerance along the first and second directions, extract a rectangular search area from the grayscale pixel matrix. S2. Obtain a first template image and a second template image. The first template image corresponds to the target style under the first type of background brightness, and the second template image corresponds to the target style under the second type of background brightness. S3. Perform multi-scale normalized cross-correlation matching on the first template image and the second template image respectively. Traverse multiple scales within the preset scaling coefficient range, calculate the cross-correlation coefficient between the template and the search area at each scale, and obtain the matching degree and matching position of each template at each scale. Its characteristic is that it further includes the following steps: S4. During the traversal of each scale, when the current matching degree of any template is greater than the highest matching degree recorded, calculate the aspect ratio of the bounding rectangle of the matching area corresponding to the matching degree. Only when the aspect ratio falls within the preset geometric ratio range, update the highest matching degree with the matching degree and record the corresponding matching position, scale and template affiliation. S5. If the highest matching degree is not less than the preset matching threshold, the matching position corresponding to the highest matching degree is determined as the target anchor point, and the template corresponding to the matching degree is determined as the background mode. The background mode includes the first mode represented by the first template winning and the second mode represented by the second template winning. S6. Using the target anchor point as a reference, crop the strip-shaped region of interest from the image to be processed according to the preset offset rules; S7. Select the binarization segmentation direction according to the background mode. When the background mode is the first mode, use forward binarization to extract pixels with gray values greater than the first brightness threshold as the foreground. When the background mode is the second mode, use reverse binarization to extract pixels with gray values less than the second brightness threshold as the foreground, and generate a binarized image. S8. Perform column-direction pixel projection on the binarized image, accumulate the number of foreground pixels by column to obtain a one-dimensional projection signal, identify the effective segment separation points in the projection signal based on the preset gap threshold and the verification window, and count the number of effective segments whose width falls within the preset segment width interval as the target count value.
2. The image target recognition and counting method as described in claim 1, characterized in that, The aspect ratio of the circumscribed rectangle in S4 is defined as follows: ; in To match the region width, To match the height of the region; The preset geometric ratio range is , where 𝜌min=0.85, 𝜌max=1.
25.
3. The image target recognition and counting method as described in claim 1, characterized in that, The preset scaling factor sequence in S3 is generated by the following formula: ; in The minimum scaling factor. To scale the step size, The largest scale index; =0.8, = 0.1, ; =4; Normalized cross-correlation matching uses the TM_CCOEFF_NORMED algorithm, and its cross-correlation coefficient is: ; in The grayscale value of the template pixel. This represents the average grayscale value of the template. is the grayscale value of the corresponding location in the search area, and ō is the average grayscale value of the search area.
4. The image target recognition and counting method as described in claim 1, characterized in that, The first brightness threshold in S7 is determined by the following formula: ; The second brightness threshold is determined by the following formula: ; in (·) represents the p-th quantile of the gray matrix. This is a grayscale matrix representing the region of interest in a strip shape. =0.15, =0.85, corresponding value =60, =230.
5. The image target recognition and counting method as described in claim 1, characterized in that, The default offset rule in S6 is: ; in The width of the anchor point. The anchor point height is [value]; the cropping region width is the remaining width of the image to be processed in the first direction; and the coordinates of the cropping start point are ([value]). , ).
6. The image target recognition and counting method as described in claim 1, characterized in that, The preset gap threshold in S8 is determined by the following formula: ; in For the target standard width, For a single column of pixels, γ=0.
05. =3; G=5; The verification window length is W_v=G. The gap is confirmed as a valid separation point only when the projection values of each column in the verification window are not greater than the effective projection threshold pth. pth=2; The preset segment width range is [0.8]. , 1.2 ].
7. The image target recognition and counting method as described in claim 1, characterized in that, The boundary of the rectangular search region in S1 is obtained by adding or subtracting corresponding tolerances along the first and second directions from the preset anchor point coordinates, and then intersecting with the image boundary. ; in( , ) represents the anchor point coordinates, ( , (W, H) represents the tolerance, and (W, H) represents the image width and height. , () represents the template size.
8. An image target recognition system, characterized in that, include: The search region cropping module is used to perform the steps of acquiring the image to be processed and converting it into a grayscale pixel matrix, and cropping a rectangular search region according to the preset anchor point coordinates and tolerance as described in any one of claims 1 to 7. A dual-template multi-scale matching module is used to perform the steps of acquiring a first template image and a second template image, performing multi-scale normalized cross-correlation matching, and determining the target anchor point and background pattern as described in any one of claims 1 to 7. The region of interest (ROI) cropping module is used to perform the step of cropping the strip-shaped ROI based on the target anchor point and according to a preset offset rule as described in any one of claims 1 to 7. Background adaptive binarization module, used to perform the step of generating a binarized image by selecting a binarization segmentation direction according to a background pattern in the method as described in any one of claims 1 to 7; The projection counting module is used to perform the step of performing column-direction pixel projection on the binarized image and counting the number of valid segments as a target count value according to any one of claims 1 to 7.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by a processor, it implements the steps of the method as described in any one of claims 1 to 7.
