Autofocus method, apparatus, electronic device, storage medium, and program product
By using an endoscopic autofocus method and controlling the motor step size with the rate of change of sharpness, the dependence on the initial rotation direction judgment is eliminated, achieving fast and reliable focusing. This solves the focusing failure problem caused by incorrect direction judgment in traditional algorithms and enhances the robustness and safety of the system.
Patent Information
- Application Number
- CN202611529128.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-29
- Publication Date
- 2026-11-03
AI Technical Summary
The problem of autofocus failure in endoscopes due to the inability to accurately determine the initial rotation direction of the motor during startup, especially the technical problem of how to eliminate the dependence on the determination of the initial rotation direction, which leads to motor focusing failure.
By providing an autofocus method in the patent, the reliance on the initial rotation direction judgment is eliminated by forcing the motor to return to the preset starting position at maximum speed. The sharpness change rate is used to control the rotation step size of the motor, first and second states are set to avoid exceeding the peak value, and the reliability of focusing is ensured by exiting the condition.
It achieves fast and reliable focusing, eliminates the dependence on the initial rotation direction judgment, saves computing resources, and ensures the accuracy and robustness of focusing in various scenarios.
Smart Images

Figure CN123035376A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of endoscopic imaging technology, and more specifically to autofocus methods, devices, electronic devices, storage media, and program products. Background Technology
[0002] Autofocus technology is one of the core functions of an endoscopic imaging system, and its performance directly affects image quality and operational efficiency.
[0003] The autofocus algorithms used in related technologies for endoscopes mainly include global search, hill climbing, and curve fitting. The global search method controls the motor to traverse the entire stroke and records the sharpness value at each position, selecting the position corresponding to the maximum value as the optimal focus point. This method has high accuracy but requires traversing the entire stroke, resulting in excessive focusing time. The hill climbing and curve fitting methods determine the motor's rotation direction by comparing the sharpness changes of adjacent frames, driving the motor along the direction of increasing sharpness until the peak is found.
[0004] However, both the hill-climbing method and the curve fitting method heavily rely on the accuracy of the initial rotation direction determination. When the motor's initial position is in the out-of-focus area, if the direction determination is incorrect, the motor will move away from the peak value, causing the entire autofocus function to fail. Furthermore, the algorithm itself cannot automatically correct this error, severely impacting the reliability of autofocus. How to eliminate the dependence on the initial rotation direction determination is a pressing technical challenge in the field of endoscopic autofocus. Summary of the Invention
[0005] This invention provides an autofocus method, device, electronic device, storage medium, and program product to solve the problem that autofocus failure occurs when the autofocus algorithm of an endoscope fails to accurately determine the initial rotation direction of the motor during startup.
[0006] In a first aspect, the present invention provides an autofocusing method applied to an endoscopic imaging system, the method comprising: In response to the command to activate the autofocus function, the motor is controlled to rotate at maximum speed along the first direction to the preset starting position; After the motor reaches the preset starting position, the motor is controlled to rotate in a second direction; the second direction is the opposite of the first direction. During the process of the motor rotating along the second direction, a first sharpness value of the image at the current frame position and a second sharpness value of the image at the previous frame position are obtained; the previous frame position is the previous step position of the motor before reaching the current frame position. Calculate the ratio of the first sharpness value to the second sharpness value, and record it as the sharpness change rate; The current focus state is determined based on the relationship between the sharpness change rate and the preset threshold, and the rotation step size of the motor is controlled according to the current focus state; the focus state includes a first state and a second state, and the motor does not return to the first state after switching from the first state to the second state; Return to the step of obtaining the first sharpness value of the image at the current frame position, and stop focusing when the preset autofocus exit condition is detected.
[0007] In one optional implementation, determining the current focus state based on the relationship between the sharpness change rate and a preset threshold, and controlling the motor's rotation step size based on the current focus state, includes: Set a first threshold, which is a constant greater than 1; When the condition 1 < resolution change rate < first threshold is met, it is determined that the current state is the first state, and the motor is controlled to rotate along the second direction by the first step length; When the sharpness change rate is greater than or equal to the first threshold, it is determined that the current state is the second state, and the motor is controlled to rotate along the second direction in a second step. Wherein, the length of the first step is greater than the length of the second step.
[0008] In one optional implementation, the autofocus exit condition includes: In the second state, when the number of times the sharpness change rate is less than 1 is continuously detected reaches a first preset number, it is determined that the optimal focus position has been passed. The method further includes: In response to the autofocus exit condition being met, the motor is controlled to reverse and retract in the second step length, the number of retraction steps being equal to the first preset number, so that the motor returns to the starting position of this continuous descent and stops focusing.
[0009] In one optional implementation, the autofocus exit condition further includes: In the initial stage when the motor starts rotating in the second direction from the preset starting position, if the number of times the sharpness change rate is less than 1 is continuously detected reaches a second preset number, it is determined that the preset starting position is not in the focus area; The method further includes: In response to the fulfillment of the autofocus exit condition, the motor is controlled to return to the preset starting position, and focusing is stopped.
[0010] In one optional implementation, the autofocus exit condition further includes: The motor rotates in the second direction until the end of its stroke; The method further includes: In response to the fulfillment of the autofocus exit condition, the motor is controlled to stop at the current endpoint position, and focusing is stopped.
[0011] In one optional implementation, the autofocus exit condition further includes: The brightness deviation between the brightness value of the image at the current frame position and the brightness value of the image at the previous frame position exceeds the preset brightness threshold. The method further includes: In response to the fulfillment of the autofocus exit condition, the current autofocus process is immediately interrupted and exited.
[0012] In a second aspect, the present invention provides an autofocus device for use in an endoscopic imaging system, the device comprising: The first rotation module is used to control the motor to rotate at maximum speed along the first direction to a preset starting position in response to the autofocus function activation command; The second rotation module is used to control the motor to rotate in a second direction after the motor reaches the preset starting position; the second direction is the opposite of the first direction. The acquisition module is used to acquire a first sharpness value of the image at the current frame position and a second sharpness value of the image at the previous frame position during the process of the motor rotating along the second direction; the previous frame position is the previous step position of the motor before reaching the current frame position; The calculation module is used to calculate the ratio of the first sharpness value to the second sharpness value, denoted as the sharpness change rate; The step size control module is used to determine the current focus state based on the relationship between the sharpness change rate and a preset threshold, and to control the rotation step size of the motor based on the current focus state; the focus state includes a first state and a second state, and the motor does not return to the first state after switching from the first state to the second state; The exit detection module is used to return to the step of obtaining the first sharpness value of the image at the current frame position, until the preset autofocus exit condition is met, at which point focusing stops.
[0013] Thirdly, the present invention provides an electronic device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the computer instructions to perform the autofocus method of the first aspect or any corresponding embodiment described above.
[0014] Fourthly, the present invention provides a computer-readable storage medium storing computer instructions for causing a computer to perform the autofocus method of the first aspect or any corresponding embodiment thereof.
[0015] Fifthly, the present invention provides a computer program product, including computer instructions for causing a computer to execute the autofocus method of the first aspect or any corresponding embodiment thereof.
[0016] The technical solution provided by this invention may include the following beneficial effects: The autofocus method provided by this invention eliminates the dependence on the initial rotation direction judgment by first forcing the motor to return to a preset starting position at maximum speed, thus solving the problem of focus failure caused by incorrect direction judgment in traditional algorithms. Sharpness is not calculated during the return process, saving computational resources. Subsequently, the motor steps unidirectionally in the opposite direction, and the change rate is calculated by using the sharpness ratio of adjacent frames, eliminating the influence of scene differences on the absolute value of sharpness. By setting a first state and a second state and adopting a unidirectional switching strategy, the motor automatically reduces the step size when approaching the peak value, avoiding exceeding the peak while ensuring convergence speed. Finally, by iteratively approximating until the exit condition is met, fast and reliable focusing is achieved. Attached Figure Description
[0017] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0018] Figure 1 This is a schematic diagram of an application scenario according to an embodiment of the present invention; Figure 2 This is a schematic flowchart of a first type of autofocus method according to an embodiment of the present invention; Figure 3 This is a schematic diagram of a second process of an autofocus method according to an embodiment of the present invention; Figure 4 This is a schematic diagram of the relationship between sharpness and motor position in a normal focusing scenario according to an embodiment of the present invention; Figure 5 This is a schematic diagram of the relationship between sharpness and motor position in a scenario where the starting position is descending, according to an embodiment of the present invention. Figure 6 This is a schematic diagram of the relationship between sharpness and motor position in a scenario where the peak value is not found at the endpoint, according to an embodiment of the present invention. Figure 7 This is a schematic diagram of the third process of the autofocus method according to an embodiment of the present invention; Figure 8 This is a structural block diagram of an autofocus device according to an embodiment of the present invention; Figure 9 This is a schematic diagram of the hardware structure of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0019] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0020] It is understood that before using the technical solutions disclosed in the various embodiments of the present invention, users should be informed of the types, scope of use, and usage scenarios of the personal information involved in the present invention and their authorization should be obtained in accordance with relevant laws and regulations through appropriate means.
[0021] The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this invention, "a plurality of" means two or more, unless otherwise explicitly specified.
[0022] As an optional application scenario of this invention, such as Figure 1 As shown, this autofocus system can be applied to endoscopic imaging equipment. Figure 1 The example shows that the autofocus system includes an endoscope handpiece 101, an insertion tube 102, and an image processing host 103. The endoscope handpiece 101 is connected to the image processing host 103 through the insertion tube 102, and the image processing host 103 is connected to the display 104 through a video interface.
[0023] The endoscope handheld unit 101 houses a motor drive module and an imaging unit. The imaging unit includes a lens assembly and an image sensor. The motor drive module is connected to the lens assembly or image sensor and drives it to move back and forth along the optical axis to change the focus position. The image sensor acquires image data of the target object and transmits this data to the image processing host 103 via a signal transmission line within the insertion tube 102. The image processing host 103 can be a standalone image processing workstation, an embedded processing unit integrated into the endoscope handheld unit 101, or a portable terminal device with image processing capabilities.
[0024] It should be noted that, Figure 1This is merely an example of an application scenario and does not limit the scope of protection of this invention. The autofocus method provided in this embodiment of the invention is also applicable to other types of electronic endoscopes, including but not limited to gastroscopes, colonoscopes, bronchoscopes, laparoscopes, and other endoscopes used in gastroenterology, respiratory medicine, urology, and surgery, as well as industrial endoscopes. Furthermore, the autofocus method provided in this embodiment of the invention can also be applied to other portable imaging devices requiring rapid focusing, and this embodiment of the invention does not impose any limitations in this regard.
[0025] The autofocus method of the present invention will be described in detail below with reference to the accompanying drawings. It should be understood that the sharpness curve and motor position relationship shown in the drawings are merely illustrative. In actual applications, the curve of image sharpness changing with motor position may vary depending on lens parameters, image sensor resolution, and the characteristics of the subject, but the method provided in the embodiments of the present invention is applicable to all of them.
[0026] According to an embodiment of the present invention, an autofocus method embodiment is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0027] This embodiment provides an autofocus method applied to an endoscopic imaging system, which can be executed by the aforementioned image processing host. Figure 2 This is a flowchart of an autofocus method according to an embodiment of the present invention, such as... Figure 2 As shown, the process includes the following steps: In step S201, in response to the command to activate the autofocus function, the motor is controlled to rotate at maximum speed along the first direction to a preset starting position.
[0028] In response to the autofocus activation command, the image processing host controls the motor to rotate at maximum speed along a first direction to a preset starting position. This preset starting position is a reference point near one of the motor's extreme positions during its travel. During this process, the motor only performs rapid rotation to reach this starting position in the shortest possible time. This eliminates uncertainty about the motor's current position, allowing subsequent focus searching to begin from a fixed, known starting point. Since this stage only requires quickly returning the motor to its original position, the sharpness of each frame is not calculated, thus saving time and computational resources.
[0029] Step S202: After the motor reaches the preset starting position, control the motor to rotate in the second direction.
[0030] Once the motor reaches the preset starting position, it immediately changes its rotation direction and begins rotating in a second direction, which is the opposite of the first direction. By first allowing the motor to quickly return to the starting position and then rotating in the opposite direction, the subsequent search process only needs to proceed in one fixed direction. During the motor's rotation in this direction, the clarity first increases and then decreases, forming a complete cycle of change. This completely avoids the technical difficulty of traditional algorithms that require determining whether the motor's rotation direction is correct at startup.
[0031] Step S203: During the rotation of the motor along the second direction, the first sharpness value of the image at the current frame position and the second sharpness value of the image at the previous frame position are obtained.
[0032] The previous frame position is the previous step position of the motor before reaching the current frame position.
[0033] As the motor rotates in the second direction, it captures a frame and calculates its sharpness value at each position. Sharpness measures the clarity of an image; a higher sharpness value indicates better focus. Since the motor rotates incrementally, the current frame position and the previous frame position are two adjacent step positions, separated by one step. By acquiring the sharpness value of the image at the current frame position and simultaneously reading the sharpness value of the image at the previous frame position, the sharpness values of two adjacent positions can be obtained, allowing for a comparison of the trend in sharpness changes.
[0034] Step S204: Calculate the ratio of the first sharpness value to the second sharpness value, and record it as the sharpness change rate.
[0035] The ratio obtained by dividing the first sharpness value at the current frame position by the second sharpness value at the previous frame position is the sharpness change rate. This sharpness change rate reflects the direction and magnitude of the sharpness change after each motor step: when the change rate is greater than 1, it indicates that the sharpness is increasing and the motor is moving towards the optimal focus position; when the change rate is equal to 1, it indicates that the sharpness remains unchanged; when the change rate is less than 1, it indicates that the sharpness is decreasing and the motor has passed the optimal focus position. This sharpness change rate is the basis for subsequent judgment of focus status and control step size. By using a ratio rather than a difference, the influence of differences in absolute sharpness values under different scenarios can be eliminated.
[0036] Step S205: Determine the current focus state based on the relationship between the sharpness change rate and the preset threshold, and control the rotation step of the motor according to the current focus state.
[0037] The focusing state includes a first state and a second state. Once the motor switches from the first state to the second state, it will not return to the first state.
[0038] The image processing unit categorizes the current focus state into two phases based on the relationship between the rate of change in sharpness and a preset threshold: Phase 1 involves a slow increase in sharpness, using a larger step size for rapid focusing; Phase 2 involves a rapid increase in sharpness, switching to a smaller step size for fine-tuning. It's important to note that the focus does not revert to Phase 1 after switching from Phase 1 to Phase 2. This is because once sharpness enters a rapid increase phase, the motor is nearing its optimal focus position, and continuing to use a large step size at this point risks exceeding the peak focus. Maintaining a small step size for fine-tuning is the only reasonable control strategy. The rotation step size for each step is determined by the current state, not by a pre-set value.
[0039] Step S206: Return to the step of obtaining the first sharpness value of the image at the current frame position, until the preset autofocus exit condition is detected, and then stop focusing.
[0040] After each rotation and status judgment step is completed, the image processing host returns to step S203 to continue acquiring sharpness values, calculating the sharpness change rate, determining the current focus state, and controlling the rotation step size at the new position. This cycle repeats, gradually bringing the motor closer to the optimal focus position. This cycle continues until a preset autofocus exit condition is detected and stopped. The autofocus exit condition is used to determine whether the motor has reached or exceeded the optimal focus position. Once the condition is met, focusing stops, the motor maintains its current position, and the entire autofocus process is completed.
[0041] The autofocus method provided in this embodiment eliminates the dependence on the initial rotation direction judgment by first forcing the motor to return to a preset starting position at maximum speed, thus solving the problem of focus failure caused by incorrect direction judgment in traditional algorithms. Sharpness is not calculated during the return process, saving computational resources. Subsequently, the motor steps unidirectionally in the opposite direction, and the change rate is calculated by using the sharpness ratio of adjacent frames, eliminating the influence of scene differences on the absolute value of sharpness. By setting a first state and a second state and adopting a unidirectional switching strategy, the motor automatically reduces the step size when approaching the peak value, avoiding exceeding the peak while ensuring convergence speed. Finally, by iteratively approximating until the exit condition is met, fast and reliable focusing is achieved.
[0042] This embodiment provides an autofocus method applied to an endoscopic imaging system, which can be executed by the aforementioned image processing host. Figure 3 This is a flowchart of an autofocus method according to an embodiment of the present invention, such as... Figure 3 As shown, the process includes the following steps: In step S301, in response to the command to activate the autofocus function, the motor is controlled to rotate at maximum speed along the first direction to a preset starting position.
[0043] For example, Figure 4This is a schematic diagram illustrating the relationship between sharpness and motor position under normal focusing conditions according to an embodiment of the present invention. Figure 4 As shown, the curve relating image sharpness to motor position is hill-shaped, with the horizontal axis representing motor position and the vertical axis representing image sharpness. Image sharpness is highest when the motor is at position 0. The motor rotates forward from -4 to 4, and sharpness increases from -4 to 0; this range is called the focus area. Sharpness decreases from 0 to 4; this range is called the out-of-focus area. When the motor is in the focus area, the correct rotation direction is the positive direction, the same as the default motor rotation direction. When the motor is in the out-of-focus area, the correct rotation direction is the opposite direction, the opposite of the default motor rotation direction. Since the motor position is random when autofocus starts, related technologies only determine the rotation direction based on the sharpness of the preceding and following frames, resulting in poor robustness and inability to adapt to all scenarios. This embodiment, however, completely avoids the problem of direction determination by forcing the motor to quickly return to its starting position, ensuring that the next rotation always begins from the starting point of the focus area.
[0044] Specifically, when the autofocus function is activated, regardless of whether the motor is currently in the focus area or the out-of-focus area, the image processing host controls the motor to rotate along the first direction at the highest speed allowed by the hardware to the preset starting position (e.g., Figure 4 (As shown in position -4). During this process, the motor only performs rapid rotation to reach the starting position in the shortest time, and does not calculate the sharpness of each frame, thus saving computing resources. This preset starting position is a reference point near one end of the motor's stroke, located at or near the starting point of the focus area.
[0045] Step S302: After the motor reaches the preset starting position, control the motor to rotate in the second direction.
[0046] The second direction is the opposite of the first direction.
[0047] For example, after the motor reaches the preset starting position, the image processing host controls the motor to change its rotation direction, along the second direction (i.e., the opposite direction of the first direction, such as...). Figure 4 The motor begins to rotate from -4 to 4. Afterward, the motor only needs to rotate in this fixed direction; there is no need to check whether the rotation direction is correct.
[0048] Step S303: During the process of the motor rotating along the second direction, the first sharpness value of the image at the current frame position and the second sharpness value of the image at the previous frame position are obtained.
[0049] The previous frame position is the previous step position of the motor before reaching the current frame position.
[0050] Please see details Figure 2Step S203 of the illustrated embodiment will not be repeated herein.
[0051] Step S304: calculating the ratio of the first sharpness value to the second sharpness value, which is recorded as a sharpness change rate.
[0052] For details, please refer to Figure 2 Step S204 of the illustrated embodiment will not be repeated herein.
[0053] Step S305: determining the current focusing state according to the relationship between the sharpness change rate and a preset threshold, and controlling the rotation step size of the motor according to the current focusing state.
[0054] The focusing state includes a first state and a second state, and the motor does not return to the first state after switching from the first state to the second state.
[0055] In an optional implementation, a first threshold TH is set, where the first threshold is a constant greater than 1 (TH>1) and can be set according to actual debugging results. When 1 < sharpness change rate < the first threshold (1<rate<TH) is satisfied, it is determined that the current state is the first state (focusing state 1), which indicates that the sharpness is rising slowly, and the image processing host controls the motor to rotate in the second direction at a first step size step1 to quickly approach the peak region; when the sharpness change rate ≥ the first threshold (rate≥TH) is satisfied, it is determined that the current state is the second state (focusing state 2), which indicates that the sharpness is rising rapidly and the motor is close to the optimal focusing position, and the motor is controlled to rotate in the second direction at a second step size step2 to perform fine search; where the first step size is greater than the second step size (step1>step2), and the second step size step2 is usually set to the minimum rotation unit of the motor.
[0056] For example, as shown in Figure 4 the figure, nine points a, b, c, d, e, f, g, h, and i are taken from the relationship curve. When the motor rotates to position a, the sharpness of the image at this time is recorded as A; then the motor rotates to position b according to a predetermined step size, and the sharpness of the image at this time is recorded as B. If B / A>1 and B / A<TH, the motor rotates in the second direction at a rotation step size of step1, which is focusing state 1. Until B / A≥TH, the motor rotates in the second direction at a rotation step size of step2, focusing state 1 is switched to focusing state 2, and in the subsequent rotation process, the step size is kept constant as step2, that is, the focusing state 2 is always maintained.
[0057] Step S306: returning to the step of acquiring the first sharpness value of the image at the current frame position, and stopping focusing until it is detected that a preset autofocus exit condition is satisfied.
[0058] Specifically, step S306 includes: Step S3061: Return to the step of obtaining the first sharpness value of the image at the current frame position until a preset autofocus exit condition is detected.
[0059] In one optional implementation, since the endoscope is held by the operator during clinical use, slight shaking of the image is unavoidable. Normally, the regular micro-movements caused by physiological activities such as heartbeat and respiration do not affect the autofocus algorithm. However, when operational errors cause significant changes in the endoscope image, such as rapid lens movement, the subject moving out of the field of view, or sudden changes in lighting conditions, if autofocus is in progress, the autofocus function should be actively discontinued to avoid focusing errors caused by scene changes. Therefore, during autofocus, it is necessary to monitor in real time whether there are drastic changes in the image scene, using this as a basis for actively interrupting focus. This implementation determines whether there are drastic changes in the scene by monitoring changes in image brightness. The autofocus exit condition includes a brightness abrupt change interruption exit condition: During autofocus, the image processing host acquires the brightness value of each frame in real time and calculates the brightness deviation between the brightness value of the current frame and the brightness value of the previous frame. If the brightness deviation exceeds a preset brightness threshold, it is determined that the image scene has changed drastically, and the sharpness data at this point can no longer reflect the true focus state. It should be noted that if the brightness deviation between the current and previous frame brightness values is less than or equal to the preset brightness threshold, it indicates that the image brightness is stable, and the autofocus process continues.
[0060] In one alternative implementation, the autofocus exit condition includes a peak-overdue exit condition: in the second state, when the number of times the sharpness change rate is continuously detected to be less than 1 reaches a first preset number, it is determined that the motor has passed the optimal focus position.
[0061] In one optional implementation, the autofocus exit condition further includes an exit condition after determining the starting position: when the image processing host continuously detects that the sharpness change rate is less than 1 for a second preset number of times during the initial stage of the motor rotating along the second direction from the preset starting position, it determines that the preset starting position is not in the focus area.
[0062] In one alternative implementation, the autofocus exit condition further includes an exit condition after the end of the travel: the image processing host controls the motor to rotate along the second direction until the end of the motor travel.
[0063] In step S3062, in response to the autofocus exit condition being met, the current autofocus process is immediately interrupted and exited.
[0064] In response to the brightness change interruption exit condition, the image processing host immediately interrupts and exits the current autofocus process, forming an active protection measure. When the image scene changes drastically due to endoscope lens shake, subject movement, or sudden changes in lighting conditions, the focusing process can be terminated in time to avoid focusing failure due to invalid or erroneous sharpness data, thereby improving the reliability and safety of the system in clinical applications.
[0065] In step S3063, in response to the autofocus exit condition being met, the motor is controlled to reverse and retract in the second step length, the number of retraction steps being equal to the first preset number, so that the motor returns to the starting position of this continuous descent and stops focusing.
[0066] In response to the exit condition after the peak value is exceeded, the image processing host controls the motor to reverse and retreat with a second step size, the number of retreating steps equal to the first preset number, so that the motor returns to the starting position of this continuous descent and stops focusing. When the motor passes the optimal focus position during the fine search phase, if the sharpness change rate is consistently less than 1, it indicates that the sharpness is decreasing, and at this point, it is determined that the peak value has been exceeded. Since the motor advances with a second step size in each step in the second state, and the first preset number of descents are detected consecutively, the motor has traveled forward from the peak position by a number of steps equal to the first preset number. Therefore, controlling the motor to reverse and retreat by the same number of steps allows the motor to accurately return to the starting position of this continuous descent, which is the position with the highest sharpness during this search process.
[0067] For example, with Figure 4 The curve shown is used as an example for explanation. Assume that in the second state (focus state 2), the rate of change in sharpness is greater than 1 (rate>1) when the motor passes points d, e, and f sequentially, so the motor continues to rotate in the second direction. When it reaches point g, the rate of change in sharpness is less than 1 (rate<1), and when it continues to point h, the rate of change in sharpness is again less than 1 (rate<1), and when it reaches point i, the rate of change in sharpness is less than 1 for the third time (rate<1). The system detects that the rate of change in sharpness is less than 1 (rate<1) three times consecutively, and determines that the motor has exceeded the peak value. Since the motor advances in steps 2 at a time in the second state, three consecutive descents indicate that the motor has advanced forward by the first preset number of steps from the peak position. Therefore, controlling the motor to reverse and retreat the same number of steps (i.e., retreat from point i to point f) will allow the motor to accurately return to the starting position of this continuous descent, which is the position with the highest sharpness during this search process.
[0068] In step S3064, in response to the autofocus exit condition being met, the motor is controlled to return to the preset starting position and focus is stopped.
[0069] In response to the exit condition after determining the starting position, the image processing host controls the motor to return to the preset starting position and stops focusing. For example, Figure 5 This is a schematic diagram illustrating the relationship between sharpness and motor position in a scenario where the starting position is decreasing, according to an embodiment of the present invention. In the initial stage when the motor starts rotating along the second direction from the preset starting position, the rate of change in sharpness is consistently less than 1, indicating that the sharpness has not increased from the starting position but has instead continued to decrease, meaning the starting position is not within the focus area. At this point, continuing to rotate along the second direction is meaningless, and the motor is controlled to return to the preset starting position (i.e.,...). Figure 5 (At the position of the horizontal axis -4) and stop focusing. This step serves as an exception handling branch, preventing the motor from searching ineffectively within the wrong area and ensuring the robustness of the algorithm under various initial conditions.
[0070] In step S3065, in response to the autofocus exit condition being met, the motor is controlled to stop at the current endpoint position, and focusing is stopped.
[0071] In response to the exit condition after the end of the journey, the image processing host controls the motor to stop at the current end position and cease focusing. For example, Figure 6 This is a schematic diagram illustrating the relationship between sharpness and motor position in a scenario where the peak value is not found at the endpoint, according to an embodiment of the present invention. When the motor starts from a preset starting position (i.e.... Figure 6 When the motor (located at position -4 on the horizontal axis) begins to rotate in the second direction, the sharpness value continuously increases. If no decrease in sharpness is detected until the end of the travel range, it indicates that the motor has traversed the entire movable range without finding the peak value. In this case, the motor stops at the current end position and exits the autofocus function. This step serves as a boundary protection measure, controlling the motor to remain at the current end position and stop focusing to prevent the motor from exceeding the hardware travel range and causing equipment damage, while also ensuring that the algorithm can exit normally under any circumstances.
[0072] The autofocus method provided in this embodiment subdivides the sharpness improvement process into two stages: a slow increase and a rapid increase, by setting a first threshold. During the rapid increase stage, it automatically switches to the minimum step size for fine-grained searching, ensuring rapid approach when far from the focus point while avoiding exceeding the peak value when approaching the focus point. Corresponding exit conditions and response actions are configured for various abnormal scenarios such as sudden changes in image brightness, improper starting position, and endpoint of the search. The method can stably exit under various conditions, including normal focusing, continuous descent, continuous ascent, and sudden scene changes, enhancing the robustness and safety of the algorithm in clinical operations.
[0073] As one or more specific application embodiments of the present invention, the optimal implementation scheme or the scheme that the inventors most want to embody is described in combination with the specific application scenario.
[0074] Figure 7This is a flowchart of an autofocus method according to an embodiment of the present invention, such as... Figure 7 As shown, the process includes the following steps: Rapid homing phase: In response to the command to activate the autofocus function, AF (Auto Focus) is turned on, and the image processing host controls the motor to rotate at maximum speed along the first direction to the preset starting position (i.e., a large reverse rotation). During this process, the sharpness of each frame is not calculated.
[0075] Sharpness Search Phase: After the motor reaches the preset starting position, the image processing host controls the motor to start rotating in the second direction at a predetermined step size (i.e., forward rotation). During the rotation, the sharpness value of the image at the previous frame position (denoted as pre) and the sharpness value of the image at the current frame position (denoted as cur1) are calculated in real time. The image processing host calculates the sharpness change rate cur1 / pre and determines whether this sharpness change rate is greater than 1. If yes (i.e., cur1 / pre > 1), it indicates that the sharpness is increasing, and the motor continues to rotate in the second direction at the current step size (i.e., continues to rotate according to the predetermined direction and step size), and returns to execute the step of "obtaining the sharpness value of the previous frame and the sharpness value of the current frame" to continue the sharpness search. If no (i.e., cur1 / pre ≤ 1), it indicates that the sharpness has stopped increasing, and it is determined that the optimal focus position has been passed or reached. The image processing host controls the motor to rotate in the opposite direction of the second direction by one step size (i.e., reverse step size), and then stops focusing, ending the autofocus process.
[0076] Brightness Monitoring Phase: During autofocus, the image processing host simultaneously counts the brightness value of each frame and calculates the brightness difference (denoted as cur2) between the current frame's brightness value and the previous frame's brightness value. The image processing host determines whether this brightness difference is greater than a preset brightness threshold. If so (i.e., cur2 > brightness threshold), it indicates a drastic change in the image scene, and the current autofocus process is immediately interrupted and exited. If not (i.e., cur2 ≤ brightness threshold), it indicates stable image brightness, and the autofocus process continues, returning to the "rapid repositioning phase" and "sharpness search phase" steps, repeating this process until focusing is complete or the process is interrupted and exited.
[0077] This embodiment also provides an autofocus device for implementing the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that performs a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0078] This embodiment provides an autofocus device applied to an endoscopic imaging system, such as... Figure 8 As shown, it includes: The first rotation module 801 is used to control the motor to rotate at maximum speed along the first direction to a preset starting position in response to the activation command of the autofocus function; The second rotation module 802 is used to control the motor to rotate in a second direction after the motor reaches the preset starting position; the second direction is the opposite of the first direction. The acquisition module 803 is used to acquire a first sharpness value of the image at the current frame position and a second sharpness value of the image at the previous frame position during the process of the motor rotating along the second direction; the previous frame position is the previous step position of the motor before reaching the current frame position. The calculation module 804 is used to calculate the ratio of the first sharpness value to the second sharpness value, denoted as the sharpness change rate; The step size control module 805 is used to determine the current focus state based on the relationship between the sharpness change rate and the preset threshold, and to control the rotation step size of the motor based on the current focus state; the focus state includes a first state and a second state, and the motor does not return to the first state after switching from the first state to the second state; The exit detection module 806 is used to return to the step of obtaining the first sharpness value of the image at the current frame position until the preset autofocus exit condition is met, at which point focusing stops.
[0079] In one optional implementation, the step size control module is further configured to: set a first threshold, the first threshold being a constant greater than 1; when 1 < resolution change rate < first threshold, determine that the current state is the first state, and control the motor to rotate along the second direction with a first step size; when resolution change rate ≥ first threshold, determine that the current state is the second state, and control the motor to rotate along the second direction with a second step size; wherein the first step size is greater than the second step size.
[0080] In one optional implementation, the autofocus exit condition includes: in the second state, when the number of times the sharpness change rate is less than 1 is continuously detected reaches a first preset number, it is determined that the current optimal focus position has been exceeded; the exit detection module is further configured to: in response to satisfying the autofocus exit condition, control the motor to reverse and retreat with the second step size, the number of retreat steps being equal to the first preset number, so that the motor returns to the starting position of this continuous descent and stops focusing.
[0081] In an optional implementation, the autofocus exit condition further includes: when the number of times the sharpness change rate is continuously detected to be less than 1 reaches a second preset number during the initial stage of the motor rotating along the second direction from the preset starting position, it is determined that the preset starting position is not in the focus area; the exit detection module is further configured to: in response to satisfying the autofocus exit condition, control the motor to return to the preset starting position and stop focusing.
[0082] In one optional implementation, the autofocus exit condition further includes: the motor rotating along the second direction to the end of the motor travel; the exit detection module is further configured to: in response to satisfying the autofocus exit condition, control the motor to stop at the current end position and stop focusing.
[0083] In one optional implementation, the autofocus exit condition further includes: the brightness deviation between the brightness value of the image at the current frame position and the brightness value of the image at the previous frame position exceeds a preset brightness threshold; the exit detection module is further configured to: immediately interrupt and exit the current autofocus process in response to the autofocus exit condition being met.
[0084] The autofocus device provided in this embodiment of the invention can execute the autofocus method provided in any embodiment of the invention, and has the corresponding functional modules and beneficial effects for executing the method. Further functional descriptions of the various modules and units described above are the same as in the corresponding embodiments described above, and will not be repeated here.
[0085] Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention.
[0086] The following is a detailed reference. Figure 9 This diagram illustrates a structural schematic suitable for implementing an electronic device according to embodiments of the present invention. The electronic device may include a processor 901 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes based on programs stored in ROM 902 (Read-Only Memory) or programs loaded from memory 908 into RAM 903 (Random Access Memory). RAM 903 also stores various programs and data required for the operation of the electronic device. The processor 901, ROM 902, and RAM 903 are interconnected via bus 904. An I / O interface 905 (input / output interface) is also connected to bus 904.
[0087] Typically, the following devices can be connected to I / O interface 905: input devices 906 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 907 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; memory devices 908 including, for example, magnetic tapes, hard disks, etc.; and communication devices 909. Communication device 909 allows electronic devices to exchange data via wireless or wired communication with other devices. Although Figure 9 Electronic devices with various devices are shown, but it should be understood that it is not required to implement or have all of the devices shown, and more or fewer devices may be implemented or have instead.
[0088] In particular, according to embodiments of the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 909, or installed from a memory 908, or installed from a ROM 902. When the computer program is executed by the processor 901, it performs the functions defined in the autofocus method of the embodiments of the present invention.
[0089] Figure 9 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments of the present invention.
[0090] This invention also provides a computer-readable storage medium. The methods described above according to embodiments of the invention can be implemented in hardware or firmware, or implemented as computer code that can be recorded on a storage medium, or implemented as computer code downloaded via a network and originally stored on a remote storage medium or a non-transitory machine-readable storage medium and then stored on a local storage medium. Thus, the methods described herein can be processed by software stored on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. The storage medium can be a magnetic disk, optical disk, read-only memory, random access memory, flash memory, hard disk, or solid-state drive, etc.; further, the storage medium can also include combinations of the above types of memory. It is understood that computers, processors, microprocessor controllers, or programmable hardware include storage components capable of storing or receiving software or computer code. When the software or computer code is accessed and executed by the computer, processor, or hardware, the autofocus method shown in the above embodiments is implemented.
[0091] A portion of this invention can be applied as a computer program product, such as computer program instructions, which, when executed by a computer, can invoke or provide the methods and / or technical solutions according to the invention through the operation of the computer. Those skilled in the art will understand that the forms in which computer program instructions exist in a computer-readable medium include, but are not limited to, source files, executable files, installation package files, etc. Correspondingly, the ways in which computer program instructions are executed by a computer include, but are not limited to: the computer directly executing the instructions, or the computer compiling the instructions and then executing the corresponding compiled program, or the computer reading and executing the instructions, or the computer reading and installing the instructions and then executing the corresponding installed program. Here, the computer-readable medium can be any available computer-readable storage medium or communication medium accessible to a computer.
[0092] Although embodiments of the invention have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the invention, and such modifications and variations all fall within the scope defined by the invention.
Claims
1. An autofocus method, characterized in that, The method, applied to an endoscopic imaging system, includes: In response to the command to activate the autofocus function, the motor is controlled to rotate at maximum speed along the first direction to the preset starting position; After the motor reaches the preset starting position, the motor is controlled to rotate in a second direction; the second direction is the opposite of the first direction. During the process of the motor rotating along the second direction, a first sharpness value of the image at the current frame position and a second sharpness value of the image at the previous frame position are obtained; the previous frame position is the previous step position of the motor before reaching the current frame position. Calculate the ratio of the first sharpness value to the second sharpness value, and record it as the sharpness change rate; The current focus state is determined based on the relationship between the sharpness change rate and the preset threshold, and the rotation step size of the motor is controlled according to the current focus state; the focus state includes a first state and a second state, and the motor does not return to the first state after switching from the first state to the second state; Return to the step of obtaining the first sharpness value of the image at the current frame position, and stop focusing when the preset autofocus exit condition is detected.
2. The method according to claim 1, characterized in that, The step of determining the current focus state based on the relationship between the sharpness change rate and a preset threshold, and controlling the motor's rotation step size based on the current focus state, includes: Set a first threshold, which is a constant greater than 1; When the condition 1 < resolution change rate < first threshold is met, it is determined that the current state is the first state, and the motor is controlled to rotate along the second direction by the first step length; When the sharpness change rate is greater than or equal to the first threshold, it is determined that the current state is the second state, and the motor is controlled to rotate along the second direction in a second step. Wherein, the length of the first step is greater than the length of the second step.
3. The method according to claim 2, characterized in that, The autofocus exit conditions include: In the second state, when the number of times the sharpness change rate is less than 1 is continuously detected reaches a first preset number, it is determined that the optimal focus position has been passed. The method further includes: In response to the autofocus exit condition being met, the motor is controlled to reverse and retract in the second step length, the number of retraction steps being equal to the first preset number, so that the motor returns to the starting position of this continuous descent and stops focusing.
4. The method according to claim 1 or 2, characterized in that, The autofocus exit condition also includes: In the initial stage when the motor starts rotating in the second direction from the preset starting position, if the number of times the sharpness change rate is less than 1 is continuously detected reaches a second preset number, it is determined that the preset starting position is not in the focus area; The method further includes: In response to the fulfillment of the autofocus exit condition, the motor is controlled to return to the preset starting position, and focusing is stopped.
5. The method according to claim 1 or 2, characterized in that, The autofocus exit condition also includes: The motor rotates in the second direction until the end of its stroke; The method further includes: In response to the fulfillment of the autofocus exit condition, the motor is controlled to stop at the current endpoint position, and focusing is stopped.
6. The method according to claim 1 or 2, characterized in that, The autofocus exit condition also includes: The brightness deviation between the brightness value of the image at the current frame position and the brightness value of the image at the previous frame position exceeds the preset brightness threshold. The method further includes: In response to the fulfillment of the autofocus exit condition, the current autofocus process is immediately interrupted and exited.
7. An automatic focusing device, characterized in that, The device is used in endoscopic imaging systems and includes: The first rotation module is used to control the motor to rotate at maximum speed along the first direction to a preset starting position in response to the autofocus function activation command; The second rotation module is used to control the motor to rotate in a second direction after the motor reaches the preset starting position; the second direction is the opposite of the first direction. The acquisition module is used to acquire a first sharpness value of the image at the current frame position and a second sharpness value of the image at the previous frame position during the process of the motor rotating along the second direction; the previous frame position is the previous step position of the motor before reaching the current frame position; The calculation module is used to calculate the ratio of the first sharpness value to the second sharpness value, denoted as the sharpness change rate; The step size control module is used to determine the current focus state based on the relationship between the sharpness change rate and a preset threshold, and to control the rotation step size of the motor based on the current focus state; the focus state includes a first state and a second state, and the motor does not return to the first state after switching from the first state to the second state; The exit detection module is used to return to the step of obtaining the first sharpness value of the image at the current frame position, until the preset autofocus exit condition is met, at which point focusing stops.
8. An electronic device, characterized in that, include: A memory and a processor are communicatively connected, the memory storing computer instructions, and the processor executing the computer instructions to perform the autofocus method of any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing the computer to perform the autofocus method according to any one of claims 1 to 6.
10. A computer program product, characterized in that, Includes computer instructions for causing a computer to perform the autofocus method according to any one of claims 1 to 6.
