Disclosure of Invention
The technical problems to be solved by the invention are as follows: aiming at the problems in the prior art, the invention provides a camera focusing control method and device based on FPGA, optimizes the automatic focusing of a microscope by combining the parallel pipeline hardware acceleration of FPGA and the control of an image acquisition device, and has the advantages of high focusing speed and high processing efficiency.
In order to solve the technical problems, the invention adopts the technical scheme that:
a method of camera focus control based on an FPGA, comprising:
1) initializing and setting the moving step length, the initial current moving step length and the moving range of each wheel;
2) controlling a motor of the image acquisition device to move the camera to the current position according to the moving step length;
3) controlling and caching a current position image shot by a camera of the image acquisition device;
4) sequentially outputting a plurality of sampling areas of each line of the cached current position image to parallel pipelines, wherein the number of the parallel pipelines corresponds to the number of lines of the sampling areas in the current position image, each parallel pipeline is used for performing frequency domain transformation on the plurality of sampling areas sequentially input into the same line in a pipeline mode, accumulating the frequency domain transformation coefficients, and summing the final accumulation results of the frequency domain transformation coefficients of the plurality of parallel pipelines to obtain a definition function value of the current position image;
5) judging whether the current position exceeds the moving range, and if not, skipping to execute the step 2); otherwise, comparing the definition function values of the images at the positions obtained under the moving step length of the round, and finding out the position with the best definition; judging whether a smaller moving step length still exists, if the smaller moving step length still exists, selecting the smaller moving step length as a new current moving step length, determining the moving range of the new current moving step length, and skipping to execute the step 2); and judging that no smaller moving step length exists, and outputting the position with the best definition found out under the moving step length of the current round as a focusing result.
An apparatus for FPGA-based camera focus control, comprising:
the parameter configuration module is used for initializing and setting the moving step length, the initial current moving step length and the moving range of each wheel;
the focusing control module comprises a motor control interface which is used for being connected with a motor of the image acquisition device and is used for controlling the motor of the image acquisition device to move the camera to the current position according to the moving step length through the motor control interface;
the image acquisition module comprises an image acquisition interface which is connected with a camera of the image acquisition device and is used for acquiring and caching the current position image shot by the camera through the image acquisition interface;
the image processing module is used for sequentially outputting a plurality of sampling areas of each line of the cached current position image to parallel pipelines, the number of the parallel pipelines corresponds to the number of lines of the sampling areas in the current position image, each parallel pipeline is used for performing frequency domain transformation on the plurality of sampling areas sequentially input to the same line in a pipeline mode, accumulating the frequency domain transformation coefficients, and summing the final accumulation results of the frequency domain transformation coefficients of the plurality of parallel pipelines to obtain a definition function value of the current position image;
the comparison and judgment module is used for judging whether the current position exceeds the moving range or not, and if the current position does not exceed the moving range, the focusing control module is called to move the camera to a new current position according to a new current moving step length; otherwise, comparing the definition function values of the images at the positions obtained under the moving step length of the round, and finding out the position with the best definition; judging whether a smaller moving step length still exists, if the smaller moving step length still exists, selecting the smaller moving step length as a new current moving step length, determining the moving range of the new current moving step length, and calling a focusing control module to move the camera to a new current position according to the new current moving step length; and judging that no smaller moving step length exists, and outputting the position with the best definition found out under the moving step length of the current round as a focusing result.
Optionally, the parameter configuration module further includes a light source control interface connected to the light source control end of the image acquisition device, and the parameter configuration module is further configured to configure the control parameters of the light source of the image acquisition device through the light source control interface.
Optionally, the image processing module includes an image collecting unit, a plurality of parallel pipelines and an accumulating unit, the image collecting unit is configured to output a plurality of sampling regions in each line of the cached current position image to the parallel pipelines in sequence, the number of the parallel pipelines corresponds to the number of lines of the sampling regions in the current position image, each parallel pipeline is configured to accumulate frequency domain transform coefficients after performing frequency domain transform on a plurality of sampling regions in the same line in sequence in a pipeline manner, and the accumulating unit is configured to sum up final accumulation results of the frequency domain transform coefficients of the plurality of parallel pipelines to obtain a sharpness function value of the current position image.
Optionally, the parallel pipeline comprises: the line-frequency domain converter is used for performing frequency domain conversion aiming at the input sampling region; the buffer is used for buffering the frequency domain transformation result of each sampling region; the column frequency domain converter is used for performing column frequency domain conversion on the cached results of the line frequency domain conversion of each sampling region to obtain frequency domain conversion coefficients; the accumulation unit comprises a preceding-stage accumulator and a subsequent-stage accumulator, the preceding-stage accumulator is used for accumulating and calculating the frequency domain transformation coefficients obtained by the various rows of frequency domain converters to obtain the frequency domain transformation coefficient accumulation result of each row of sampling areas, and the subsequent-stage accumulator is used for accumulating and calculating the frequency domain transformation coefficient accumulation result of each row of sampling areas to obtain the accumulation results of all the sampling areas.
Optionally, the image processing module includes three parallel pipelines L1, L2, and L3, the image capturing unit uniformly extracts 9 sampling regions of a specified pixel size from the cached current position image, where the first row of sampling regions is P1, P4, and P7, the second row of sampling regions is P2, P5, and P8, and the third row of sampling regions is P3, P6, and P9, sequentially outputs the first row of sampling regions P1, P4, and P7 of the cached current position image to the parallel pipeline L1, sequentially outputs the second row of sampling regions P2, P5, and P8 of the cached current position image to the parallel pipeline L2, and sequentially outputs the third row of sampling regions P3, P6, and P9 of the cached current position image to the parallel pipeline L3; a line frequency domain converter of the parallel pipeline L1 sequentially performs line frequency domain conversion on first line sampling regions P1, P4 and P7 and buffers the line frequency domain conversion into corresponding buffers, a column frequency domain converter of the parallel pipeline L1 sequentially performs column frequency domain conversion on the buffered first line sampling regions P1, P4 and P7 and sends the results of the column frequency domain conversion into a preceding-stage accumulator, a line frequency domain converter of the parallel pipeline L2 sequentially performs line frequency domain conversion on second line sampling regions P2, P5 and P8 and buffers the results of the column frequency domain conversion into corresponding buffers, and a column frequency domain converter of the parallel pipeline L2 sequentially performs column frequency domain conversion on the buffered second line sampling regions P2, P5 and P8 and sends the results of the column frequency domain conversion into the preceding-stage accumulator; the row frequency domain converter of the parallel pipeline L3 sequentially performs row frequency domain conversion on the third row sampling regions P3, P6 and P9 and caches the third row sampling regions in corresponding caches, and the column frequency domain converter of the parallel pipeline L3 sequentially performs column frequency domain conversion on the cached third row sampling regions P3, P6 and P9 and sends the results of the column frequency domain conversion to the former-stage accumulator; the first-stage accumulator sums up the first columns of sampling areas P1, P2 and P3 to obtain a frequency domain conversion coefficient accumulation result Sum123 and outputs the frequency domain conversion coefficient accumulation result Sum123 to a later-stage accumulator, the second-stage accumulator sums up the second columns of sampling areas P4, P5 and P6 to obtain a frequency domain conversion coefficient accumulation result Sum456 and outputs the frequency domain conversion coefficient accumulation result Sum456 to a later-stage accumulator, the third-stage accumulator sums up the third columns of sampling areas P7, P8 and P9 to obtain a frequency domain conversion coefficient accumulation result Sum789 and outputs the frequency domain conversion coefficient accumulation result Sum789 to the later-stage accumulator, and the later-stage accumulator sums up the frequency domain conversion coefficient accumulation result Sum123, the frequency domain conversion coefficient accumulation result Sum456 and the frequency domain conversion coefficient accumulation result Sum789 to obtain frequency domain conversion coefficients Sum.
Optionally, the specified pixel-sized sampling region refers to a 128x128 pixel-sized sampling region.
Optionally, the parameter configuration module initializes the moving step lengths of the wheels to be arranged from large to small, the initial current moving step length is the largest moving step length, and the moving range is the size of the image along the moving direction.
Optionally, the moving direction is a Z-axis direction.
Optionally, the comparing and determining module determines that the moving range of the new current moving step is [ zi-2,zi+2]Wherein z isiAnd finding out the position with the best definition for the current moving step length of the current round, and obtaining the new current moving step length.
Optionally, when the motor of the image acquisition device is controlled by the focus control module through the motor control interface to move the camera to the current position according to the movement step length, the movement directions of the movement step lengths of two adjacent wheels are opposite.
Compared with the prior art, the invention has the following advantages: the image processing module sequentially outputs a plurality of sampling areas of each line of a cached current position image to parallel pipelines, the number of the parallel pipelines corresponds to the number of lines of the sampling areas in the current position image, each parallel pipeline is used for performing frequency domain transformation on a plurality of sampling areas sequentially input into the same line in a pipeline mode, then performing accumulation, and summing the final accumulation results of the plurality of parallel pipelines to obtain a definition function value of the current position image. The invention optimizes the automatic focusing of the microscope by combining the parallel pipeline hardware acceleration of the FPGA with the control of the image acquisition device, and has the advantages of high focusing speed and high processing efficiency.
Detailed Description
The embodiment provides a camera focus control method based on an FPGA, which comprises the following steps:
1) initializing and setting the moving step length, the initial current moving step length and the moving range of each wheel;
2) controlling a motor of the image acquisition device to move the camera to the current position according to the moving step length;
3) controlling and caching a current position image shot by a camera of the image acquisition device;
4) sequentially outputting a plurality of sampling areas of each line of the cached current position image to parallel pipelines, wherein the number of the parallel pipelines corresponds to the number of lines of the sampling areas in the current position image, each parallel pipeline is used for performing frequency domain transformation on the plurality of sampling areas sequentially input into the same line in a pipeline mode, accumulating the frequency domain transformation coefficients, and summing the final accumulation results of the frequency domain transformation coefficients of the plurality of parallel pipelines to obtain a definition function value of the current position image;
5) judging whether the current position exceeds the moving range, and if not, skipping to execute the step 2); otherwise, comparing the definition function values of the images at the positions obtained under the moving step length of the round, and finding out the position with the best definition; judging whether a smaller moving step length still exists, if the smaller moving step length still exists, selecting the smaller moving step length as a new current moving step length, determining the moving range of the new current moving step length, and skipping to execute the step 2); and judging that no smaller moving step length exists, and outputting the position with the best definition found out under the moving step length of the current round as a focusing result.
As shown in fig. 1, the apparatus for controlling focusing of a camera based on FPGA of the present embodiment includes:
the parameter configuration module is used for initializing and setting the moving step length, the initial current moving step length and the moving range of each wheel;
the focusing control module comprises a motor control interface which is used for being connected with a motor of the image acquisition device and is used for controlling the motor of the image acquisition device to move the camera to the current position according to the moving step length through the motor control interface;
the image acquisition module comprises an image acquisition interface which is connected with a camera of the image acquisition device and is used for acquiring and caching the current position image shot by the camera through the image acquisition interface;
the image processing module is used for sequentially outputting a plurality of sampling areas of each line of the cached current position image to parallel pipelines, the number of the parallel pipelines corresponds to the number of lines of the sampling areas in the current position image, each parallel pipeline is used for performing frequency domain transformation on the plurality of sampling areas sequentially input to the same line in a pipeline mode, accumulating the frequency domain transformation coefficients, and summing the final accumulation results of the frequency domain transformation coefficients of the plurality of parallel pipelines to obtain a definition function value of the current position image;
the comparison and judgment module is used for judging whether the current position exceeds the moving range or not, and if the current position does not exceed the moving range, the focusing control module is called to move the camera to a new current position according to a new current moving step length; otherwise, comparing the definition function values of the images at the positions obtained under the moving step length of the round, and finding out the position with the best definition; judging whether a smaller moving step length still exists, if the smaller moving step length still exists, selecting the smaller moving step length as a new current moving step length, determining the moving range of the new current moving step length, and calling a focusing control module to move the camera to a new current position according to the new current moving step length; and judging that no smaller moving step length exists, and outputting the position with the best definition found out under the moving step length of the current round as a focusing result.
As can be seen from fig. 1, the image acquisition device comprises a motor, a camera and a light source, wherein the motor can move under the control of the FPGA, so as to move the area-array camera and realize the focal length adjustment of the camera and the slide; a camera (area-array camera) for taking an image; the light source is used for polishing the object. In order to realize automatic light supplement by controlling the light source, the parameter configuration module in this embodiment further includes a light source control interface for connecting with the light source control end of the image acquisition device, and the parameter configuration module is further configured to configure the control parameters of the light source of the image acquisition device through the light source control interface.
The image processing module adopts a mode of replacing an original image with a sampling region for calculating the definition function value of the current position image, so that the calculated amount can be effectively reduced, and the calculation efficiency is improved.
In this embodiment, the image processing module includes an image collecting unit (not shown in the figure), a plurality of parallel pipelines, and an accumulating unit, where the image collecting unit is configured to sequentially output a plurality of sampling regions in each line of the cached current position image to the parallel pipelines, the number of the parallel pipelines corresponds to the number of lines of the sampling regions in the current position image, each parallel pipeline is configured to sequentially input a plurality of sampling regions in the same line, perform frequency domain transformation in a pipeline manner, and then accumulate frequency domain transformation coefficients, and the accumulating unit is configured to sum final accumulation results of the frequency domain transformation coefficients of the plurality of parallel pipelines to obtain a sharpness function value of the current position image.
As shown in fig. 2, the parallel pipeline includes: the line-frequency domain converter is used for performing frequency domain conversion aiming at the input sampling region; the buffer is used for buffering the frequency domain transformation result of each sampling region; the column frequency domain converter is used for performing column frequency domain conversion on the cached results of the line frequency domain conversion of each sampling region to obtain frequency domain conversion coefficients; the accumulation unit comprises a preceding-stage accumulator and a subsequent-stage accumulator, the preceding-stage accumulator is used for accumulating and calculating the frequency domain transformation coefficients obtained by the various rows of frequency domain converters to obtain the frequency domain transformation coefficient accumulation result of each row of sampling areas, and the subsequent-stage accumulator is used for accumulating and calculating the frequency domain transformation coefficient accumulation result of each row of sampling areas to obtain the accumulation results of all the sampling areas.
As shown in fig. 2, the image processing module in this embodiment includes three parallel pipelines L1, L2, and L3, the image capturing unit uniformly extracts 9 sampling regions of specified pixel sizes from the cached current position image, where the first row of sampling regions is P1, P4, and P7, the second row of sampling regions is P2, P5, and P8, and the third row of sampling regions is P3, P6, and P9, sequentially outputs the first row of sampling regions P1, P4, and P7 of the cached current position image to the parallel pipeline L1, sequentially outputs the second row of sampling regions P2, P5, and P8 of the cached current position image to the parallel pipeline L2, and sequentially outputs the third row of sampling regions P3, P6, and P9 of the cached current position image to the parallel pipeline L3; a line frequency domain converter of the parallel pipeline L1 sequentially performs line frequency domain conversion on first line sampling regions P1, P4 and P7 and buffers the line frequency domain conversion into corresponding buffers, a column frequency domain converter of the parallel pipeline L1 sequentially performs column frequency domain conversion on the buffered first line sampling regions P1, P4 and P7 and sends the results of the column frequency domain conversion into a preceding-stage accumulator, a line frequency domain converter of the parallel pipeline L2 sequentially performs line frequency domain conversion on second line sampling regions P2, P5 and P8 and buffers the results of the column frequency domain conversion into corresponding buffers, and a column frequency domain converter of the parallel pipeline L2 sequentially performs column frequency domain conversion on the buffered second line sampling regions P2, P5 and P8 and sends the results of the column frequency domain conversion into the preceding-stage accumulator; the row frequency domain converter of the parallel pipeline L3 sequentially performs row frequency domain conversion on the third row sampling regions P3, P6 and P9 and caches the third row sampling regions in corresponding caches, and the column frequency domain converter of the parallel pipeline L3 sequentially performs column frequency domain conversion on the cached third row sampling regions P3, P6 and P9 and sends the results of the column frequency domain conversion to the former-stage accumulator; the first-stage accumulator sums up the first columns of sampling areas P1, P2 and P3 to obtain a frequency domain conversion coefficient accumulation result Sum123 and outputs the frequency domain conversion coefficient accumulation result Sum123 to a later-stage accumulator, the second-stage accumulator sums up the second columns of sampling areas P4, P5 and P6 to obtain a frequency domain conversion coefficient accumulation result Sum456 and outputs the frequency domain conversion coefficient accumulation result Sum456 to a later-stage accumulator, the third-stage accumulator sums up the third columns of sampling areas P7, P8 and P9 to obtain a frequency domain conversion coefficient accumulation result Sum789 and outputs the frequency domain conversion coefficient accumulation result Sum789 to the later-stage accumulator, and the later-stage accumulator sums up the frequency domain conversion coefficient accumulation result Sum123, the frequency domain conversion coefficient accumulation result Sum456 and the frequency domain conversion coefficient accumulation result Sum789 to obtain frequency domain conversion coefficients Sum.
In this embodiment, the sampling region of the specified pixel size refers to a sampling region of 128 × 128 pixels.
In this embodiment, the parameter configuration module initializes and sets the moving step length of each wheel to be arranged from large to small, the initial current moving step length is the largest moving step length, and the moving range is the size of the image along the moving direction.
In this embodiment, the moving direction is the Z-axis direction.
In order to further narrow the moving range of each round, the comparing and determining module in this embodiment determines the moving range of the new current moving step as [ zi-2,zi+2]Wherein z isiAnd finding out the position with the best definition for the current moving step length of the current round, and obtaining the new current moving step length. For example, as shown in fig. 4, if the first round finds the position with the best definition to be 80 and the second round moves by 20 steps, the moving range of the second round move step is determined to be 60,100](ii) a The second round finds out the position with the best definition as 70, the third round moves the step size as 2, then determine the third round moves the step size and moves the range as 66,74]. In FIG. 4, each line frame is a roundAnd moving the moving position of the step length, acquiring an image at the position, calculating a definition function value, and obtaining a gray picture frame as the moving position with the best definition under the step length of the movement.
In order to further improve the moving efficiency of each round, in this embodiment, when the focus control module controls the motor of the image acquisition device to move the camera to the current position according to the moving step length through the motor control interface, the moving directions of the moving step lengths of two adjacent rounds are opposite. For example, as shown in fig. 4, the moving direction is a positive z-axis direction when the moving step of the first round is 40, the moving direction is a negative z-axis direction when the moving step of the second round is 10, and the moving direction is a positive z-axis direction when the moving step of the third round is 2, so that the homing operation of each round of the motor can be avoided, and the next round of the moving initial position can be directly entered. In this embodiment, a total of three wheels move by step size, so that the position of the final found 72 is the final focusing result.
As shown in fig. 5, the device for controlling the focusing of the camera based on the FPGA of the present embodiment is implemented by an FPGA image processing board based on an FPGA chip, and the main working process thereof is as follows: the slide position is kept fixed, and the camera is arranged on a motor platform; the FPGA controls the motor to move according to the step length, so that the focal length of the camera and the slide is changed; after the motor moves every time, the FPGA controls the camera to shoot, image data is collected, image processing is carried out, the definition function value of each image is calculated, and the clearest position is obtained after the current step length movement is finished; then adjusting the step length, and repeating the above processes again; and after the clearest position of the image under the minimum step length is obtained, the focusing of the camera is realized. The main working contents of the FPGA are as follows: the control motor moves, control light source switch, control camera shooting, image data acquisition buffering, image processing, definition comparison etc.. The acquisition module comprises a motor platform, an area-array camera and a light source; the area-array camera is installed on the motor platform and can move along with the movement of the motor, as shown in fig. 6, the specific working steps of the FPGA image processing board include:
1) the parameter configuration module is used for initializing and setting the moving step length, the initial current moving step length and the moving range of each wheel; the parameter configuration module 1 is used for presetting various working parameters of the FPGA, and in the embodiment, the parameter configuration module also comprises light source intensity, motor movement times, image length and width, image processing coordinates and the like;
2) the focusing control module controls a motor of the image acquisition device to move the camera to the current position according to the moving step length through the motor control interface;
3) the image acquisition module acquires and caches a current position image shot by the camera through the image acquisition interface;
4) the image processing module sequentially outputs a plurality of sampling areas of each line of the cached current position image to parallel pipelines, the number of the parallel pipelines corresponds to the number of lines of the sampling areas in the current position image, each parallel pipeline is used for performing frequency domain transformation on the plurality of sampling areas sequentially input into the same line in a pipeline mode, then accumulating the sampling areas, and summing the final accumulation results of the plurality of parallel pipelines to obtain a definition function value of the current position image; in order to give consideration to both definition resolution and FPGA parallel pipeline implementation and represent each position of an image, 9 regions are uniformly selected from a 1920x1200 whole image, the size of each region is 128x128, row-column frequency domain processing calculation is carried out on each region, calculation results of the 9 regions are accumulated finally, the accumulated result of frequency domain transformation coefficients is used as a definition function value, theoretically, the clearer the image is, the richer specific details of the image are, the larger the transformation coefficient value after frequency domain transformation is, the larger the accumulated result of the transformation coefficients is, and conversely, the clearer the value is, the clearer the image is represented.
5) The comparison and judgment module judges whether the current position exceeds the moving range, and if the current position does not exceed the moving range, the comparison and judgment module jumps to execute the focusing control module; otherwise, comparing the definition function values of the images at the positions obtained under the moving step length of the round, and finding out the position with the best definition; judging whether a smaller moving step length still exists, if so, selecting the smaller moving step length as a new current moving step length, determining the moving range of the new current moving step length, and jumping to the execution focusing control module; and judging that no smaller moving step length exists, and outputting the position with the best definition found out under the moving step length of the current round as a focusing result.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-readable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein. The present application is directed to methods, apparatus (systems), and computer program products according to embodiments of the application wherein instructions, which execute via a flowchart and/or a processor of the computer program product, create means for implementing functions specified in the flowchart and/or block diagram block or blocks. These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks. These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
The above description is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments, and all technical solutions belonging to the idea of the present invention belong to the protection scope of the present invention. It should be noted that modifications and embellishments within the scope of the invention may occur to those skilled in the art without departing from the principle of the invention, and are considered to be within the scope of the invention.