Autofocus method, apparatus, electronic device, storage medium, and program product

CN122994616APending Publication Date: 2026-10-27SHENZHEN COMEN MEDICAL INSTR
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611215404.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-11
Publication Date
2026-10-27

AI Technical Summary

Technical Problem

[0005]本发明提供了一种自动对焦方法、装置、电子设备、存储介质及程序产品,以解决相关技术中的自动对焦方案难以兼顾对焦速度和定位精度的问题

Benefits of technology

本发明提供的自动对焦方法,先以第一步长快速遍历并锁定目标区域,避免全行程搜索,大幅缩短粗搜时间;再以更小的第二步长在目标区域内精细步进,防止越过峰值。在对数域上计算清晰度偏差,使误差信号更准确地反映真实变化趋势。比例-积分-微分控制器综合利用当前及历史误差项进行加权计算,使电机每一步的转动角度均根据清晰度的实际变化趋势实时动态确定,而非采用固定步长,从而使电机趋近焦点时速度平滑衰减,兼顾了速度与精度。最后,通过检测电机在峰值附近微步位置间的往复振荡状态来判断退出时机,确保对焦完成的判定准确可靠。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122994616A_ABST
    Figure CN122994616A_ABST
Patent Text Reader

Abstract

The application relates to the technical field of endoscope imaging, and discloses an automatic focusing method and device, electronic equipment, a storage medium and a program product. The method is applied to an endoscope imaging system and comprises the following steps: controlling a motor to rotate in a first direction at a first step length, acquiring the image definition at each position in real time during the rotation process, and determining a target area containing an optimal focusing position; controlling the motor to rotate to a preset starting position in the target area and stepping to a current frame position in a direction towards the inside of the target area at a second step length; acquiring a first definition value and a second definition value, calculating the deviation value of the two in a logarithmic domain as a current error term; acquiring a first historical error term and a second historical error term; calculating the target rotation angle and the target rotation direction of the next step of the motor; and controlling the motor to rotate to the next position, and repeating the above steps until an automatic focusing exit condition is met. When the automatic focusing function is implemented, the above scheme can take into account the speed and the accuracy.
Need to check novelty before this filing date? Find Prior Art

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 a key component of endoscopic imaging systems, and its performance directly affects image clarity and diagnostic accuracy. How to enable the motor to quickly, smoothly, and accurately reach the optimal focusing position is a pressing issue that needs to be addressed.

[0003] In related technologies, autofocus algorithms are commonly used for autofocus, such as global search, hill climbing, curve fitting, and the golden section method. Among them, the global search method searches for the maximum sharpness value by traversing the entire path, which has high accuracy but is the most time-consuming; the hill climbing method determines the direction of rotation by comparing the sharpness changes of adjacent positions and converges repeatedly near the peak; the curve fitting method collects sharpness data from multiple sampling points and estimates the peak position by fitting a curve; the golden section method gradually narrows the search interval according to the golden ratio, reducing the number of samplings.

[0004] However, the aforementioned algorithms use pre-set fixed step sizes to control the motor rotation during focusing. While some algorithms claim to have variable step size capabilities, their variable step size strategy merely involves multiplying the fixed step size by a preset coefficient such as 1 / 2 or 1 / 3 to decrease it in a stepwise manner. Essentially, they still rely on the initial fixed step size setting and fail to adjust the step size in real time and continuously according to the actual trend of image sharpness changes. This results in the motor taking too large a step when approaching the optimal focusing position, easily exceeding the peak value, while taking too small a step results in slow convergence. The entire focusing process is relatively coarse, making it difficult to balance focusing speed and positioning accuracy. Summary of the Invention

[0005] This invention provides an autofocus method, apparatus, electronic device, storage medium, and program product to solve the problem that autofocus solutions in related technologies struggle to balance focusing speed and positioning accuracy.

[0006] In a first aspect, the present invention provides an autofocusing method applied to an endoscopic imaging system, the method comprising: The motor is controlled to rotate in the first direction by the first step length, and the image clarity at each position is acquired in real time during the rotation to determine the target area containing the optimal focus position; The motor is controlled to rotate to a preset starting position within the target area, and then moves to the current frame position in a direction toward the interior of the target area with a second step length; the second step length is less than the first step length. Obtain the first sharpness value of the current frame image at the current frame position, and the second sharpness value of the previous frame image at the previous frame position; the previous frame position is the position of the motor before reaching the current frame position; Calculate the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, and use it as the current error term; The sharpness deviation values ​​of the two frames at the previous two frame positions and the three frames at the previous three frame positions are obtained and used as the first historical error term and the second historical error term, respectively. The current error term, the first historical error term, and the second historical error term are input into the proportional-integral-derivative controller to calculate the target rotation angle and target rotation direction of the motor for the next step. The process of controlling the motor to rotate to the next position according to the target rotation angle and target rotation direction, and returning to obtain the first sharpness value of the current frame image at the current frame position, continues until the motion state of the motor near the optimal focus position meets the preset autofocus exit condition, at which point focusing stops.

[0007] In one optional implementation, calculating the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, as the current error term, includes: The current error term is calculated using the following formula. :

[0008] in, This represents the first resolution value. This indicates the second resolution value.

[0009] In one alternative implementation, the calculation of the target rotation angle of the motor for the next step includes: The target rotation angle is calculated using the following formula. :

[0010] in, This represents the proportionality coefficient. Represents the integral coefficient. Represents the differential coefficient. This represents the deviation of the sharpness value between the previous frame and the two frames above it in the logarithmic domain. This represents the deviation of the sharpness values ​​between the previous two frames and the previous three frames in the logarithmic domain.

[0011] In one alternative implementation, determining the target area containing the optimal focus position includes: The motor is controlled to rotate step by step along the first direction from the initial position with the first step length, and the sharpness value of the current frame image is calculated after each step. When the rate of increase of the sharpness value relative to the sharpness value of the previous frame image is detected to be greater than the preset increase threshold, the current motor position is recorded as the first sampling point; The motor is controlled to rotate in the first direction at the first step length from the first sampling point. When the growth rate of the sharpness value relative to the sharpness value of the previous frame image is less than zero and the number of consecutive decreases reaches a preset number, the current motor position is recorded as the second sampling point. The target area is determined based on the positions of the first sampling point and the second sampling point; wherein the first sampling point is located on the side of the target area closer to the starting end, the second sampling point is located on the side of the target area closer to the ending end, and the optimal focus position is located between the first sampling point and the second sampling point.

[0012] In one optional implementation, the preset starting position is the location of the second sampling point; the direction toward the interior of the target area is the direction from the second sampling point to the first sampling point.

[0013] In one optional implementation, the detection that the motor's movement near the optimal focus position meets a preset autofocus exit condition includes: The rotational state of the detection motor is between three adjacent micro-step positions near the optimal focusing position; the three adjacent micro-step positions include a first position, a second position, and a third position; the second position is located between the first position and the third position. When the motor oscillates back and forth between the first position, the second position and the third position, record the number of times the motor passes through the second position; When the number of times reaches a preset threshold, it is determined that the autofocus exit condition is met.

[0014] In a second aspect, the present invention provides an autofocus device, the device comprising: The first rotation module is used to control the motor to rotate along the first direction by a step length, and to acquire the image clarity at each position in real time during the rotation process to determine the target area containing the best focus position. The second rotation module is used to control the motor to rotate to a preset starting position within the target area, and to step to the current frame position in a direction toward the interior of the target area with a second step length; the second step length is less than the first step length; The first acquisition module is used to acquire a first sharpness value of the current frame image at the current frame position and a second sharpness value of the previous frame image at the previous frame position; the previous frame position is the position of the motor before reaching the current frame position; The first calculation module is used to calculate the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, as the current error term; The second acquisition module is used to acquire the sharpness deviation values ​​between the two frames at the previous two frame positions and the three frames at the previous three frame positions, and use them as the first historical error item and the second historical error item, respectively. The second calculation module is used to input the current error term, the first historical error term and the second historical error term into the proportional-integral-derivative controller to calculate the target rotation angle and target rotation direction of the motor in the next step. The condition module is used to control the motor to rotate to the next position according to the target rotation angle and target rotation direction, and return to the step of obtaining the first sharpness value of the current frame image at the current frame position, until the motor's motion state near the optimal focus position meets the preset autofocus exit condition, and then focus is stopped.

[0015] 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.

[0016] 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.

[0017] 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.

[0018] The technical solution provided by this invention may include the following beneficial effects: The autofocus method provided by this invention first rapidly traverses and locks onto the target area with a first step size, avoiding a full-range search and significantly shortening the coarse search time. Then, it uses a smaller second step size to finely advance within the target area, preventing overshooting of the peak value. Sharpness deviation is calculated in the logarithmic domain, allowing the error signal to more accurately reflect the true trend of change. A proportional-integral-derivative controller comprehensively utilizes current and historical error terms for weighted calculation, ensuring that the rotation angle of the motor at each step is dynamically determined in real-time based on the actual trend of sharpness change, rather than using a fixed step size. This allows the motor speed to smoothly decrease as it approaches the focus point, balancing speed and accuracy. Finally, the timing of exiting focus is determined by detecting the reciprocating oscillation state of the motor between micro-step positions near the peak value, ensuring accurate and reliable determination of focus completion. Attached Figure Description

[0019] 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.

[0020] 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 third process of the autofocus method according to an embodiment of the present invention; Figure 5 This is a schematic diagram of the change curve of image clarity as the motor rotates throughout the entire motor operation state according to an embodiment of the present invention. Figure 6 This is a schematic diagram of the motor rotation angle according to an embodiment of the present invention; Figure 7 This is a flowchart illustrating the process of determining whether the motor has reached the focusing state 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

[0021] 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.

[0022] 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.

[0023] 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.

[0024] 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 system, as exemplarily shown, includes an endoscope handheld unit 101, an insertion tube 102, and an image processing host 103. The endoscope handheld unit 101 is connected to the image processing host 103 via the insertion tube 102, and the image processing host 103 is connected to a display 104 via a video interface.

[0025] The endoscope handheld unit 101 houses a motor drive module and an imaging unit, which includes a lens assembly and an image sensor. The motor drive module is connected to the lens assembly or image sensor and drives the lens assembly or image sensor to move back and forth along the optical axis to change the focus position. The image sensor acquires image data of the target tissue and transmits the image data to the image processing host 103 via a signal transmission line within the insertion tube 102.

[0026] The image processing host 103 includes a sharpness calculation module and a proportional-integral-derivative (PID) control module. The sharpness calculation module receives image data acquired by the image sensor and calculates the sharpness value of the current frame. The PID control module is connected to both the sharpness calculation module and the motor drive module. Based on the sharpness deviation between the current frame and historical frames, it calculates the target rotation angle and direction of the motor according to the PID control law and sends a control command to the motor drive module. The motor drive module responds to this control command, driving the lens assembly or image sensor to move to the target position.

[0027] It should be noted that, Figure 1 The types of endoscopic devices shown are merely illustrative. 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. 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. This embodiment of the invention does not impose any limitations on these aspects.

[0028] 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 focusing 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.

[0029] 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.

[0030] This embodiment provides an autofocus method that can be used in the image processing host 103 of the aforementioned endoscopic imaging system. 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: Step S201: Control the motor to rotate along the first direction with the first step length, and acquire the image clarity at each position in real time during the rotation to determine the target area containing the optimal focus position.

[0031] During autofocus, the motor first rotates in a preset direction (the first direction) with a large, fixed step size (the first step length). As the motor rotates, the system calculates the image sharpness at each position it reaches. Sharpness measures the richness of detail in an image; a higher sharpness value indicates a clearer image and better focus. By recording the sharpness values ​​corresponding to different motor positions, the system can plot a roughly hill-shaped sharpness variation curve. When the sharpness value continuously increases with motor rotation, it indicates that the motor is gradually approaching the optimal focus position; when the sharpness value changes from increasing to decreasing, it indicates that the motor has passed the optimal focus position. Based on this characteristic, the system can determine a range of high sharpness values ​​during motor rotation. This range contains the position with the highest sharpness, which is the approximate area of ​​the optimal focus position. Further finer focusing operations will then be performed within this area. The larger first step length allows the motor to quickly traverse the search range, reducing search time.

[0032] Step S202: Control the motor to rotate to the preset starting position within the target area, and step to the current frame position with a second step length in the direction toward the interior of the target area.

[0033] The second step length is smaller than the first step length.

[0034] After identifying the target area containing the optimal focus position, the system first adjusts the motor to a preset starting position within that target area. This preset starting position is located on the side of the target area near the edge, typically where the sharpness begins to decrease as detected in step S201. Subsequently, the motor gradually rotates to the current frame position in a direction pointing inwards from the target area with smaller step sizes (i.e., the second step size). Because the second step size is smaller than the first step size, the motor rotates at smaller angles with each step within the target area, resulting in more precise movement. Using smaller step sizes for fine searching avoids directly overshooting the optimal focus position due to excessively large step sizes, thus enabling the acquisition of more and more accurate sharpness sample data near the optimal focus position, providing a reliable data foundation for subsequent accurate calculations.

[0035] Step S203: Obtain the first sharpness value of the current frame image at the current frame position and the second sharpness value of the previous frame image at the previous frame position.

[0036] The previous frame position is the position the motor was in before reaching the current frame position.

[0037] After the motor moves to the current frame position with a second step size, the imaging unit acquires a frame image at that position. The system calculates the sharpness value of this frame image and records it as the first sharpness value. Simultaneously, the system reads the sharpness value corresponding to the image acquired and saved by the motor at the previous step position and records it as the second sharpness value. In this paper, "frame" refers to the image data obtained in a single imaging operation; each motor position corresponds to one frame image. Since the motor is in motion, a frame image is acquired each time it reaches a new position. The "previous frame" refers to the image acquired by the motor at the step position preceding the current frame position. It should be noted that the "previous frame position" and the "current frame position" are two adjacent motor positions, separated by a rotation angle of the second step size. After obtaining the sharpness values ​​at these two positions, the trend of sharpness change can be determined by comparing their differences: if the current frame sharpness is higher than the previous frame, it indicates that the motor is moving towards the optimal focusing position; conversely, it indicates that the motor has passed the optimal focusing position.

[0038] Step S204: Calculate the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, and use it as the current error term.

[0039] After obtaining the sharpness values ​​of the current frame position and the previous frame position, the system calculates the deviation between the two sharpness values ​​in the logarithmic domain. The reason for using the logarithmic domain instead of directly calculating the linear domain difference is that the image sharpness value changes relatively smoothly near the optimal focus position, while changing drastically far from the optimal focus position. Directly using the linear difference would lead to excessively high sensitivity when far from the focus and excessively low sensitivity when close to the focus. Logarithmic operations can compress the non-linear changes in sharpness values ​​within the dynamic range into approximately linear changes, making subsequent control calculations more accurate and stable. This current error term reflects the magnitude and direction of the change in sharpness of the motor at the current frame position relative to the previous frame position (positive values ​​indicate an increase, negative values ​​indicate a decrease), and is the core input signal for the proportional-integral-derivative controller to determine the next action of the motor.

[0040] Step S205: Obtain the sharpness deviation values ​​of the two frames at the previous two frame positions and the three frames at the previous three frame positions, and use them as the first historical error term and the second historical error term, respectively.

[0041] In addition to the sharpness deviation between the current frame and the previous frame, the system also needs to acquire sharpness change information from earlier times to comprehensively grasp the trend of sharpness changes. Specifically, the system acquires the logarithmic deviation of the sharpness values ​​of the image two frames ago at the previous frame position and the image two frames ago at the previous two frame positions, denoted as the first historical error term; simultaneously, it acquires the logarithmic deviation of the sharpness values ​​of the image two frames ago at the previous two frame positions and the image three frames ago at the previous three frame positions, denoted as the second historical error term. In actual execution, when the system first enters the precise focusing stage, since sufficient historical frame data has not yet been accumulated, the first and second historical error terms can be preset to initial values, and then replaced with actual calculated values ​​after the system has acquired a sufficient number of frames. These two historical errors reflect the rate and acceleration of sharpness change at earlier times, and together with the current error term, they constitute an error sequence, enabling the proportional-integral-derivative controller to comprehensively grasp the rate of change of sharpness and the rate of change of the rate of change, knowing how much the current deviation is and whether the deviation is intensifying or slowing down, thereby making more accurate predictions and adjustments.

[0042] Step S206: Input the current error term, the first historical error term, and the second historical error term into the proportional-integral-derivative controller to calculate the target rotation angle and target rotation direction of the motor in the next step.

[0043] The Proportional-Integral-Derivative (PID) controller is a classic control algorithm. The current error term, the first historical error term, and the second historical error term obtained in steps S204 and S205 are input into the PID controller. The PID controller performs a weighted sum of the three error terms according to preset proportional, integral, and derivative coefficients: the proportional term is adjusted in real-time based on the magnitude of the current error, with larger errors resulting in larger adjustments; the integral term is adjusted based on the accumulated error to eliminate long-term steady-state deviations; and the derivative term is adjusted predictively based on the rate of error change to suppress system oscillations in advance. The weighted sum of these three terms represents the target rotation angle of the motor for the next step, with the sign of the target rotation angle corresponding to the target rotation direction of the motor. In this way, the rotation angle of the motor at each step is not predetermined but calculated in real-time based on the trend of sharpness changes, making the entire focusing process smoother and more efficient.

[0044] Step S207: Control the motor to rotate to the next position according to the target rotation angle and target rotation direction, and return to the step of obtaining the first sharpness value of the current frame image at the current frame position, until the motion state of the motor near the optimal focus position meets the preset autofocus exit condition, and stop focusing.

[0045] The system sends the target rotation angle and target rotation direction calculated in step S206 to the motor drive module, and the motor rotates to the new position according to the instruction. After reaching the new position, the system returns to execute step S203 and subsequent steps, acquiring an image again at the new position, calculating the sharpness deviation, and calculating the next rotation angle through the proportional-integral-derivative controller, and so on. This cycle continues until the system detects that the motor's movement near the optimal focus position meets the preset autofocus exit condition, at which point focusing stops. The preset autofocus exit condition is used to determine whether the motor has stabilized at the optimal focus position. When the motor oscillates back and forth between adjacent micro-step positions near the optimal focus position, it indicates that the motor has sufficiently converged to the vicinity of the peak value and cannot be further optimized. At this time, the system determines that focusing is complete and stops the cycle, keeping the motor in the current position to maintain the focus state.

[0046] The autofocus method provided in this embodiment first rapidly traverses and locks onto the target area with a first step size, avoiding a full-range search and significantly shortening the coarse search time. Then, it uses a smaller second step size to finely advance within the target area, preventing overshooting of the peak value. Sharpness deviation is calculated in the logarithmic domain, making the error signal more accurately reflect the true trend of change. The proportional-integral-derivative controller comprehensively utilizes current and historical error terms for weighted calculation, ensuring that the rotation angle of the motor at each step is dynamically determined in real time according to the actual trend of sharpness change, rather than using a fixed step size. This allows the motor speed to smoothly decrease as it approaches the focus point, balancing speed and accuracy. Finally, the timing of exiting focus is determined by detecting the reciprocating oscillation state of the motor between micro-step positions near the peak value, ensuring accurate and reliable determination of focus completion.

[0047] This embodiment provides an autofocus method that can be used in the image processing host 103 of the aforementioned endoscopic imaging system. 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: Step S301, parameter initialization.

[0048] In response to the autofocus activation command, the system loads preset focusing parameters, including a first step length for coarse searching, a second step length for fine searching, an increase threshold for determining whether sharpness has entered the rising phase, a preset value for the number of consecutive decreases to determine whether sharpness has exceeded the peak, and the proportional, integral, and derivative coefficients of the proportional-integral-derivative controller. These parameters can be automatically loaded by the system when the autofocus function is activated, or they can be adapted and adjusted according to the endoscope lens model, image sensor resolution, and clinical application scenario. In practical applications, the second step length is usually set to the minimum rotation unit of the motor, the first step length is set to an integer multiple of this minimum rotation unit, and the increase threshold can be set to a constant greater than 1. After parameter initialization, the system controls the motor to begin the coarse searching process in step S302. In addition, before executing step S302, the system can first control the motor to rotate to the initial position to ensure that the starting point of the searching process is determined. This initial position can be the extreme position near one end of the motor's stroke, or the position recorded when the last focusing was completed.

[0049] Step S302: Control the motor to rotate along the first direction with the first step length, and acquire the image clarity at each position in real time during the rotation to determine the target area containing the optimal focus position.

[0050] In one optional implementation, during autofocus, the motor is first controlled to rotate in a preset first direction with a large step size from the initial position. After each step, the sharpness value of the current frame image at the corresponding current position is calculated, thus traversing the search range. During the stepping process, the system compares the sharpness value of the current frame image with that of the previous frame image in real time. When the growth rate of the sharpness value of the current frame image relative to the sharpness value of the previous frame image is detected to be greater than a preset increase threshold, it indicates that the sharpness has entered a significant increase phase from a relatively flat state. At this time, the current motor position is recorded as the first sampling point, which marks the beginning of the motor entering the focusing area. Then, the motor continues to be controlled to rotate in the first direction with the same step size from the first sampling point. When the growth rate of the sharpness value relative to the sharpness value of the previous frame image is detected to be less than zero (from positive to negative) and the number of consecutive decreases reaches a preset number, it indicates that the motor has crossed the peak of the sharpness curve and entered the decrease phase. At this time, the current motor position is recorded as the second sampling point, which marks the beginning of the motor leaving the focusing area. The target region containing the optimal focus position can be determined based on the positions of the first sampling point and the second sampling point. The first sampling point is located on the side of the target region closer to the start end, and the second sampling point is located on the side of the target region closer to the end end. The optimal focus position is located between the first and second sampling points. The purpose of the larger first step is to quickly traverse the search range and shorten the coarse search time. The settings for the rising threshold and the number of consecutive falling steps are used to avoid misjudgments caused by image noise.

[0051] Step S303: Control the motor to rotate to the preset starting position within the target area, and step to the current frame position with a second step length in the direction toward the interior of the target area.

[0052] The second step size is smaller than the first step size. The preset starting position is the location of the second sampling point. The direction towards the interior of the target area is the direction from the second sampling point to the first sampling point.

[0053] After identifying the target area containing the optimal focus position, the system first adjusts the motor to a preset starting position within that target area, which is the location of the second sampling point recorded in step S302. Then, the motor gradually rotates to the current frame position with a smaller second step size along the direction from the second sampling point to the first sampling point. The second sampling point is chosen as the preset starting position because it is located on the side of the target area near the end, and the sharpness has already begun to decrease. Stepping from the second sampling point along the direction towards the first sampling point means that the motor is moving towards the interior of the target area, i.e., towards the optimal focus position. The second step size is set to a value smaller than the first step size because the optimal focus position is already close within the target area, requiring a finer step size to avoid directly exceeding the peak value due to excessively large steps. This also allows for obtaining higher density sharpness sampling data, providing a reliable basis for subsequent accurate calculations.

[0054] Step S304: Obtain the first sharpness value of the current frame image at the current frame position and the second sharpness value of the previous frame image at the previous frame position.

[0055] The previous frame position is the position the motor was in before reaching the current frame position.

[0056] Please see details Figure 2 Step S203 of the illustrated embodiment will not be described again here.

[0057] Step S305: Calculate the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, and use it as the current error term.

[0058] In one alternative implementation, the common logarithm of the first and second sharpness values ​​is taken respectively, and then the two are subtracted to obtain the current error term. This current error term can be calculated using the following formula. :

[0059] in, This represents the first resolution value. This indicates the second resolution value.

[0060] Step S306: Obtain the sharpness deviation values ​​of the two frames at the previous two frame positions and the three frames at the previous three frame positions, and use them as the first historical error term and the second historical error term, respectively.

[0061] Please see details Figure 2 Step S205 of the illustrated embodiment will not be described again here.

[0062] Step S307: Input the current error term, the first historical error term, and the second historical error term into the proportional-integral-derivative controller to calculate the target rotation angle and target rotation direction of the motor for the next step.

[0063] The current error term obtained in step S305 and the first and second historical error terms obtained in step S306 are input into the proportional-integral-derivative controller for weighted calculation. The result of the weighted sum of the three is the target rotation angle of the motor in the next step. The positive or negative sign of the target rotation angle corresponds to the target rotation direction of the motor.

[0064] In one alternative implementation, the target rotation angle is calculated using the following formula. :

[0065] in, This represents the proportionality coefficient. Represents the integral coefficient. Represents the differential coefficient. This represents the deviation of the sharpness value between the previous frame and the two frames above it in the logarithmic domain. This represents the deviation of the sharpness values ​​between the previous two frames and the previous three frames in the logarithmic domain.

[0066] Step S308: Control the motor to rotate to the next position according to the target rotation angle and target rotation direction, and return to the step of obtaining the first sharpness value of the current frame image at the current frame position, until the motion state of the motor near the optimal focus position meets the preset autofocus exit condition, and stop focusing.

[0067] The system sends the target rotation angle and target rotation direction calculated in step S307 to the motor drive module to generate a command. The motor rotates to the new position according to the command. After reaching the new position, the system returns to execute step S304 and subsequent steps, repeating this cycle to form a closed-loop feedback control process. This cycle continues until the system detects that the motor's movement near the optimal focusing position meets the preset autofocus exit condition and stops.

[0068] In one optional implementation, when detecting whether the motor's movement near the optimal focusing position meets the preset autofocus exit condition, the rotational state of the motor between three adjacent micro-step positions near the optimal focusing position is first detected; these three adjacent micro-step positions include a first position, a second position, and a third position; the second position is located between the first position and the third position. When the motor oscillates back and forth between the first position, the second position, and the third position, it indicates that the motor has sufficiently converged to the vicinity of the peak value and cannot approach it further. At this time, the number of times the motor passes through the second position is recorded. When this number reaches a preset threshold, it is determined that the autofocus exit condition is met, indicating that focusing is complete, and the loop stops. The preset threshold is set to ensure that the motor has stabilized in the oscillation state rather than accidentally crossing it, and can usually be set to an appropriate value based on actual debugging results. After exiting, the motor stays in the current position, and the endoscope system maintains the focusing state.

[0069] The autofocus method provided in this embodiment ensures the consistency of the search starting point and the compatibility of the algorithm with different endoscopic devices by preset parameters and determining the initial position. It accurately locks the boundary of the target area by using both the rising threshold and the number of consecutive falling steps, without the need to pre-determine the motor direction and resisting noise interference. It achieves a smooth transition from coarse to fine search by using the coarse search endpoint as the fine search starting point and stepping along the direction pointing into the area. By acquiring the sharpness data of the current and historical frames respectively, it provides the controller with a complete error sequence including the rate of change and acceleration. By mapping the logarithmic domain, it makes the error signal have consistent sensitivity in different sharpness intervals. The proportional term ensures fast convergence, the integral term eliminates steady-state deviation, and the differential term suppresses overshoot. The three factors work together to make the motor step size continuously adjusted in real time according to the sharpness change trend. It continuously approaches the focus through closed-loop loop and uses the number of reciprocating oscillations of the motor between three adjacent micro-step positions to determine stable convergence. The exit mechanism does not depend on the absolute value of sharpness and has strong versatility.

[0070] 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.

[0071] Figure 4 This is a flowchart of an autofocus method according to an embodiment of the present invention. Figure 5 This is a schematic diagram illustrating the change in image clarity as the motor rotates throughout the entire motor operation process according to an embodiment of the present invention. Figure 4 As shown, the autofocus method includes the following steps: S1: Set the motor to move in a fixed large step (first step length) to perform a rough search for the focusing position (optimal focusing position) and determine the approximate area of ​​the focus point (target area), such as... Figure 5 The positions of point A (first sampling point) and point B (second sampling point).

[0072] S2: Perform precise focusing between points A and B based on proportional-integral-differential methods. First, rotate the motor to the position corresponding to point B and obtain the sharpness value of the image at point B. (Second resolution value).

[0073] S3: If point A is determined to be the positive direction, then rotate from point B along the direction of point A in fixed small steps. Assuming the position is reached, obtain the sharpness value at the corresponding position of point C. (First resolution value). It should be noted that in the above process, when selecting point C, it is assumed that the position of point A relative to point B is in the positive direction. Then point C can be in either the positive or negative direction.

[0074] S4: Calculate over the logarithmic field and Current error term ( Figure 4 The middle part represents the third error), as shown in the following formula:

[0075] S5: Based on the obtained current error term Perform proportional-integral-derivative (PID) calculations to obtain the motor rotation position (including the next motor rotation direction and step size) for the next frame image, as shown in the following formula:

[0076] in, This represents the proportionality coefficient. Represents the integral coefficient. Represents the differential coefficient. This represents the deviation of the sharpness value between the previous frame and the two frames above it in the logarithmic domain. Figure 4 The middle part represents the second error, corresponding to the first historical error term. This represents the deviation of the sharpness values ​​between the previous two frames and the previous three frames in the logarithmic domain. Figure 4 The first error is represented in the middle, corresponding to the second historical error item. Since the motor enters the precise focusing state at point B, the sharpness value of the current frame image represents the sharpness of the image at point C, and the sharpness value of the previous frame image represents the sharpness of the image at point B. The sharpness values ​​of the previous two frames and the previous three frames can be preset to initial values ​​or recorded and recalled during the loop.

[0077] S6: Return to step S3 to obtain the new position C, and repeat steps S3-S6 until the motor exits autofocus. In the autofocus algorithm process of proportional-integral-derivative control, it is impossible to determine when the image sharpness has reached its maximum value, which means that it is impossible to determine when the optimal focus position is reached based on sharpness. Therefore, a special focus exit algorithm needs to be designed. Figure 6 This is a schematic diagram of the motor rotation angle according to an embodiment of the present invention. The motor has a minimum rotation unit during its rotation, such as... Figure 5 As shown, the distance from site-1 to site-2 represents the minimum rotation angle of the motor. The rotation angle from site-1 to site-2 is the same as the rotation angle from site-2 to site-3. When the motor is about to reach the final focus point, there are two rotation methods: approaching from site-1 or approaching from site-3. Assuming the motor approaches from site-1, the state analysis is as follows: When the motor approaches from site-1, the proportional-integral-derivative (PI-DI) control calculates the parameters and finds that the sharpness of site-2 is greater than that of site-1, meaning the motor needs to rotate to site-3. When rotating to site-3, the sharpness of site-3 is less than that of site-2, the parameter calculated by PI-DI is negative, the motor rotates in a different direction, and then rotates back to site-2, and then back to site-1. In this approaching state, the final rotation state of the motor is: site-1>>site-2>>site-3>>site-2>>site-1>>site-2>>site-3…… When the motor approaches from site-3, based on the above analysis, the final rotation state of the motor is: site-3>>site-2>>site-1>>site-2>>site-3>>site-2>>site-1…… Assuming the motor rotates to site-1 on the first rotation, to site-2 on the second rotation, and so on, the relationship between the number of motor rotations and site-1, site-2, and site-3 can be obtained, as shown in the table below: Table 1: Number of Motor Rotations.

[0078]

[0079] As can be seen from the table above, the motor returns to the site-2 position every time it rotates. This means that the absolute position of the motor is the same every time it rotates, and the image sharpness data is also the same at this time. This feature can be used to determine whether the motor has reached the focus state. Figure 7This is a flowchart illustrating the process of determining whether a motor has reached the focusing state according to an embodiment of the present invention. When the motor rotates continuously, the number of motor rotations is recorded, and it is determined whether the current rotation is an odd number. If so (the current rotation is an odd number), it is determined whether the current motor coordinates are the same as the previous motor coordinates. If not, the counter a is incremented by one; if so, the counter a is set to zero. If not (the current rotation is not an odd number), it is determined whether the current motor coordinates are the same as the previous motor coordinates. If not, the counter b is incremented by one; if so, the counter b is set to zero. Figure 7 In the middle, Th represents the preset number of attempts threshold. Next, the count of counter a is compared with the number of attempts threshold, and the count of counter b is also compared with the number of attempts threshold. When the count of either counter a or counter b is greater than the number of attempts threshold, the autofocus process ends, the autofocus algorithm exits, and focusing is completed; otherwise, the autofocus process continues.

[0080] 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.

[0081] 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 along the first direction by a step length, and to acquire the image clarity at each position in real time during the rotation process, and to determine the target area containing the best focus position. The second rotation module 802 is used to control the motor to rotate to a preset starting position within the target area, and to step to the current frame position in a direction toward the interior of the target area with a second step length; the second step length is smaller than the first step length. The first acquisition module 803 is used to acquire a first sharpness value of the current frame image at the current frame position and a second sharpness value of the previous frame image at the previous frame position; the previous frame position is the position of the motor before reaching the current frame position; The first calculation module 804 is used to calculate the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, as the current error term; The second acquisition module 805 is used to acquire the sharpness deviation values ​​of the two frames at the position of the two frames above and the three frames at the position of the three frames above, and use them as the first historical error item and the second historical error item, respectively. The second calculation module 806 is used to input the current error term, the first historical error term, and the second historical error term into the proportional-integral-derivative controller to calculate the target rotation angle and target rotation direction of the motor in the next step. The condition module 807 is used to control the motor to rotate to the next position according to the target rotation angle and target rotation direction, and return to the step of obtaining the first sharpness value of the current frame image at the current frame position, until the motor's motion state near the optimal focus position meets the preset autofocus exit condition, and then stop focusing.

[0082] In an optional implementation, the first calculation module is further configured to: calculate the current error term using the following formula. :

[0083] in, This represents the first resolution value. This indicates the second resolution value.

[0084] In an optional implementation, the second calculation module is further configured to: calculate the target rotation angle using the following formula. :

[0085] in, This represents the proportionality coefficient. Represents the integral coefficient. Denotes the differential coefficient. This represents the deviation of the sharpness value between the previous frame and the two frames above it in the logarithmic domain. This represents the deviation of the sharpness values ​​between the previous two frames and the previous three frames in the logarithmic domain.

[0086] In an optional implementation, the first rotation module is further configured to: control the motor to rotate stepwise along the first direction from the initial position by the step length, and calculate the sharpness value of the current frame image after each step; when it is detected that the growth rate of the sharpness value relative to the sharpness value of the previous frame image is greater than a preset rise threshold, record the current motor position as a first sampling point; control the motor to rotate stepwise along the first direction from the first sampling point by the step length, and when it is detected that the growth rate of the sharpness value relative to the sharpness value of the previous frame image is less than zero and the number of consecutive drops reaches a preset number, record the current motor position as a second sampling point; determine the target area based on the position of the first sampling point and the position of the second sampling point; wherein the first sampling point is located on the side of the target area closer to the starting end, the second sampling point is located on the side of the target area closer to the ending end, and the optimal focus position is located between the first sampling point and the second sampling point.

[0087] In one optional implementation, the preset starting position is the location of the second sampling point; the direction toward the interior of the target area is the direction from the second sampling point to the first sampling point.

[0088] In an optional implementation, the condition module is further configured to: detect the rotational state of the motor between three adjacent micro-step positions near the optimal focus position; the three adjacent micro-step positions include a first position, a second position, and a third position; the second position is located between the first position and the third position; when the motor oscillates back and forth between the first position, the second position, and the third position, record the number of times the motor passes through the second position; when the number of times reaches a preset threshold, determine that the autofocus exit condition is met.

[0089] 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.

[0090] Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention.

[0091] 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.

[0092] 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 9Electronic 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.

[0093] 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.

[0094] Figure 9 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.

[0095] 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.

[0096] 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.

[0097] 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: The motor is controlled to rotate in the first direction by the first step length, and the image clarity at each position is acquired in real time during the rotation to determine the target area containing the optimal focus position; The motor is controlled to rotate to a preset starting position within the target area, and then moves to the current frame position in a direction toward the interior of the target area with a second step length; the second step length is less than the first step length. Obtain the first sharpness value of the current frame image at the current frame position, and the second sharpness value of the previous frame image at the previous frame position; the previous frame position is the position of the motor before reaching the current frame position; Calculate the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, and use it as the current error term; The sharpness deviation values ​​of the two frames at the previous two frame positions and the three frames at the previous three frame positions are obtained and used as the first historical error term and the second historical error term, respectively. The current error term, the first historical error term, and the second historical error term are input into the proportional-integral-derivative controller to calculate the target rotation angle and target rotation direction of the motor for the next step. The process of controlling the motor to rotate to the next position according to the target rotation angle and target rotation direction, and returning to obtain the first sharpness value of the current frame image at the current frame position, continues until the motion state of the motor near the optimal focus position meets the preset autofocus exit condition, at which point focusing stops.

2. The method according to claim 1, characterized in that, The step of calculating the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, as the current error term, includes: The current error term is calculated using the following formula. : in, This represents the first resolution value. This indicates the second resolution value.

3. The method according to claim 2, characterized in that, The calculation yields the target rotation angle of the motor for the next step, including: The target rotation angle is calculated using the following formula. : in, This represents the proportionality coefficient. Represents the integral coefficient. Represents the differential coefficient. This represents the deviation of the sharpness value between the previous frame and the two frames above it in the logarithmic domain. This represents the deviation of the sharpness values ​​between the previous two frames and the previous three frames in the logarithmic domain.

4. The method according to any one of claims 1 to 3, characterized in that, Determining the target area containing the optimal focus position includes: The motor is controlled to rotate step by step along the first direction from the initial position with the first step length, and the sharpness value of the current frame image is calculated after each step. When the rate of increase of the sharpness value relative to the sharpness value of the previous frame image is detected to be greater than the preset increase threshold, the current motor position is recorded as the first sampling point; The motor is controlled to rotate in the first direction at the first step length from the first sampling point. When the growth rate of the sharpness value relative to the sharpness value of the previous frame image is less than zero and the number of consecutive decreases reaches a preset number, the current motor position is recorded as the second sampling point. The target area is determined based on the positions of the first sampling point and the second sampling point; wherein the first sampling point is located on the side of the target area closer to the starting end, the second sampling point is located on the side of the target area closer to the ending end, and the optimal focus position is located between the first sampling point and the second sampling point.

5. The method according to claim 4, characterized in that, The preset starting position is the location of the second sampling point; the direction toward the interior of the target area is the direction from the second sampling point to the first sampling point.

6. The method according to any one of claims 1 to 3, characterized in that, The detection that the motor's movement near the optimal focus position meets preset autofocus exit conditions includes: The rotational state of the detection motor is between three adjacent micro-step positions near the optimal focusing position; the three adjacent micro-step positions include a first position, a second position, and a third position; the second position is located between the first position and the third position. When the motor oscillates back and forth between the first position, the second position and the third position, record the number of times the motor passes through the second position; When the number of times reaches a preset threshold, it is determined that the autofocus exit condition is met.

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 along the first direction by a step length, and to acquire the image clarity at each position in real time during the rotation process to determine the target area containing the best focus position. The second rotation module is used to control the motor to rotate to a preset starting position within the target area, and to step to the current frame position in a direction toward the interior of the target area with a second step length; the second step length is less than the first step length; The first acquisition module is used to acquire a first sharpness value of the current frame image at the current frame position and a second sharpness value of the previous frame image at the previous frame position; the previous frame position is the position of the motor before reaching the current frame position; The first calculation module is used to calculate the deviation between the first sharpness value and the second sharpness value in the logarithmic domain, as the current error term; The second acquisition module is used to acquire the sharpness deviation values ​​between the two frames at the previous two frame positions and the three frames at the previous three frame positions, and use them as the first historical error item and the second historical error item, respectively. The second calculation module is used to input the current error term, the first historical error term and the second historical error term into the proportional-integral-derivative controller to calculate the target rotation angle and target rotation direction of the motor in the next step. The condition module is used to control the motor to rotate to the next position according to the target rotation angle and target rotation direction, and return to the step of obtaining the first sharpness value of the current frame image at the current frame position, until the motor's motion state near the optimal focus position meets the preset autofocus exit condition, and then focus is stopped.

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.