Methods, equipment and storage media for detecting blemishes on camera lenses
By performing grayscale conversion and binarization on images captured by the camera lens, analyzing the enclosing relationship of closed contours, identifying and determining blemishes, the problem of unclear images caused by camera lens contamination is solved, and efficient blemish detection is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HANGZHOU HUACHENG SOFTWARE TECH CO LTD
- Filing Date
- 2026-01-27
- Publication Date
- 2026-06-02
Smart Images

Figure CN122134625A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of image detection technology, and in particular to a method, device and storage medium for detecting blemishes on a camera lens. Background Technology
[0002] The camera lens is the core component of a camera that receives optical objects and adjusts the image, directly determining the quality of the image. However, during the production process of camera lenses, there may be environmental contamination, negligence of production personnel, etc., and during use, the camera lens may be exposed to harsh environments. All of these reasons can lead to the contamination of the camera lens, directly resulting in unclear images.
[0003] Therefore, it is crucial to effectively identify camera lens dirt to prompt relevant personnel to clean the camera. Summary of the Invention
[0004] To address the aforementioned technical problems, this application provides at least one method, apparatus, and storage medium for detecting stains on camera lenses.
[0005] The first aspect of this application provides a method for detecting blemishes on a camera lens. The method includes: acquiring an image captured by a camera to be tested, and converting the image into a grayscale image; acquiring multiple grayscale thresholds; converting the grayscale image into a binary image based on each grayscale threshold to obtain multiple binary images; extracting the closed contours contained in each binary image to obtain the closed contours corresponding to each binary image; analyzing the enclosing relationship between the closed contours corresponding to each binary image, and determining the contours belonging to blemishes based on the closed contours with enclosing relationships to obtain the blemish detection result of the camera to be tested.
[0006] In one embodiment, obtaining multiple grayscale thresholds includes: statistically analyzing the lowest and highest grayscale values in a grayscale image to obtain a grayscale value selection range; and selecting multiple grayscale thresholds from the grayscale value selection range.
[0007] In one embodiment, selecting multiple grayscale thresholds from a grayscale value selection range includes: obtaining a preset threshold modification step size, and selecting a starting threshold from the grayscale value selection range; gradually increasing and / or gradually decreasing the starting threshold based on the threshold modification step size to obtain multiple grayscale thresholds, wherein the grayscale thresholds are within the numerical range of the lowest and highest grayscale values.
[0008] In one embodiment, the process of determining the contours belonging to blemishes based on the closed contours with enclosing relationships to obtain the blemish detection results of the camera to be inspected includes: counting the number of enclosing contours corresponding to each closed contour based on the enclosing relationship between the closed contours; filtering out closed contours with an enclosing contour number greater than a preset threshold to obtain candidate contours; and determining the contours belonging to blemishes based on the candidate contours to obtain the blemish detection results of the camera to be inspected.
[0009] In one embodiment, the method further includes: adding the closed contours extracted from the current binary image to the initial extracted contour set; analyzing the enclosing relationship between the closed contours corresponding to each binary image, including: obtaining the closed contours contained in the enclosing judgment contour set, and analyzing the enclosing relationship between the closed contours in the initial extracted contour set and the closed contours in the enclosing judgment contour set; if there is no enclosing relationship between the closed contours in the initial extracted contour set and all closed contours in the enclosing judgment contour set, then directly adding the closed contours in the initial extracted contour set to the enclosing judgment contour set; if there is an enclosing relationship between the closed contours in the initial extracted contour set and the closed contours in the enclosing judgment contour set, then replacing the closed contours in the enclosing judgment contour set with the closed contours in the initial extracted contour set, and incrementing the number of enclosing contours corresponding to the replaced closed contours by one; filtering out closed contours whose number of enclosing contours is greater than a preset number threshold to obtain candidate contours, including: filtering out closed contours in the enclosing judgment contour set whose number of enclosing contours is greater than a preset number threshold to obtain candidate contours.
[0010] In one embodiment, adding the closed contour extracted from the current binary image to the initial extracted contour set includes: calculating the area of the enclosed region of the closed contour extracted from the current binary image to obtain a first area; detecting whether the first area is less than a preset maximum area threshold, and if so, adding the closed contour to the initial extracted contour set; and / or, filtering closed contours in the enclosed judgment contour set whose number of enclosed contours is greater than a preset number threshold to obtain candidate contours, including: filtering closed contours in the enclosed judgment contour set whose number of enclosed contours is greater than a preset number threshold, obtaining the smallest closed contour contained in the filtered closed contours; obtaining the area of the enclosed region of the filtered closed contours to obtain a second area; detecting whether the second area is greater than a preset minimum area threshold, and if so, using the closed contour as a candidate contour.
[0011] In one embodiment, determining the contour belonging to the blemish based on the candidate contour to obtain the blemish detection result of the camera to be detected includes: acquiring the gray value information of the enclosing region of the candidate contour; verifying whether the candidate contour belongs to the blemish based on the gray value information to obtain the blemish detection result of the camera to be detected.
[0012] In one embodiment, obtaining the grayscale value information of the enclosing region of the candidate contour includes: calculating the average grayscale value of the enclosing region of the candidate contour to obtain a first grayscale value; verifying whether the candidate contour is a contour belonging to a blemish based on the grayscale value information to obtain the blemish detection result of the camera to be detected includes: expanding the enclosing region of the candidate contour outward to obtain an extended region, calculating the average grayscale value of the extended region to obtain a second grayscale value; calculating the difference between the first grayscale value and the second grayscale value to obtain a grayscale value difference; if the grayscale value difference is higher than a preset difference threshold, then the candidate contour is regarded as a contour belonging to a blemish, and the blemish detection result of the camera to be detected is obtained.
[0013] A second aspect of this application provides a blemish detection device for a camera lens. The device includes: an image preprocessing module for acquiring an image captured by a camera to be inspected, converting the image into a grayscale image, and acquiring multiple grayscale thresholds; a binarization module for converting the grayscale image into a binary image based on each grayscale threshold, thereby obtaining multiple binary images; a contour extraction module for extracting closed contours contained in each binary image, thereby obtaining closed contours corresponding to each binary image; and a blemish detection module for analyzing the enclosing relationship between the closed contours corresponding to each binary image, determining the contours belonging to blemishes based on the closed contours with enclosing relationships, and obtaining the blemish detection result of the camera to be inspected.
[0014] A third aspect of this application provides an electronic device, including a memory and a processor, wherein the processor is configured to execute program instructions stored in the memory to implement the above-described method for detecting blemishes on a camera lens.
[0015] The fourth aspect of this application provides a computer-readable storage medium having program instructions stored thereon, which, when executed by a processor, implement the above-described method for detecting blemishes on a camera lens.
[0016] The above scheme acquires an image captured by the camera to be detected, converts the image into a grayscale image, and obtains multiple grayscale thresholds. Based on each grayscale threshold, the grayscale image is converted into a binary image, resulting in multiple binary images. The closed contours contained in each binary image are extracted, resulting in closed contours corresponding to each binary image. The enclosing relationship between the closed contours corresponding to each binary image is analyzed, and the contours belonging to blemishes are determined based on the closed contours with enclosing relationships, thus obtaining the blemish detection result of the camera to be detected. Multiple binarization and contour extraction ensure the accuracy of blemish region recognition. The enclosing relationship is used to distinguish blemishes from other similar structures in the image, reducing misjudgments caused by the complexity of image content, and simplifying the judgment conditions for blemishes. It can adapt to the recognition of blemishes of different shapes and sizes.
[0017] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this application. Attached Figure Description
[0018] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the specification, serve to explain the technical solutions of this application.
[0019] Figure 1 This is a schematic diagram illustrating the implementation environment of the solution in an exemplary embodiment of this application;
[0020] Figure 2 This is a flowchart illustrating a method for detecting blemishes on a camera lens, as shown in an exemplary embodiment of this application. Figure 3 This is a schematic diagram illustrating the contour enclosure relationship of an exemplary embodiment of this application; Figure 4 This is a schematic diagram illustrating the selection of candidate contours in an exemplary embodiment of this application; Figure 5 This is a block diagram illustrating a stain detection device for a camera lens, as shown in an exemplary embodiment of this application. Figure 6 This is a schematic diagram of the structure of an electronic device shown in an exemplary embodiment of this application; Figure 7 This is a schematic diagram illustrating the structure of a computer-readable storage medium, as shown in an exemplary embodiment of this application. Detailed Implementation
[0021] The embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0022] In the following description, specific details such as particular system architectures, interfaces, and technologies are presented for illustrative purposes rather than for limiting purposes, in order to provide a thorough understanding of this application.
[0023] In this document, the term "and / or" is merely a description of the association information of related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following related objects have an "or" relationship. Furthermore, "many" in this document means two or more. Moreover, the term "at least one" in this document means any combination of at least two of any one or more of a plurality of elements. For example, including at least one of A, B, and C can mean including any one or more elements selected from the set consisting of A, B, and C.
[0024] The method for detecting blemishes on camera lenses provided in the embodiments of this application will be described below.
[0025] Please refer to Figure 1 , Figure 1This is a schematic diagram illustrating an implementation environment of the scheme according to an exemplary embodiment of this application. The implementation environment may include a terminal 110 and a server 120, and the terminal 110 and the server 120 are interconnected.
[0026] Terminal 110 can be any electronic device equipped with a camera lens, such as a camera, smartphone, tablet, drone, dashcam, or robot vacuum cleaner. This application does not limit the specific type of terminal 110.
[0027] Server 120 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms.
[0028] In one example, server 120 can perform blemish detection processing on an image obtained from terminal 110 to obtain blemish detection results. Server 120 can store the blemish detection results locally, send them back to terminal 110, or transmit them to other terminals.
[0029] In one example, a client application running the target application is installed on terminal 110. This target application may be an application that provides blemish detection functionality. Based on this target application, blemish detection processing is performed on the acquired image to obtain blemish detection results. Server 120 may be a backend server for the target application, used to provide backend services to the client of the target application.
[0030] The camera lens stain detection method provided in this application embodiment can be executed by a terminal 110, such as a client of the target application installed and running on the terminal 110, or by a server 120, or by the interaction and cooperation between the terminal 110 and the server 120. That is, some steps of the method are executed by the terminal 110 and other steps are executed by the server 120. This application does not limit the specific type of the executing entity.
[0031] Please see Figure 2 , Figure 2 This is a flowchart illustrating a stain detection method for a camera lens, as shown in an exemplary embodiment of this application. Figure 2 As shown, the method for detecting blemishes on a camera lens includes at least steps S210 to S240, which are described in detail below: Step S210: Acquire an image captured by the camera to be detected, convert the image into a grayscale image; and acquire multiple grayscale thresholds.
[0032] The image used for blemish detection can be a picture taken by the camera under test, or a video frame from a video captured by the camera under test. Furthermore, the image used for blemish detection can be taken from a preset scene or from a random scene.
[0033] For example, the camera under test is controlled to be aimed at a solid-color, uncontaminated background board and then taken a picture to obtain an image for spot detection.
[0034] Then, convert the image to grayscale.
[0035] An image represented by grayscale is called a grayscale image. The grayscale level is the number of levels between white and black based on a logarithmic relationship.
[0036] For example, each pixel in an image includes pixel quality corresponding to multiple channels, such as three channels R, G, and B, or three channels Y, U, and V. A simple averaging method or a weighted averaging method can be used to calculate the grayscale image for each channel. Alternatively, a single channel can be selected and its contrast information can be retained to obtain a grayscale image. Or, for each pixel, the median value between the maximum and minimum values of each channel can be selected to obtain a grayscale image.
[0037] The specific implementation method for converting grayscale images can be flexibly selected according to the actual application scenario, and this application does not limit it.
[0038] In addition, multiple grayscale thresholds are obtained.
[0039] The grayscale threshold can be preset based on experience or calculated flexibly based on image information; this application does not limit this.
[0040] Step S220: Convert the grayscale image into a binary image based on each grayscale threshold to obtain multiple binary images.
[0041] Binarization is achieved by setting a grayscale threshold to divide the pixel values of a grayscale image into two categories (usually 0 and 255), thus obtaining a black and white image.
[0042] The grayscale image is transformed multiple times according to each grayscale threshold, resulting in a binary image corresponding to each grayscale threshold. That is, each grayscale threshold is transformed into a binary image, and finally multiple binary images are obtained.
[0043] Step S230: Extract the closed contours contained in each binary image to obtain the closed contours corresponding to each binary image.
[0044] A closed contour refers to a continuous closed curve formed by the boundaries of objects in an image.
[0045] For example, if the grid cell containing the outline is black and the grid cell outside the outline is white, the extracted outline and its enclosed area should be considered as a closed outline.
[0046] Step S240: Analyze the enclosing relationship between the closed contours corresponding to each binary image, determine the contours belonging to the blemishes based on the closed contours with enclosing relationships, and obtain the blemish detection results of the camera to be detected.
[0047] If one closed contour is inside another closed contour, it indicates that there is an enclosing relationship between the two closed contours.
[0048] For example, please see Figure 3 , Figure 3 This is a schematic diagram illustrating the contour enclosure relationship of an exemplary embodiment of this application, as shown below. Figure 3 As shown, binary image P1 extracts closed contour c1, and binary image P2 extracts closed contour c2. Closed contour c1 is inside closed contour c2, so there is an enclosure relationship between closed contour c1 and closed contour c2, specifically, closed contour c2 encloses closed contour c1. Of course, if closed contour c2 is enclosed within closed contour c1, then there is also an enclosure relationship between closed contour c1 and closed contour c2.
[0049] Then, based on the closed contours that have an enclosing relationship, the contours belonging to the stains are determined.
[0050] For example, a closed contour that has an enclosing relationship can be directly regarded as the contour belonging to the stain.
[0051] For example, count the number of other closed contours contained within a closed contour, and identify closed contours whose number exceeds a preset threshold as contours belonging to blemishes.
[0052] For example, screening closed contours that are mutually enclosing can be used to determine the contours that belong to blemishes based on the size relationship between the areas of the enclosing regions corresponding to each of the screened closed contours, and / or the distance between the centers of the corresponding closed contours, and / or the width of the loops formed between the screened closed contours, and / or the curve variation information of the screened closed contours.
[0053] For example, the enclosure relationship between closed contours c1 and c2 is detected by checking whether the difference in the area of the enclosed regions between closed contours c1 and c2 is within a preset area threshold range, and / or whether the center distance between closed contours c1 and c2 is within a preset distance threshold range, and / or whether the average width of the loop formed between closed contours c1 and c2 is within a preset width threshold range, and / or whether the curve similarity between closed contours c1 and c2 is greater than a preset similarity threshold. If so, closed contours c1 and c2 are determined to be contours of a blemish. The preset area threshold range, and / or preset distance threshold range, and / or preset width threshold range, and / or preset similarity threshold can be determined based on the grayscale threshold corresponding to each closed contour obtained by filtering. For example, if the difference in grayscale thresholds between closed contour c1 and closed contour c2 is large, a larger threshold range and / or a smaller preset similarity threshold is set. Conversely, a smaller threshold range and / or a larger preset similarity threshold is set. Of course, the above threshold ranges can also be preset based on experience, and this application does not limit this.
[0054] It should be noted that the specific method for determining the outline of a stain can be flexibly determined according to the actual application scenario. For example, based on the types of stains that may appear in the scene where the camera is located (such as oil stains, water stains, mud spots, etc.), corresponding judgment conditions can be set, and closed outlines with enclosing relationships can be judged according to the judgment conditions to determine the outline of a stain. This application does not limit the specific method for determining the outline of a stain.
[0055] Based on the outline of the blemish, the blemish detection result of the camera to be inspected is obtained.
[0056] The results of stain detection include, but are not limited to, parameters such as the location, and / or size, and / or total number, and / or type, and / or average gray value of the stains.
[0057] This application acquires an image captured by a camera to be detected, converts the image into a grayscale image, and acquires multiple grayscale thresholds. Based on each grayscale threshold, the grayscale image is converted into a binary image, resulting in multiple binary images. The closed contours contained in each binary image are extracted, resulting in closed contours corresponding to each binary image. The enclosing relationship between the closed contours corresponding to each binary image is analyzed, and the contours belonging to blemishes are determined based on the closed contours with enclosing relationships, thus obtaining the blemish detection result of the camera to be detected. Multiple binarization and contour extraction ensure the accuracy of blemish region recognition. The enclosing relationship is used to distinguish blemishes from other similar structures in the image, reducing misjudgments caused by the complexity of image content, and simplifying the judgment conditions for blemishes. It can adapt to the recognition of blemishes of different shapes and sizes.
[0058] The following describes some embodiments of this application in detail.
[0059] In some implementations, multiple grayscale thresholds are obtained in step S210, including steps S211 to S212 below.
[0060] Step S211: Calculate the lowest and highest gray values in the grayscale image to obtain the grayscale value selection range.
[0061] For example, the lowest grayscale value of a grayscale image is used This indicates that the highest grayscale value is represented by... This indicates that the range of grayscale values to be selected is [ , ].
[0062] Step S212: Select multiple grayscale thresholds from the grayscale value selection range.
[0063] A value is selected from the range of grayscale values, and the selected value is used as the grayscale threshold.
[0064] For example, the number of grayscale thresholds can be predetermined. This number can be preset based on experience or flexibly set according to the types of dirt that may appear in the scene where the camera is located (such as oil stains, water stains, mud spots, etc.).
[0065] For example, mud spots generally become lighter from the center to the edge, so a larger number of grayscale thresholds can be set. Water stains, on the other hand, have a very obvious, dark ring around the edge, while the central area is relatively clean, light in color, or even transparent, so a smaller number of grayscale thresholds can be set.
[0066] Then, depending on the number of grayscale thresholds to be selected, a corresponding number of grayscale thresholds can be randomly selected, or a corresponding number of grayscale thresholds can be selected according to preset rules. For example, the grayscale value selection range can be evenly divided to obtain a corresponding number of grayscale thresholds, with equal intervals between each grayscale threshold.
[0067] For example, the number of grayscale thresholds can be unlimited, but the threshold modification step size can be limited. Initially, a starting threshold is selected, and then other grayscale thresholds are calculated sequentially based on the threshold modification step size and the starting threshold.
[0068] For example, selecting multiple grayscale thresholds from the grayscale value selection range includes: obtaining a preset threshold modification step size, and selecting a starting threshold from the grayscale value selection range; gradually increasing and / or gradually decreasing the starting threshold based on the threshold modification step size to obtain multiple grayscale thresholds, wherein the grayscale thresholds are within the numerical range of the lowest and highest grayscale values.
[0069] The threshold modification step size can be preset based on experience, or it can be determined based on scene information of the scene where the camera is located.
[0070] For example, based on information such as the scene type of the camera's location, and / or weather, and / or air quality, and / or the pose of the camera to be tested, the degree of lens dirtiness of the camera to be tested can be predicted, and the threshold can be set and the step size modified according to the degree of lens dirtiness.
[0071] For example, the higher the degree of lens dirtiness, the smaller the threshold modification step size can be to more accurately identify more contour information and avoid omissions; the lower the degree of lens dirtiness, the larger the threshold modification step size can be to reduce invalid calculations (there may be multiple different grayscale thresholds that extract completely identical closed contours), thus saving computational resources. Alternatively, multiple dirtiness levels can be defined, with each level having a pre-set threshold modification step size. The threshold modification step size corresponding to the dirtiness level of the lens can be queried. The specific method of setting the threshold modification step size based on the degree of lens dirtiness can be flexibly set according to the actual application scenario, and this application does not limit this.
[0072] The starting threshold can be the minimum, maximum, or median value within the selected grayscale range. The starting threshold can be flexibly set according to the actual application scenario, and this application does not impose any limitations on it. Furthermore, the starting threshold can also be used as a grayscale threshold for image binarization and closed contour extraction.
[0073] For example, the initial threshold is the range of grayscale values selected. , The smallest value in ] Then use the smallest value Starting from the threshold, the values are incremented sequentially according to the threshold adjustment step size. The value obtained from each increment is used as the grayscale threshold until the incremented value is not less than or equal to the threshold value. .
[0074] To illustrate further, the initial threshold is the range of grayscale values selected. , The largest value in ] Then the maximum value Starting from the threshold, the grayscale threshold is continuously reduced step by step, with the value obtained from each reduction serving as the grayscale threshold, until the cumulative reduction result is no greater than or equal to the threshold value. .
[0075] For example, the initial threshold is the range of grayscale values selected. , The middle value in the image is used as the starting point. The values are then incremented and decremented according to the threshold step size. The results of each increment and decrement are used as the grayscale threshold until the decremented value is no greater than or equal to the threshold value. Or the sum of the results is not less than or equal to .
[0076] The grayscale image is binarized multiple times according to each grayscale threshold, resulting in a separate binary image for each threshold. Closed contours are then extracted from each binary image. Next, the bounding relationships between the closed contours corresponding to each binary image are detected to determine the contours belonging to blemishes based on these relationships.
[0077] In some implementations, step S240 determines the contour belonging to the blemish based on the closed contour with an enclosing relationship, and obtains the blemish detection result of the camera to be inspected, including the following steps S241 to S243.
[0078] Step S241: Based on the enclosing relationship between each closed contour, count the number of enclosing contours corresponding to the closed contour.
[0079] Identify other enclosing contours that exist around each enclosing contour, and count the number of enclosing contours.
[0080] The number of enclosing contours refers to the number of closed contours that are mutually enclosing.
[0081] Step S242: Filter out closed contours whose number of surrounding contours is greater than a preset threshold to obtain candidate contours.
[0082] For example, please see Figure 4 , Figure 4 This is a schematic diagram illustrating the selection of candidate contours in an exemplary embodiment of this application, as shown below. Figure 4 As shown, binary image P1 extracts closed contour c1, binary image P2 extracts closed contours c2, c3, and c4, and binary image P3 extracts closed contours c5 and c6. c1, c2, and c5 are mutually enclosing contours, and the number of enclosing contours corresponding to c1 is 3. If the preset number threshold is 2, then the number of enclosing contours corresponding to c1 is greater than the preset number threshold, and c1 is selected as a candidate contour, or c1, c2, and c5 are selected as candidate contours.
[0083] In some implementations, the number of bounding contours can be updated after each extracted binary image to reduce the amount of contour information that needs to be stored during the calculation process. Specifically, the closed contours extracted from the current binary image are added to the initial extracted contour set; the closed contours contained in the bounding judgment contour set are obtained, and the bounding relationship between the closed contours in the initial extracted contour set and the closed contours in the bounding judgment contour set is analyzed; if there is no bounding relationship between the closed contours in the initial extracted contour set and all closed contours in the bounding judgment contour set, then the closed contours in the initial extracted contour set are directly added to the bounding judgment contour set; if there is a bounding relationship between the closed contours in the initial extracted contour set and the closed contours in the bounding judgment contour set, then the closed contours in the bounding judgment contour set are replaced with the closed contours in the initial extracted contour set, and the number of bounding contours corresponding to the replaced closed contours is incremented by one.
[0084] The initial extraction contour set and the bounding judgment contour set are both initially empty sets.
[0085] For ease of explanation, let set A be the set of bounding contours to be judged, and set B be the set of initially extracted contours. The closed contours extracted from the current binary image are added to set B.
[0086] If a closed contour c2 in set B and a closed contour c1 in set A have an enclosing relationship, then the closed contour c1 in set A is replaced with the closed contour c2 in set B, and the number of enclosing contours of the closed contour c2 in set B after the replacement is incremented by one. For example, if the number of enclosing contours of the closed contour c1 in set A before the replacement is 1, then the number of enclosing contours of the closed contour c2 in set A after the replacement is 2.
[0087] If a closed contour c3 in set B has no enclosing relationship with any closed contour in set A, then closed contour c3 in set B is directly added to set A for comparison with the closed contours extracted from the subsequent binary image. It is also possible to initialize the number of enclosing contours of the newly added closed contour c3 in set A, for example, initializing the number of enclosing contours to 1. For ease of calculation, if every closed contour in the current binary image has been determined, the initial extracted contour set is re-initialized to an empty set to execute the new binary image determination steps.
[0088] Then, close contours with a number of enclosing contours greater than a preset threshold are selected from the set of enclosing contours to obtain candidate contours.
[0089] For example, after detecting an update in the number of enclosing contours of a closed contour in set A, it is determined whether the updated number of enclosing contours is greater than a preset threshold. If it is, the closed contour is extracted from set A to obtain a candidate contour. By directly selecting a candidate contour after each update of the enclosing contour set, recognition efficiency can be improved.
[0090] For example, after sorting the grayscale thresholds sequentially according to their numerical values (e.g., from smallest to largest or from largest to smallest), grayscale thresholds are selected sequentially from the sorting results to perform layer-by-layer binarization on the grayscale image. Then, for each resulting binary image, the above steps are performed. If a closed contour is detected in set A and all closed contours in set B have no enclosing relationship, the closed contour is extracted from set A. It is then determined whether the number of enclosing contours of the closed contour is greater than a preset threshold. If it is, it is considered a candidate contour; otherwise, the closed contour is discarded. By performing enclosing relationship judgment and candidate contour judgment after layer-by-layer binarization, the continuity of tainted area recognition is ensured, the judgment conditions for tainted areas are simplified, and it is easier to accurately identify the area where the tainted area is located, effectively filtering out interfering contour information.
[0091] Optionally, based on the previous embodiment, if a closed contour is detected in set A and all closed contours in set B are not bound by any enclosing relationship, the discontinuity count of the closed contour in set A is incremented by one. If the discontinuity count exceeds a preset discontinuity count threshold, it is then determined whether the number of enclosing contours of the closed contour is greater than a preset number threshold. If it is greater, it is used as a candidate contour; otherwise, the closed contour is discarded directly to improve the accuracy of interference contour filtering. The preset discontinuity count threshold can be pre-set based on experience or flexibly set according to the types of dirt (such as oil stains, water stains, mud spots, etc.) that may appear in the scene where the camera is located. This application does not limit this setting.
[0092] In some implementations, the area of the candidate contours can also be limited. For example, closed contours with a number of enclosing contours greater than a preset threshold and an enclosing area that meets a preset numerical condition can be selected as candidate contours.
[0093] For example, adding the closed contours extracted from the current binary image to the initial extracted contour set includes: calculating the area of the enclosed region of the closed contours extracted from the current binary image to obtain a first area; detecting whether the first area is less than a preset maximum area threshold, and if so, adding the closed contour to the initial extracted contour set. And / or, filtering closed contours in the enclosed contour set whose number of enclosed contours is greater than a preset number threshold to obtain candidate contours includes: filtering closed contours in the enclosed contour set whose number of enclosed contours is greater than a preset number threshold, obtaining the smallest closed contour contained in the filtered closed contours; calculating the area of the enclosed region of the smallest closed contour to obtain a second area; detecting whether the second area is greater than a preset minimum area threshold, and if so, using the closed contour as a candidate contour.
[0094] Step S243: Determine the contours belonging to the blemishes based on the candidate contours to obtain the blemish detection results of the camera to be detected.
[0095] For example, a candidate contour can be determined as a blemish contour based on the area of the enclosed region of the candidate contour, and / or the center distance between the contours of the associated candidate contours, and / or the width of the loop formed between the associated candidate contours, and / or the curve similarity between the associated candidate contours.
[0096] In some implementations, determining the contour belonging to the blemish based on the candidate contour to obtain the blemish detection result of the camera to be detected includes: acquiring the gray value information of the enclosing region of the candidate contour; verifying whether the candidate contour belongs to the blemish based on the gray value information to obtain the blemish detection result of the camera to be detected.
[0097] The grayscale value information may be the average grayscale value, and / or the maximum grayscale value, and / or the minimum grayscale value, and / or the grayscale value distribution within the bounding area of the candidate contour, etc., which are not limited in this application.
[0098] Verify whether the candidate contour belongs to a taint based on the grayscale value information.
[0099] For example, by statistically analyzing the grayscale images of sample blemishes, template parameters such as the average grayscale value range, and / or the maximum grayscale value range, and / or the minimum grayscale value range, and / or the grayscale value distribution corresponding to the blemishes can be obtained. It can then be determined whether the average grayscale value, and / or the maximum grayscale value, and / or the minimum grayscale value, and / or the grayscale value distribution of the candidate contours conform to the template parameters. If they do, the candidate contour is determined to be the contour of the blemish.
[0100] For example, obtaining the grayscale value information of the bounding region of the candidate contour includes: calculating the average grayscale value of the bounding region of the candidate contour to obtain a first grayscale value. Verifying whether the candidate contour belongs to a taint based on the grayscale value information to obtain the taint detection result of the camera to be detected includes: expanding the bounding region of the candidate contour outward to obtain an expanded region; calculating the average grayscale value of the expanded region to obtain a second grayscale value; calculating the difference between the first grayscale value and the second grayscale value to obtain a grayscale value difference; if the grayscale value difference is higher than a preset difference threshold, then the candidate contour is considered a taint contour, and the taint detection result of the camera to be detected is obtained.
[0101] Since there are usually multiple candidate contours, the average gray value can be calculated by randomly selecting the enclosing region corresponding to a candidate contour or by selecting the enclosing region corresponding to the smallest candidate contour. This application does not limit this.
[0102] By comparing average gray values, the accuracy of identifying inconspicuous blemishes can be effectively improved, and interference from non-blemish contour information can be avoided, thereby improving the accuracy of blemish detection.
[0103] Taking a specific scenario as an example, the process of selecting candidate contours will be illustrated in detail: Step S301: Convert the acquired image into a grayscale image, the grayscale range of the grayscale image is [ , The threshold modification step size is preset to s, let... And initialize empty sets A, B, C, and D.
[0104] Step S302: Using g as the grayscale threshold, convert the grayscale image into a binary image.
[0105] For example, when the pixel grayscale value of a grayscale image is greater than g, it must be converted into a white raster in a binary image; otherwise, it must be converted into a black raster.
[0106] Step S303: Extract closed contours from the binary image, and select regions whose enclosing area is less than a preset maximum area threshold. The closed outline is placed into set B.
[0107] For example, the grid cell containing the closed profile must be black, the grid cell outside the closed profile must be white, and the extracted closed profile and the closed profile within its enclosing area should be considered as a single closed profile.
[0108] Step S304: If a closed contour in set B can enclose a closed contour in set A, then the closed contour in set B will cover the closed contour in set A, and the number of enclosing contours of the covered contour will be incremented by 1 based on the number of enclosing contours of the covered contour. If a closed contour in set B cannot enclose any closed contour in set A, then the closed contour in set B will be directly put into set A, and its number of enclosing contours will be initialized to 1.
[0109] Step S305: If a closed contour in set A cannot be enclosed by any closed contour in set B, then extract the closed contour from set A. If the number of enclosing contours of the closed contours extracted from set A is greater than a preset threshold... The area of the enclosed region is greater than the preset minimum area threshold. If so, then the closed contour is placed into set C.
[0110] Step S306: Let g = g + s. If g is less than or equal to... If the number of enclosing contours in set A exceeds a preset threshold, then proceed to step S302; otherwise, proceed to step S302. The area of the enclosed region is greater than the preset minimum area threshold. After the closed contour is placed into set C, step S307 is executed.
[0111] Step S307: Iterate through the closed contours in set C, and for each closed contour, make the following judgment: Calculate the current contour. Enclosed area The average grayscale value is used to obtain the first grayscale value. Then, Extend outward Each pixel obtains an expanded area ( Option to include Alternatively, you can choose not to include it. ),area The average gray value is ,like Then it is believed For the stained area, and Add them to set D. Where, , All are preset values.
[0112] Step S308: If set D is empty, then there are no blemishes on the camera lens. If set D is not empty, then blemish-related information is generated based on the data contained in set D, including but not limited to the number of blemishes, their location, and the average gray value.
[0113] Figure 5This is a block diagram illustrating a stain detection device for a camera lens, as shown in an exemplary embodiment of this application. Figure 5 As shown, the exemplary camera lens stain detection device 500 includes: The image preprocessing module 510 is used to acquire an image captured by the camera to be detected, convert the image into a grayscale image, and acquire multiple grayscale thresholds. Binarization module 520 is used to convert grayscale images into binary images based on each grayscale threshold, resulting in multiple binary images; The contour extraction module 530 is used to extract the closed contours contained in each binary image, so as to obtain the closed contours corresponding to each binary image. The blemish detection module 540 is used to analyze the enclosing relationship between the closed contours corresponding to each binary image, determine the contours belonging to blemishes based on the closed contours with enclosing relationships, and obtain the blemish detection results of the camera to be inspected.
[0114] It should be noted that the camera lens stain detection device and the camera lens stain detection method provided in the above embodiments belong to the same concept. The specific operation methods of each module and unit have been described in detail in the method embodiments and will not be repeated here. In practical applications, the camera lens stain detection device provided in the above embodiments can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. This is not a limitation here.
[0115] Please see Figure 6 , Figure 6 This is a schematic diagram illustrating the structure of an electronic device in an exemplary embodiment of this application. The electronic device 600 includes a memory 610 and a processor 620. The processor 620 executes program instructions stored in the memory 610 to implement the steps in any of the above-described embodiments of the camera lens blemish detection method. In a specific implementation scenario, the electronic device 600 may include, but is not limited to, a microcomputer or a server. Furthermore, the electronic device 600 may also include mobile devices such as laptops and tablets, without limitation.
[0116] Specifically, processor 620 controls itself and memory 610 to implement the steps in any of the above-described camera lens blemish detection method embodiments. Processor 620 may also be referred to as a Central Processing Unit (CPU). Processor 620 may be an integrated circuit chip with signal processing capabilities. Processor 620 may also be a general-purpose processor, digital signal processor (DSP), application-specific integrated circuit (ASIC), field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. A general-purpose processor may be a microprocessor or any conventional processor. Furthermore, processor 620 may be implemented using integrated circuit chips.
[0117] Please see Figure 7 , Figure 7 This is a schematic diagram illustrating the structure of a computer-readable storage medium in an exemplary embodiment of this application. The computer-readable storage medium 700 stores program instructions 710 that can be executed by a processor. The program instructions 710 are used to implement the steps in any of the above-described embodiments of the method for detecting blemishes on a camera lens.
[0118] In some embodiments, the functions or modules of the apparatus provided in this disclosure can be used to perform the methods described in the above method embodiments. The specific implementation can be referred to the description of the above method embodiments, and for the sake of brevity, it will not be repeated here.
[0119] The description of the various embodiments above tends to emphasize the differences between the various embodiments. The similarities or similarities between them can be referred to, and for the sake of brevity, they will not be repeated here.
[0120] In the several embodiments provided in this application, it should be understood that the disclosed methods and apparatus can be implemented in other ways. For example, the apparatus implementations described above are merely illustrative. For instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection of devices or units may be electrical, mechanical, or other forms.
[0121] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit. If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
Claims
1. A method for detecting stains on a camera lens, characterized in that, The method includes: Acquire an image captured by the camera to be detected, convert the image into a grayscale image; and acquire multiple grayscale thresholds; The grayscale image is converted into a binary image based on each grayscale threshold, resulting in multiple binary images. Extract the closed contours contained in each binary image to obtain the closed contours corresponding to each binary image; The bounding relationship between the closed contours corresponding to each binary image is analyzed, and the contours belonging to the blemishes are determined based on the closed contours with bounding relationships, so as to obtain the blemish detection results of the camera to be detected.
2. The method according to claim 1, characterized in that, The acquisition of multiple grayscale thresholds includes: The lowest and highest grayscale values in the grayscale image are statistically analyzed to obtain the range of grayscale values to be selected. Select multiple grayscale thresholds from the grayscale value selection range.
3. The method according to claim 2, characterized in that, The step of selecting multiple grayscale thresholds from the grayscale value selection range includes: Obtain the preset threshold modification step size, and select the starting threshold from the grayscale value selection range; Based on the threshold modification step size, the initial threshold is gradually increased and / or gradually decreased to obtain multiple grayscale thresholds, wherein the grayscale thresholds are within the numerical range of the lowest grayscale value and the highest grayscale value.
4. The method according to claim 1, characterized in that, The process of determining the contours belonging to blemishes based on closed contours with enclosing relationships to obtain the blemish detection results of the camera under test includes: Based on the enclosing relationship between each closed contour, the number of enclosing contours corresponding to the closed contour is counted. Candidate contours are obtained by filtering out closed contours whose number of enclosing contours is greater than a preset threshold. Based on the candidate contours, contours belonging to blemishes are determined, and the blemish detection results of the camera to be detected are obtained.
5. The method according to claim 4, characterized in that, The method further includes: Add the closed contours extracted from the current binary image to the initial set of extracted contours; The analysis of the enclosing relationships between the closed contours corresponding to each binary image includes: Obtain the closed contours contained in the bounding judgment contour set, and analyze the bounding relationship between the closed contours in the initial extracted contour set and the closed contours in the bounding judgment contour set; If the closed contours in the initial extracted contour set do not have an enclosing relationship with any of the closed contours in the enclosing judgment contour set, then the closed contours in the initial extracted contour set are directly added to the enclosing judgment contour set. If the closed contour in the initial extracted contour set has an enclosing relationship with the closed contour in the enclosing judgment contour set, then the closed contour in the enclosing judgment contour set is replaced with the closed contour in the initial extracted contour set, and the number of enclosing contours corresponding to the replaced closed contour is incremented by one. The process of filtering out closed contours with a number of enclosing contours greater than a preset threshold yields candidate contours, including: Candidate contours are obtained by filtering out closed contours in the set of bounding contours where the number of bounding contours is greater than a preset threshold.
6. The method according to claim 5, characterized in that, The step of adding the closed contours extracted from the current binary image to the initial extracted contour set includes: Calculate the area of the region enclosed by the closed contour extracted from the current binary image to obtain the first area; If the first area is less than a preset maximum area threshold, the closed contour is added to the initial extracted contour set. And / or, the step of filtering out closed contours in the set of bounding contours where the number of bounding contours is greater than a preset threshold to obtain candidate contours includes: Filter out closed contours in the set of bounding judgment contours whose number of bounding contours is greater than a preset number threshold, and obtain the smallest closed contour contained in the filtered closed contours. Obtain the area of the region enclosed by the selected closed contours to get the second area; Detect whether the second area is greater than a preset minimum area threshold. If so, the closed contour is selected as a candidate contour.
7. The method according to claim 4, characterized in that, The step of determining the contours belonging to the blemishes based on the candidate contours to obtain the blemish detection results of the camera to be detected includes: Obtain the grayscale value information of the region enclosing the candidate contour; Based on the grayscale information, the candidate contour is verified to determine whether it is a contour belonging to a blemish, and the blemish detection result of the camera to be detected is obtained.
8. The method according to claim 7, characterized in that, The step of obtaining the grayscale value information of the enclosing region of the candidate contour includes: Calculate the average gray value of the region enclosed by the candidate contour to obtain the first gray value; The step of verifying whether the candidate contour is a blemish based on the grayscale information to obtain the blemish detection result of the camera to be detected includes: The bounding region of the candidate contour is expanded outward to obtain an expanded region, and the average gray value of the expanded region is calculated to obtain a second gray value; Calculate the difference between the first gray value and the second gray value to obtain the gray value difference; If the difference in grayscale values is higher than a preset difference threshold, the candidate contour is taken as the contour belonging to the blemish, and the blemish detection result of the camera to be detected is obtained.
9. An electronic device, characterized in that, The electronic device includes a memory and a processor, the processor being configured to execute program instructions stored in the memory to implement the steps of the method as described in any one of claims 1-8.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program instructions that can be executed by a processor to implement the steps of the method as described in any one of claims 1-8.