Detailed Description
The following description of the embodiments of the present application will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present application, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
The terms "system" and "network" are often used interchangeably herein. The term "and/or" is merely an association relationship describing the associated object, and means that three relationships may exist, for example, a and/or B may mean that a exists alone, while a and B exist together, and B exists alone. In addition, the character "/" herein generally indicates that the front and rear associated objects are an "or" relationship. Further, "a plurality" herein means two or more than two.
The image encoding method provided by the application is used for encoding the original data of the image into a file in an image format, wherein the original data of the image can be video frames extracted from video data or images which are arranged in sequence, and the image decoding method provided by the application is used for decoding the file in the image format into the original data of the image for display.
Referring to fig. 1, fig. 1 is a flowchart of an embodiment of an image encoding method according to the present application, the method includes:
s101, obtaining a current image and determining a reference image detection result of the current image.
Specifically, a current image to be encoded is obtained, and reference image detection is performed on the current image to determine a reference image detection result of the current image. The reference image detection result comprises a reference image and a reference image, wherein the reference image is a completely coded image and is time-sequence before the current image.
In an application mode, after the reference image is referred once, the corresponding use count value of the reference image is accumulated once and cleared after the count threshold is accumulated, and whether the reference image which is completely encoded and can be referred by the current image exists before the current image or not is determined based on the use count value, so that the reference image detection result of the current image is obtained.
In an application scene, the reference image is an image after complete coding, when the reference image is used, the corresponding use count value of the reference image is added, when the use count value is added to exceed the count threshold, the current image is judged to have no reference image, so that the current image is coded in a whole frame later, the situation that the same reference image is used for a plurality of times is avoided, the coding compression rate is improved, and meanwhile, the probability of poor quality of the coded image is reduced.
And S102, responding to the reference image detection result as a reference image, obtaining a motion detection result and selecting a processing mode to process the current image based on the motion detection result, wherein the reference image is an image after the whole frame is encoded, and when the motion detection result is a moving object, the processing mode is to encode at least part of the area of the current image, and the part of the area comprises the moving object.
Specifically, when the reference image detection result is a reference image, a motion detection result is obtained, wherein the motion detection result includes no moving object and a moving object.
Further, the reference image is an image after the whole frame is encoded, when the motion detection result is that there is a moving object, the processing mode is that at least part of the area of the current image is encoded, and the part of the area comprises the moving object. That is, when a moving object is included in the current image, the processing is to encode at least a partial area of the current image, and the partial area includes the moving object.
Optionally, when the current image does not include the moving object, the processing manner is to follow the reference image corresponding to the current image.
In one application mode, when the motion detection result is that the motion target is not present and the reference image is present, the processing mode can be selected to follow the reference image corresponding to the current image and increment the use count value of the reference image once, and when the motion detection result is that the motion target is present and the reference image is present, the processing mode can be selected to encode at least part of the area of the current image and increment the use count value of the reference image once, so that the matched processing mode is selected based on the combination between different motion detection results and the reference image detection results.
Further, when the motion detection result is that a moving object exists, and when the partial region on the current image is encoded, the partial region comprises the moving object, so that the encoded partial region and the reference image are combined to decode a complete picture of the image and feed back the state of the moving object.
And S103, setting a flag bit matched with the processing mode for the processed current image.
Specifically, based on the combination of different motion detection results and reference image detection results, a matched processing mode is selected, each processing mode corresponds to a matched flag bit, and after the processing mode is selected, the flag bit matched with the processing mode is set for the current image, so that the processing mode of any image in encoding can be conveniently determined in decoding, and the decoding efficiency is improved.
Further, in the decoding process, the decoded partial region is overlapped on the decoded reference image, so that a complete image corresponding to the image which is processed in a mode of at least partially encoding the region can be obtained, and the decoding processing amount is reduced to improve the decoding efficiency.
In an application scene, the reference image is an image after full-frame coding, when the processing mode of the image is that the reference image is used or at least part of the area of the current image is coded, the use count value of the reference image corresponding to the image needs to be added, when the use count value of the reference image is added to exceed the count threshold value, the image is subjected to full-frame coding again to obtain a new reference image, and therefore when the use count value exceeds the count threshold value or the image is the first image to be coded, the reference image detection result of the current image is considered to be a reference-free image.
Further, whether the reference image detection result corresponds to the reference image is judged, if the reference image detection result is no reference image, the whole frame encoding is carried out on the current image so that the whole current image is used as a new reference image after being encoded, if the reference image detection result is the reference image, whether the motion detection result corresponds to a moving target is further judged, if the motion detection result is no moving target, the reference image corresponding to the current image is used, and if the motion detection result is the moving target, the partial region corresponding to the moving target in the current image is encoded, so that when the reference image exists in the current image, the encoding quantity of image encoding is effectively reduced, and the encoding compression rate and the encoding efficiency are improved.
According to the scheme, after the current image is obtained, the reference image detection result of the current image is determined in the current image, wherein the reference image is an image after the whole frame of encoding, when the reference image detection result is the reference image, the motion detection result is obtained, the current image is processed based on the motion detection result, when the motion detection result is the moving target, the processing mode is to encode at least part of the area of the current image, the encoding compression rate is improved, for a scene with the moving target, the encoding consumption is effectively reduced, the encoding efficiency is improved, the encoded part of the area is decoded and then superimposed on the decoded reference image, and the whole image corresponding to the image with the processing mode of encoding at least part of the area can be obtained, so that the decoding efficiency is improved.
Referring to fig. 2, fig. 2 is a flow chart of another embodiment of the image encoding method of the present application, the method includes:
s201, obtaining a current image and determining a reference image detection result of the current image.
Specifically, if the current image is an initial image of the whole video data, the reference image detection result of the current image is no reference image, and if a last completely encoded image with a time sequence before the current image is lost, the reference image detection result of the current image is no reference image.
S202, in response to the reference image detection result being no reference image, selecting a processing mode to carry out whole frame coding on the current image, and taking the current image after whole frame coding as a new reference image.
Specifically, when the reference image detection result is no reference image, the selection processing mode is to encode the whole frame of the current image, encode the whole picture of the current image by using an encoding algorithm, and take the current image after the whole frame encoding as a new reference image.
Further, when the reference image corresponds to the usage count value, the usage count value of the new reference image is cleared while the new reference image is obtained.
In one application mode, when the reference image detection result is no reference image, the selection processing mode is to encode the whole frame of the current image, encode the whole picture of the current image by using an encoding algorithm, take the current image after the whole frame encoding as a new reference image, and set the use count value of the new reference image to zero at the same time.
And S203, obtaining a motion detection result in response to the reference image detection result being the reference image.
Specifically, referring to fig. 3, fig. 3 is a flowchart of an embodiment corresponding to step S203 in fig. 2, where the motion detection result is determined based on the following steps, including:
s301, dividing the current image into a plurality of macro blocks, and determining sub-block detection results corresponding to the macro blocks.
Specifically, after the current image is obtained, the current image is divided into a plurality of macro blocks, and motion detection is performed on each macro block, so that sub-block detection results corresponding to the macro blocks are determined.
In an application scene, after a current image is obtained, dividing the current image into a plurality of macro blocks according to pixel sizes, wherein the pixel sizes of the macro blocks at the edge positions of the current image are self-adaptive, the pixel sizes of the macro blocks at the non-edge positions of the current image are preset pixel values, and performing motion detection on each macro block, so that a motion detection zone bit corresponding to a motion detection result in each macro block is output, and a sub-block detection result of each macro block is obtained based on the motion detection zone bit of each macro block, wherein the sub-block detection result is that a moving target and a non-moving target are present, and the macro blocks with the moving target are the moving macro blocks.
S302, determining that a motion detection result corresponding to the current image is a moving target according to the motion macro block which comprises at least one sub-block detection result in the current image and is the moving target.
Specifically, if at least one sub-block detection result in the current image is a motion macro block with a motion target, determining that the motion detection result corresponding to the current image is the motion target.
And S303, determining that the motion detection result corresponding to the current image is a motion-free target in response to the detection results of the sub-blocks corresponding to all the macro blocks corresponding to the current image being the motion-free target.
Specifically, if the sub-block detection results of all the macro blocks in the current image are the motion macro blocks without the motion target, determining that the motion detection result corresponding to the current image is the motion target.
And S204, responding to the motion detection result as no moving object, selecting a processing mode to follow the reference image corresponding to the current image, extracting the time stamp of the current image, and adding the use count value corresponding to the reference image.
Specifically, when the motion detection result is that no moving object exists and the reference frame detection result is that a reference frame exists, the coding mode is selected to follow the reference frame corresponding to the current frame, the time stamp in the current frame is extracted from the current frame to be stored, and the use calculation value corresponding to the reference frame is added on the basis of the prior art.
And S205, responding to the motion detection result as a moving object, selecting a processing mode to encode at least part of the area of the current image, thereby extracting at least the area corresponding to the object which moves from the current image, encoding the area, and adding the use calculation value corresponding to the reference image.
Specifically, when the motion detection result is that there is a moving object and the reference image detection result is that there is a reference image, the selection processing mode is to encode at least a partial region of the current image, extract a region corresponding to the object in the current image, which has moved, or extract a region corresponding to the object in the current image, which has moved, and its peripheral region, to perform partial encoding, so that only partial encoding is performed when there is a movement of the object in the current image, and since only partial encoding is performed, the partially encoded image needs to be decoded by superimposing the reference image when decoding, and therefore, the selection processing mode is to increment the usage count value corresponding to the reference image when encoding at least a partial region of the current image.
In an application mode, the step of encoding at least part of the current image comprises the steps of determining a motion boundary area corresponding to a motion macro block, expanding the motion boundary area outwards according to a preset macro block step length to obtain a region to be scratched, determining the position information of the region to be scratched in the current image, and encoding the region to be scratched in the current image based on encoding parameters matched with the position information.
Specifically, when the motion detection result is determined, the current image is divided into a plurality of macro blocks, when the current image is partially encoded, the motion boundary corresponding to the motion macro block is driven based on the position of the motion macro block, and the motion boundary region is outwards expanded according to the preset macro block step to obtain the region to be scratched, so that the range of the partial encoding is enlarged, the fault tolerance rate for judging the motion boundary region is improved, and the moving object is ensured to be in the region to be scratched.
Further, the position information of the region to be scratched in the current image is determined, and the coding parameters are adjusted so that the coding parameters are matched with the position information of the region to be scratched, and then the region to be scratched in the current image is coded.
In an application scene, determining a motion boundary area corresponding to a motion macro block, and expanding the motion boundary area outwards according to a preset macro block step length to obtain a region to be scratched, wherein the step of determining the motion boundary area based on all the motion macro blocks in response to the fact that the distance between any motion macro block and the motion macro block closest to the motion macro block is within a distance threshold value, and the step of dividing all the motion macro blocks into at least two motion macro block gathering regions based on the distance threshold value in response to the fact that the distance between at least two motion macro blocks and the motion macro block closest to the motion macro block exceeds the distance threshold value respectively determine the motion boundary areas respectively corresponding to the motion macro block gathering regions.
Specifically, whether the distance between any one motion macro block and the motion macro block closest to the motion macro block is within a distance threshold is judged, and if all the motion macro blocks and the motion macro block closest to the motion macro block are within the distance threshold, a motion boundary region is determined based on all the motion macro blocks, so that a region to be scratched is obtained. If the distance between at least two motion macro blocks and the motion macro block closest to the motion macro block exceeds a distance threshold, the motion macro blocks are divided according to the distance threshold to obtain at least two motion macro block gathering areas, wherein when the distance between N motion macro blocks and the motion macro block closest to the motion macro block exceeds the distance threshold, N motion macro block gathering areas are obtained, wherein the motion macro block gathering areas at least comprise one motion macro block, a motion boundary area is determined based on each motion macro block gathering area, so that a region to be scratched corresponding to each motion boundary area is obtained, and when the motion macro blocks are farther away from each other, different motion macro block gathering areas are respectively subjected to partial coding, so that the coding quantity of partial coding is reduced, and the compression rate of coding is improved.
In a specific application scenario, referring to fig. 4, fig. 4 is a schematic application scenario of an embodiment corresponding to step S204 in fig. 2, in which a moving macro block corresponds to a diagonal frame, a distance threshold is a distance between 4 moving macro blocks, a distance between any moving macro block and a nearest moving macro block is within the distance threshold, only one moving macro block gathering area is obtained, a one-dimensional array (the array is sequentially stored from left to right according to an image from top to bottom) is returned by the motion detection algorithm, each member in the array represents a motion state of one macro block, 1 is motion, 0 is no motion, coordinates of each moving macro block can be obtained through the array and the width of the image, then the ratio of the horizontal to vertical coordinates of all the coordinates is judged, the maximum and minimum values are respectively taken out, namely, an upper left corner and a lower right corner in the dashed line frame are taken as moving boundary areas, and after the moving boundary areas are obtained, a region to be scratched according to a preset macro block step length is obtained, namely, a region to be scratched by gray blocks in fig. 4.
In another specific application scenario, referring to fig. 5, fig. 5 is a schematic view of an application scenario of another embodiment corresponding to step S204 in fig. 2, where a moving macroblock corresponds to a diagonal frame, a distance threshold is a distance between 5 moving macroblocks, a distance between two regions corresponding to two moving macroblocks exceeds the distance threshold, and two aggregation regions are located between the moving macroblocks, so as to obtain two moving macroblock aggregation regions shown in a dashed frame in fig. 5, and thus, the two regions to be scratched are obtained by respectively expanding the two moving macroblock regions outwards according to a preset macroblock step length, that is, a region surrounded by a gray macroblock in fig. 5. Thus, when the distance between the motion macro block gathering areas is larger, the encoding can be divided into areas so as to reduce the encoding quantity of the non-motion areas, thereby improving the encoding compression rate.
Optionally, before the step of encoding at least a partial region of the current image, so as to extract at least a region corresponding to a moving object from the current image, and adding the usage count value corresponding to the reference image, the method further includes determining whether the total area of the region corresponding to the moving object exceeds a duty ratio threshold, and in response to the duty ratio threshold being exceeded, selecting the method to encode the current image in whole frame, so that the current image after the whole frame is used as a new reference image, and clearing the usage count value.
Specifically, when the motion detection result is that a moving object exists and the reference image detection result is that a reference image exists, whether the area of the area corresponding to the object which has moved accounts for the total area of the current image exceeds a duty ratio threshold value is judged, when the area exceeds the duty ratio threshold value, more and larger moving objects in the current image are judged, the whole frame of the current image is coded in a selection processing mode, the image is directly coded in other judging processes, the coding efficiency is improved, the moving objects are prevented from being lost, and when the duty ratio does not exceed the duty ratio threshold value, at least part of the area of the current image is coded in the selection processing mode.
In an application scene, when judging whether the area of the area corresponding to the moving target exceeds the total area of the current image or not, determining the ratio of the area corresponding to the moving target to the total area of the current image through the ratio of the moving macro block to all macro blocks corresponding to the current image.
And S206, in response to the usage count value exceeding the count threshold, encoding the whole frame of the current image and clearing the usage count value.
Specifically, when the usage count value corresponding to the reference image exceeds the count threshold, the current image is subjected to full-frame encoding while the usage count value is cleared, so that the phenomenon that the same reference image is used or referenced for too many times is avoided, inter-frame encoding errors are reduced, and waiting time during decoding is reduced.
S207, setting a flag bit matched with the processing mode for the processed current image.
Specifically, the processing mode corresponds to the matched flag bit, thereby facilitating efficient determination of the decoding mode at the time of decoding.
In an application mode, a first zone bit corresponding to a processing mode is set for carrying out whole frame coding on a current image in response to the processing mode, the processed current image and the first zone bit are packaged and stored, a second zone bit corresponding to the processing mode is set for carrying out follow-up of a reference image corresponding to the current image in response to the processing mode, a timestamp of the current image and the second zone bit are packaged and stored, a third zone bit corresponding to the processing mode is set for carrying out coding on at least part of the area of the current image in response to the processing mode, and position information and the processed part of the area and the third zone bit are packaged and stored.
Specifically, after a matched processing mode is selected for an image, the processed image is required to be packaged and stored, when the processing mode is to encode the whole frame of the current image, a first zone bit corresponding to the processing mode is set, the processed current image and the first zone bit are packaged and stored, when the processing mode is to follow a reference image corresponding to the current image, a second zone bit corresponding to the processing mode is set, a timestamp of the current image extracted before is packaged and stored with the second zone bit, when the processing mode is to encode at least part of the current image, a third zone bit corresponding to the processing mode is set, and position information corresponding to the processed part of the area and the processed part of the area are packaged and stored with the third zone bit, so that the superposition of the decoded part of the image area and the reference image can be determined during decoding, and the decoding accuracy is improved.
In an application scenario, when the processing manner is encoding at least a partial region of the current image, the partial region corresponds to the region to be scratched in the step S205, and the position information corresponding to the region to be scratched, the processed region to be scratched and the third flag bit are packaged and stored.
In this embodiment, after a current image is divided into a plurality of macro blocks and motion detection is performed, after a motion detection result and a reference image detection result are obtained, a proper processing mode is selected based on the motion detection result and the reference image detection result, so as to improve the encoding compression rate in the time domain, wherein when the processing mode is to encode at least a partial area of the current image, the distance between any one of the motion macro blocks and the nearest motion macro block is determined, thereby dividing a motion macro block gathering area and expanding the area to be scratched, the area to be scratched is encoded, when the distance between the motion macro block gathering areas is larger, the area encoding can be performed, so as to reduce the encoding quantity of a non-motion area, thereby improving the encoding compression rate, and when the processing mode is to encode a reference image corresponding to the current image, the encoding compression rate and the encoding efficiency are not required to be improved, when the processing mode is to encode the reference image corresponding to the current image, when the processing mode is to encode the whole frame encoding, the image is to encode the whole frame, so as to obtain a new reference image, the encoding quality is not required to be improved, and the encoding rate is not reduced more than the reference image is required.
Referring to fig. 6, fig. 6 is a flowchart illustrating an embodiment of an image decoding method according to the present application, wherein the image decoding method according to the present embodiment is applied to an image encoded by the image encoding method according to any one of the embodiments, and the method includes:
S601, obtaining a current image to be decoded, and determining the processing mode of the current image based on the zone bit matched with the processing mode.
Specifically, when the coded image is decoded, a current image to be decoded is obtained, and the processing mode of the current image in the coding process is determined based on the zone bit matched with the processing mode in the coding process.
And S602, responding to the processing mode to encode at least part of the area of the current image, decoding the encoded area to obtain a decoding subarea, superposing the timestamp corresponding to the current image and the decoding subarea on the corresponding position of the reference image corresponding to the current image to obtain a decoded current image, and taking the decoded current image as a new reference image.
Specifically, when the processing mode is to encode at least part of the area of the current image, decoding the encoded area to obtain a decoding subarea, and overlapping the decoding subarea on the current image, wherein the decoding subarea is overlapped and replaced based on the position information to display a moving target, and the timestamp of the current image is overlapped on the reference image of the current image to ensure that the timestamp is accurately displayed, and the decoded current image is used as a new reference image, so that the continuity and instantaneity of the image in continuous display are ensured.
In an application scene, the encoded region is obtained based on the region to be scratched in the embodiment, the encoded region corresponding to the region to be scratched is decoded to obtain a decoded sub-region, so that the decoded sub-region is superimposed into the current image based on the position information corresponding to the region to be scratched, the decoded image is used as a new reference image, and when the reference image is needed to be used by a subsequent image, a moving target in the current image can still be displayed.
Further, after the step of obtaining the current image to be decoded and determining the processing mode of the current image based on the zone bit matched with the processing mode, the method further comprises the steps of directly performing full-frame decoding on the current image to obtain the decoded current image in response to the processing mode of performing full-frame encoding on the current image, taking the decoded current image as a new reference image, and superposing a timestamp corresponding to the current image on the reference image corresponding to the current image in response to the processing mode of continuing to use the reference image corresponding to the current image to obtain the decoded current image.
Specifically, when the processing mode is that the current image is subjected to whole frame coding, the current image is directly subjected to whole frame decoding during decoding to obtain a decoded current image, the whole frame decoded current image is used as a new reference image, and after each whole frame decoded image is obtained, the whole frame decoded image is used as a new reference image and later images are referenced.
Further, when the processing mode is to follow the reference image corresponding to the current image, the current reference image is acquired, and the time stamp of the current image is superimposed on the reference image corresponding to the current image, so that the time stamp is accurately displayed, and the decoded current image is obtained.
It can be understood that when the video stream needs to be dragged for playback, if the processing mode of the playback initial image is full-frame encoding, full-frame decoding is performed from the current image, and if the processing mode of the playback initial image is along with the reference image or partial encoding corresponding to the current image, the reference image corresponding to the playback initial video can be necessarily obtained by searching up to at most the threshold number of images in the above embodiment from the current image, so that the picture of the playback initial image is obtained, and frame-by-frame display is performed from the playback initial image.
According to the scheme, if the processing mode of the current image is the reference image corresponding to the current image, only the stored time stamp is needed to be extracted, and if the processing mode of the current image is the encoding mode of at least part of the current image, the encoded region is decoded to obtain the decoded sub-region, and the decoded sub-region and the time stamp corresponding to the current image are overlapped on the reference image, so that the decoding efficiency is improved.
Referring to fig. 7, fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present application, the electronic device 70 includes a memory 701 and a processor 702 coupled to each other, wherein the memory 701 stores program data (not shown), and the processor 702 invokes the program data to implement the method in any of the above embodiments, and the description of the related content is referred to the detailed description of the above method embodiments and is not repeated herein.
Referring to fig. 8, fig. 8 is a schematic structural diagram of an embodiment of a computer readable storage medium 80 according to the present application, where the computer readable storage medium 80 stores program data 800, and the program data 800 when executed by a processor implements the method in any of the above embodiments, and details of the related content are described in the above embodiments, which are not repeated herein.
The units described as separate units may or may not be physically separate, and units displayed as units may or may not be physical units, may be located in one place, or may be distributed over a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiment.
In addition, each functional unit in the embodiments of the present application may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit. The integrated units may be implemented in hardware or in software functional units.
The integrated units, if implemented in the form of software functional units and sold or used as stand-alone products, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present application may be embodied in essence or a part contributing to the prior art or all or part of the technical solution in the form of a software product stored in a storage medium, including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) or a processor (processor) to execute all or part of the steps of the methods of the embodiments of the present application. The storage medium includes a U disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), a magnetic disk, an optical disk, or other various media capable of storing program codes.
The foregoing description is only of embodiments of the present application, and is not intended to limit the scope of the application, and all equivalent structures or equivalent processes using the descriptions and the drawings of the present application or directly or indirectly applied to other related technical fields are included in the scope of the present application.