A method for recognizing a single-color object in an image based on a gradient-based adjustment threshold
By automatically updating the threshold using a gradient-based solution method, the accuracy problem caused by illumination changes in monochrome object recognition is solved, achieving efficient and real-time monochrome object detection, suitable for low-computing-power devices.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-12
- Publication Date
- 2026-03-24
AI Technical Summary
Existing technologies for recognizing monochrome objects suffer from high training costs, high hardware performance requirements, and poor real-time performance. Especially in outdoor scenes with changing lighting, fixed threshold segmentation may result in some structures of the object not being identified, affecting the accuracy of morphological detection. Furthermore, manually adjusting the threshold increases manpower and time costs.
A gradient-based solution method is adopted to find object edges by calculating the color gradient of the image and automatically update the threshold to adapt to environmental changes. Combined with HSV format and morphological detection, the image preprocessing and morphological detection steps are optimized to reduce computational pressure.
It enables automatic threshold adjustment under varying lighting conditions, reducing computational burden, improving real-time performance and accuracy of recognition, minimizing manual intervention, and making it suitable for low-computing-power devices.
Smart Images

Figure CN116758304B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method for identifying monochrome objects in an image based on gradient-based threshold adjustment, belonging to the field of image recognition technology. Background Technology
[0002] With the continuous development of computer science, image recognition technology is being used more and more widely. Artificial intelligence provides a continuous driving force for the development of image recognition technology, while also putting forward new requirements for hardware performance.
[0003] In the scenario of recognizing monochrome objects, the commonly used YOLO algorithm, while offering high accuracy, suffers from drawbacks such as high training costs, demanding hardware requirements, and poor real-time performance. Since monochrome objects have relatively distinct color features, traditional methods combining color and morphological detection can also accomplish the task. However, in outdoor scenes, unpredictable changes in lighting due to variations in sun position and weather conditions, coupled with the poor sensitivity of some cameras, can lead to significant color variations in the same object captured at different times. This can result in some parts of the object's structure not being segmented when using a fixed threshold for image segmentation, thus affecting morphological detection and causing recognition failure. Manually adjusting the threshold significantly increases both labor and time costs. Summary of the Invention
[0004] To address the shortcomings of existing technologies, this invention provides a method for identifying monochrome objects in an image based on gradient-based threshold adjustment. Given an initial threshold, a portion of the object region is selected, and then the color gradient of the image is calculated to find possible object edges. Among these, the edges containing the defined object regions are identified as the edges of the objects to be tested. The threshold is then automatically updated based on the color value distribution of the finally selected region.
[0005] The technical solution of the present invention is as follows:
[0006] A method for identifying monochromatic objects in an image based on gradient-based threshold adjustment includes the following steps:
[0007] S1: Set the threshold range in the initial RGB color gamut according to the color of the object to be detected. The selected threshold range should include the color features of the object as much as possible. For example, if the object to be detected is red, then the R threshold in the initial threshold should be set to a higher range so that the threshold can extract most of the red area.
[0008] S2: Use a camera to capture an image of the object under test in the environment at a certain moment and perform image preprocessing;
[0009] S3: Calculate the color gradient of the image after preprocessing in step S2, extract the object to be tested based on the threshold range under the initial RGB color gamut, and update the HSV threshold range based on the image of the extracted object to be tested.
[0010] S4: The camera acquires new images, performs image preprocessing, and converts them to HSV format;
[0011] S5: Extract the object to be tested from the newly acquired image using the updated HSV threshold, and perform erosion and dilation to remove noise;
[0012] S6: Perform morphological detection on the extracted image as required to complete object recognition.
[0013] Preferably, the image preprocessing in steps S2 and S4 involves Gaussian downsampling of the image to reduce its size. The specific implementation process is as follows:
[0014] S2.1: Apply a Gaussian template to the current image for convolution and then apply a Gaussian blur.
[0015] Gaussian blur is the process of convolving the original data with a Gaussian template, resulting in a slightly blurred image compared to the original image.
[0016] The two-dimensional Gaussian function is as follows:
[0017]
[0018] Where (x,y) are coordinates, and σ is the standard deviation (the square of the standard deviation equals the variance);
[0019] Different filter sizes yield different results. Below is the formula for calculating a (2k+1)x(2k+1) filter:
[0020]
[0021] Where i and j correspond to the row and column in the Gaussian convolution kernel;
[0022] The standard deviation is generally taken as σ = 0.3 × (2ksize + 1), where ksize is the size of the window.
[0023] S2.2: Delete the even-numbered rows and columns of the current layer to obtain the image of the previous layer. The size of the image of the previous layer is only 1 / 4 of that of the image of the next layer, which greatly reduces the computational burden of subsequent operations.
[0024] Preferably, the specific implementation process of step S3 is as follows:
[0025] S3.1: Solve for the color gradient of the entire image;
[0026] S3.2: Find the edges of all objects in the image based on the calculated gradient magnitude;
[0027] S3.3: Crop the image into multiple parts based on the edges of the solved objects;
[0028] S3.4: Perform morphological detection on each cut-out part as needed, and extract the parts that match the object to be detected;
[0029] S3.5: Calculate the proportion of points within the threshold range of the initial RGB color gamut in each cropped part. The part with the highest proportion is the part of the object to be detected that is finally extracted.
[0030] S3.6: Based on the part of the object to be detected finally extracted in step S3.5, obtain the final object image, then convert it to HSV format, and calculate the new HSV threshold range.
[0031] Preferably, the process of solving the color gradient of the color image in step S3.1 is as follows:
[0032] The derivative represents the rate of change of a function, while the gradient is a vector with magnitude and direction, pointing in the direction of the fastest change in the function value. If we consider an image as a two-dimensional function f(x,y), then the gradients of the image in the x and y directions are denoted as G, respectively. x G y f(x,y) is discrete, and the gradient is calculated as the difference between two consecutive points, as follows:
[0033] G x = f(x+1,y)-f(x,y);
[0034] G y = f(x,y+1)-f(x,y);
[0035] Three matrices, r, g, and b, are used to represent the RGB values of the three channels of the image.
[0036] r = img[:,:,2];
[0037] g = img[:,:,1];
[0038] b = img[:,:,0];
[0039] Use G xr G xg G xb G represents the gradient of the three channels in the x-direction. yr G yg G yb These represent the gradients of the three channels in the y-direction;
[0040] Then the gradient in the x-direction of the image is G. xx =G xr 2 +G xg 2 +G xb 2 ;
[0041] The gradient in the y-direction is G. yy =G yr 2 +G yg 2 +G yb 2 ;
[0042] The gradient in the x and y directions is G. xy =G xr *G yr +G xg *G yg +G xb *G yb ;
[0043] M = [G xx (i,j),G xy (i,j); G xy (i,j),G yy (i,j)]
[0044] M is a 2×2 matrix, and the elements of the matrix are all quantities related to the gradient, where i,j are the coordinates of the point to be determined;
[0045] The gradient is then represented by the largest eigenvalue in M, as follows:
[0046] grad(i,j) = max(eign(M));
[0047] After obtaining the gradient magnitude and direction, the image is fully scanned to remove all unwanted pixels that may not constitute an edge. To do this, at each pixel, it is checked whether the pixel is a local maximum near the gradient direction. That is, the gradient value of each pixel is compared with the two adjacent gradient values in that direction. If it is the maximum value, it is kept; otherwise, it is set to zero.
[0048] Then, a dual threshold detection is performed. Appropriate high and low thresholds are selected based on the image. Usually, the high threshold is 2 to 3 times the low threshold. If the gradient value of a pixel is higher than the high threshold, it is retained; if the gradient value of a pixel is lower than the low threshold, it is discarded. If the gradient value of a pixel is between the high and low thresholds, the pixel gradient value is searched from the 8-neighborhood of that pixel. If there is a pixel gradient value higher than the high threshold, it is retained; otherwise, it is discarded.
[0049] This completes the process of calculating the color gradient and extracting the object's edge.
[0050] Preferably, simply using RGB thresholding to detect images in low-light conditions is not very practical. Images can also be represented using HSV format, i.e., H (hue), S (saturation), and V (brightness). In HSV, V is most affected by lighting, while H is basically unaffected by shadows or excessive brightness. Therefore, a method of discarding V channel information to detect colored objects can be used, which is more reliable than using RGB format.
[0051] In step S3.6, the process of calculating the new HSV threshold is as follows:
[0052] Find the ranges of the H and S channels of points in the extracted final object image to derive a new HSV threshold range.
[0053] Preferably, the morphological detection method is related to the object to be detected; different objects require different morphological detection methods. Below is one method for morphological detection:
[0054] 1. First, use the Findcontours() function to extract the outline from the already found image edges (this is a commonly used algorithm, from the 1986 paper: Topological structural analysis of digitized binary images by border following);
[0055] 2. Then, use the Douglas-Pecker algorithm to smooth the curve and reduce redundant data.
[0056] 3. Determine the object to be detected based on the optimized shape and size of the outline;
[0057] For example, to detect a barrel cone, after extracting the outer contour, it is only necessary to determine that the width-to-height ratio of the outer contour is less than 1 and the upper part does not exceed the position of the lower part to determine that it is a barrel cone, that is, the outer contour shape is like a triangle or trapezoid.
[0058] For any details not covered in this invention, please refer to the prior art.
[0059] The beneficial effects of this invention are as follows:
[0060] 1. This invention designs a method for detecting monochrome objects, which solves the problem that changes in object color caused by ambient lighting and other factors result in poor detection performance when using a fixed threshold. Under the same environment, this invention only needs to update the threshold for one image, resulting in low computational pressure and good real-time performance.
[0061] 2. This method provides a way to automatically adjust the color threshold according to the environment, eliminating the tedious operation of manually setting the threshold after changes in ambient light.
[0062] 3. The method for identifying monochrome objects in images based on gradient solving and adjusting threshold proposed in this invention does not involve algorithms such as neural networks that require large amounts of computation or high training costs. The computational pressure is only slightly higher when updating the threshold, while the computational pressure is low in subsequent detection. It has good real-time performance and can be deployed on devices with low computing power. Attached Figure Description
[0063] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments of this application and their descriptions are used to explain this application and do not constitute an undue limitation of this application.
[0064] Figure 1 This is a flowchart of the method for identifying monochrome objects in an image based on gradient solving and adjusting the threshold, according to the present invention.
[0065] Figure 2 A flowchart for updating the HSV threshold range. Detailed implementation method:
[0066] To enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. However, this is not the only description; all aspects not described in detail herein are based on conventional techniques in the art.
[0067] Example 1
[0068] A method for identifying monochrome objects in an image based on gradient-based threshold adjustment, such as... Figure 1 As shown, it includes the following steps:
[0069] S1: Set the threshold range in the initial RGB color gamut according to the color of the object to be detected. The selected threshold range should include the color features of the object as much as possible. For example, if the object to be detected is red, then the R threshold in the initial threshold should be set to a higher range so that the threshold can extract most of the red area.
[0070] S2: Use a camera to capture an image of the object under test in the environment at a certain moment and perform image preprocessing;
[0071] S3: Calculate the color gradient of the image after preprocessing in step S2, extract the object to be tested based on the threshold range under the initial RGB color gamut, and update the HSV threshold range based on the image of the extracted object to be tested.
[0072] S4: The camera acquires new images, performs image preprocessing, and converts them to HSV format;
[0073] S5: Extract the object to be tested from the newly acquired image using the updated HSV threshold, and perform erosion and dilation to remove noise;
[0074] S6: Perform morphological detection on the extracted image as required to complete object recognition.
[0075] Example 2
[0076] A method for identifying monochromatic objects in an image based on gradient-based threshold adjustment, as described in Example 1, differs in that the image preprocessing in steps S2 and S4 involves Gaussian downsampling to reduce the image size. The specific implementation process is as follows:
[0077] S2.1: Apply a Gaussian template to the current image for convolution and then apply a Gaussian blur.
[0078] Gaussian blur is the process of convolving the original data with a Gaussian template, resulting in a slightly blurred image compared to the original image.
[0079] The two-dimensional Gaussian function is as follows:
[0080]
[0081] Where (x,y) are coordinates, and σ is the standard deviation (the square of the standard deviation equals the variance);
[0082] Different filter sizes yield different results. Below is the formula for calculating a (2k+1)x(2k+1) filter:
[0083]
[0084] Where i and j correspond to the row and column in the Gaussian convolution kernel;
[0085] The standard deviation is generally taken as σ = 0.3 × (2ksize + 1), where ksize is the size of the window.
[0086] S2.2: Delete the even-numbered rows and columns of the current layer to obtain the image of the previous layer. The size of the image of the previous layer is only 1 / 4 of that of the image of the next layer, which greatly reduces the computational burden of subsequent operations.
[0087] Example 3
[0088] A method for identifying monochrome objects in an image based on gradient-based threshold adjustment, as described in Example 2, is as follows. Figure 2 The difference lies in the specific implementation process of step S3:
[0089] S3.1: Solve for the color gradient of the entire image;
[0090] S3.2: Find the edges of all objects in the image based on the calculated gradient magnitude;
[0091] S3.3: Crop the image into multiple parts based on the edges of the solved objects;
[0092] S3.4: Perform morphological detection on each cut-out part as needed, and extract the parts that match the object to be detected;
[0093] S3.5: Calculate the proportion of points within the threshold range of the initial RGB color gamut in each cropped part. The part with the highest proportion is the part of the object to be detected that is finally extracted.
[0094] S3.6: Based on the part of the object to be detected finally extracted in step S3.5, obtain the final object image, then convert it to HSV format, and calculate the new HSV threshold range.
[0095] Example 4
[0096] A method for identifying monochrome objects in an image based on an adjustable threshold using gradient solving, as described in Example 3, differs in that the process of solving the color gradient of the color image in step S3.1 is as follows:
[0097] The derivative represents the rate of change of a function, while the gradient is a vector with magnitude and direction, pointing in the direction of the fastest change in the function value. If we consider an image as a two-dimensional function f(x,y), then the gradients of the image in the x and y directions are denoted as G, respectively. x G y f(x,y) is discrete, and the gradient is calculated as the difference between two consecutive points, as follows:
[0098] G x = f(x+1,y)-f(x,y);
[0099] G y = f(x,y+1)-f(x,y);
[0100] Three matrices, r, g, and b, are used to represent the RGB values of the three channels of the image.
[0101] r = img[:,:,2];
[0102] g = img[:,:,1];
[0103] b = img[:,:,0];
[0104] Use G xr G xg Gxb G represents the gradient of the three channels in the x-direction. yr G yg G yb These represent the gradients of the three channels in the y-direction;
[0105] Then the gradient in the x-direction of the image is G. xx =G xr 2 +G xg 2 +G xb 2 ;
[0106] The gradient in the y-direction is G. yy =G yr 2 +G yg 2 +G yb 2 ;
[0107] The gradient in the x and y directions is G. xy =G xr *G yr +G xg *G yg +G xb *G yb ;
[0108] M = [G xx (i,j),G xy (i,j); G xy (i,j),G yy (i,j)]
[0109] M is a 2×2 matrix, and the elements of the matrix are all quantities related to the gradient, where i,j are the coordinates of the point to be determined;
[0110] The gradient is then represented by the largest eigenvalue in M, as follows:
[0111] grad(i,j) = max(eign(M));
[0112] After obtaining the gradient magnitude and direction, the image is fully scanned to remove all unwanted pixels that may not constitute an edge. To do this, at each pixel, it is checked whether the pixel is a local maximum near the gradient direction. That is, the gradient value of each pixel is compared with the two adjacent gradient values in that direction. If it is the maximum value, it is kept; otherwise, it is set to zero.
[0113] Then, a dual threshold detection is performed. Appropriate high and low thresholds are selected based on the image. Usually, the high threshold is 2 to 3 times the low threshold. If the gradient value of a pixel is higher than the high threshold, it is retained; if the gradient value of a pixel is lower than the low threshold, it is discarded. If the gradient value of a pixel is between the high and low thresholds, the pixel gradient value is searched from the 8-neighborhood of that pixel. If there is a pixel gradient value higher than the high threshold, it is retained; otherwise, it is discarded.
[0114] This completes the process of calculating the color gradient and extracting the object's edge.
[0115] Example 5
[0116] A method for identifying monochrome objects in an image based on gradient-based threshold adjustment, as described in Example 4, differs in that, in step S3.6, the process of calculating the new HSV threshold is as follows:
[0117] Find the ranges of the H and S channels of points in the extracted final object image to derive a new HSV threshold range.
[0118] Example 6
[0119] A method for identifying monochromatic objects in an image based on gradient-based threshold adjustment, as described in Example 4, differs in that the morphological detection method is related to the object to be detected; different objects require different morphological detection methods. A method for morphological detection is given below:
[0120] 1. First, use the Findcontours() function to extract the outline from the already found image edges (this is a commonly used algorithm, from the 1986 paper: Topological structural analysis of digitized binary images by border following);
[0121] 2. Then, use the Douglas-Pecker algorithm to smooth the curve and reduce redundant data.
[0122] 3. Determine the object to be detected based on the optimized shape and size of the outline;
[0123] For example, to detect a barrel cone, after extracting the outer contour, it is only necessary to determine that the width-to-height ratio of the outer contour is less than 1 and the upper part does not exceed the position of the lower part to determine that it is a barrel cone, that is, the outer contour shape is like a triangle or trapezoid.
[0124] The above description represents the preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the 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 identifying monochrome objects in an image based on gradient-based threshold adjustment, characterized in that, Includes the following steps: S1: Set the threshold range under the initial RGB color gamut according to the color of the object to be detected; S2: Use a camera to capture an image of the object under test in the environment at a certain moment and perform image preprocessing; S3: Calculate the color gradient of the image after preprocessing in step S2, extract the object to be tested based on the threshold range under the initial RGB color gamut, and update the HSV threshold range based on the image of the extracted object to be tested. S4: The camera acquires new images, performs image preprocessing, and converts them to HSV format; S5: Extract the object to be tested from the newly acquired image using the updated HSV threshold, and perform erosion and dilation to remove noise; S6: Perform morphological detection on the extracted image to complete object recognition; The specific implementation process of step S3 is as follows: S3.1: Solve for the color gradient of the entire image; S3.2: Find the edges of all objects in the image based on the calculated gradient magnitude; S3.3: Crop the image into multiple parts based on the edges of the solved objects; S3.4: Perform morphological detection on each cut-out part and extract the parts that match the object to be detected; S3.5: Calculate the proportion of points within the threshold range of the initial RGB color gamut in each cropped part. The part with the highest proportion is the part of the object to be detected that is finally extracted. S3.6: Based on the part of the object to be detected finally extracted in step S3.5, obtain the final object image, then convert it to HSV format, and calculate the new HSV threshold range.
2. The method for identifying monochrome objects in an image based on gradient-based threshold adjustment according to claim 1, characterized in that, The image preprocessing in steps S2 and S4 involves Gaussian downsampling of the image, and the specific implementation process is as follows: S2.1: Apply a Gaussian template to the current image for convolution and then apply a Gaussian blur. S2.2: Delete the even-numbered rows and columns of the current layer to obtain the image of the previous layer. The size of the image of the previous layer is only 1 / 4 of that of the image of the next layer, which greatly reduces the computational burden of subsequent operations.
3. The method for identifying monochrome objects in an image based on gradient-based threshold adjustment according to claim 2, characterized in that, The process of solving the color gradient in step S3.1 is as follows: The derivative represents the rate of change of a function, while the gradient is a vector with magnitude and direction, pointing in the direction of the fastest change in the function value. If we consider an image as a two-dimensional function f(x,y), then the gradients of the image in the x and y directions are denoted as G, respectively. x G y f(x,y) is discrete, and the gradient is calculated as the difference between two consecutive points, as follows: G x =f(x+1,y)-f(x,y); G y =f(x,y+1)-f(x,y); Three matrices, r, g, and b, are used to represent the RGB values of the three channels of the image. r=img[ :, :, 2]; g = img[ :, :, 1]; b = img[ :, :, 0]; Use G xr G xg G xb G represents the gradient of the three channels in the x-direction. yr G yg G yb These represent the gradients of the three channels in the y-direction; Then the gradient in the x-direction of the image is G. xx =G xr 2 +G xg 2 +G xb 2 ; The gradient in the y-direction is G. yy =G yr 2 +G yg 2 +G yb 2 ; The gradient in the x and y directions is G. xy =G xr *G yr +G xg *G yg +G xb *G yb ; M=[G xx (i,j),G xy (i,j);G xy (i,j),G yy (i,j)] M is a 2×2 matrix, and the elements of the matrix are all quantities related to the gradient, where i and j are the coordinates of the point to be determined; The gradient is then represented by the largest eigenvalue in M, as follows: grad(i,j)=max(eign(M)); After obtaining the gradient magnitude and direction, at each pixel, check whether the pixel is a local maximum value near the gradient direction. That is, compare the gradient value of each pixel with the two adjacent gradient values in that direction. If it is the maximum value, keep it; otherwise, set it to zero. Then, a dual threshold detection is performed. Appropriate high and low thresholds are selected based on the image. If the gradient value of a pixel is higher than the high threshold, it is retained; if the gradient value of a pixel is lower than the low threshold, it is discarded. If the gradient value of a pixel is between the high and low thresholds, the pixel gradient value is searched from the 8-neighborhood of that pixel. If there is a pixel gradient value higher than the high threshold, it is retained; otherwise, it is discarded. This completes the process of calculating the color gradient and extracting the object's edge.
4. The method for identifying monochrome objects in an image based on gradient-based threshold adjustment according to claim 3, characterized in that, In step S3.6, the process of calculating the new HSV threshold is as follows: Find the ranges of the H and S channels of points in the extracted final object image to derive a new HSV threshold range.
Citation Information
Patent Citations
Road warning mark detection and recognition method based on block recognition
CN105809138A
Safety helmet detection method based on color segmentation
CN112488031A