Method and device for improving video image difference detection accuracy
By setting a similarity dynamic threshold in video image difference detection and using OpenCV's image processing technology to perform image enhancement and variance value calculation, the problems of low accuracy and high false alarm rate in the prior art are solved, and higher detection accuracy and lower false alarm rate are achieved.
Patent Information
- Application Number
- CN202411775905.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-05
- Publication Date
- 2025-04-18
AI Technical Summary
In the case of a large environmental impact, the existing video image difference detection method has low accuracy and high false alarm rate, making it difficult to accurately set the similarity threshold.
By setting the similarity dynamic threshold, using OpenCV's video image processing technology and image enhancement technology, the monitoring video is extracted, the image enhancement and the clarity enhancement are enhanced, the variance value of the image is calculated, the historical curve of the variance value is generated, and the dynamic threshold is calculated using a simple moving average algorithm to determine the image difference.
It effectively improves the accuracy of video image difference detection, reduces the chance of false alarms, and can detect tiny image changes more accurately.
Smart Images

Figure CN119851002A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image detection technology, specifically a method and apparatus for improving the accuracy of video image difference detection. Background Technology
[0002] Today, the development of artificial intelligence (AI) algorithms is accelerating rapidly, exhibiting a state of constant change. Every so often, image algorithms undergo enhanced updates and iterations. These updates not only improve the accuracy and efficiency of the algorithms but also greatly enhance image detection capabilities, enabling them to demonstrate superior performance in various application scenarios.
[0003] Video image difference detection is a crucial application in image detection, a technique used to detect discrepancies between video images. Traditional image difference detection methods utilize digital video and image processing techniques to calculate pixel differences between a reference image and the image to be detected, thus obtaining a similarity value. If the similarity value is below a pre-set threshold, the two images are considered different. However, video image acquisition is significantly affected by the environment; different lighting, weather conditions, and equipment can all cause pixel differences, resulting in poor stability, low accuracy, and certain limitations. Furthermore, in surveillance video analysis scenarios, such as detecting changes in equipment location or wiring in a server room at different times, actual business needs must be considered during image comparison. False alarms should not be generated due to changes in lighting or shadows, while precise detection of subtle changes, such as indicator light variations, is required. This places demands on the setting of the similarity threshold, and traditional methods struggle to accurately determine its value. Summary of the Invention
[0004] This invention addresses the needs and shortcomings of current technological development by providing a method and apparatus for improving the accuracy of video image difference detection. By setting a dynamic similarity threshold, it effectively improves the accuracy of image difference comparison and reduces the probability of false alarms.
[0005] Firstly, the present invention provides a method for improving the accuracy of video image difference detection, and the technical solution adopted to solve the above-mentioned technical problem is as follows:
[0006] A method for improving the accuracy of video image difference detection includes the following steps:
[0007] S1. Using OpenCV's video image processing technology, frames are extracted from the surveillance video of the same device at the same location at different times to obtain images;
[0008] S2. Using OpenCV's image enhancement technology, image enhancement and sharpness enhancement are achieved;
[0009] S3. Using image processing techniques, calculate the variance of the two images to assist in subsequent similarity assessment.
[0010] S4. Repeat steps S1-S3 to generate a historical curve of variance by performing similarity comparison analysis on images extracted at different time points. Based on the historical curve of variance, a simple moving average algorithm is used to calculate the dynamic threshold for each time point. The dynamic threshold is used to determine whether there is a difference between the image at the current time point and the reference image.
[0011] Optionally, when performing step S1, a scheduled task is set; after acquiring the monitoring video of the same device at the same location, the scheduled task is executed to extract frames from the monitoring video at set intervals to obtain images.
[0012] Optionally, step S2 specifically includes the following operations:
[0013] S2.1 Image Denoising: Use OpenCV to read a grayscale image and apply the GaussianBlur function for denoising;
[0014] S2.2 Histogram Equalization: Use OpenCV's equalizeHist function to perform histogram equalization on the image;
[0015] S2.3 Image Sharpening: Use OpenCV's Laplacian function to perform Laplacian filtering on the image;
[0016] S2.4 Non-local means denoising: Use OpenCV's fastNlMeansDenoising function to perform non-local means denoising on the image;
[0017] S2.5 Adaptive Histogram Equalization: Use OpenCV's createCLAHE function to create a CLAHE object, and use the CLAHE object's apply method to perform adaptive histogram equalization on the image;
[0018] S2.6 Edge Enhancement Filtering: Use OpenCV's Sobel function to perform Sobel filtering on the image in the horizontal and vertical directions respectively, and calculate the combination of these two directions;
[0019] S2.7, Canny Edge Detection: Use OpenCV's Canny function to perform edge detection on the image.
[0020] Optionally, step S3 specifically includes the following operations:
[0021] S3.1 Scale the image to be processed to the specified size;
[0022] S3.2, Perform grayscale processing on the image;
[0023] S3.3 Calculate the average value of each row of pixels by iterating through each row of pixels in the image, and use each average value as a feature representation of the pixels in that row;
[0024] S3.4 Calculate the variance of all the obtained average values. The variance is used as the feature value of the image, reflecting the fluctuation of the pixel features in each row.
[0025] Alternatively, step S4 may specifically include the following operations:
[0026] S4.1 After obtaining images at different time points according to step S1, step S2 and step S3 are executed sequentially for each image to obtain the variance value of each image. These variance values are arranged in chronological order to generate a historical curve of variance values.
[0027] S4.2. A simple moving average algorithm is used as the dynamic threshold calculation method to remove the highest and lowest values to reduce the interference of outliers. A time window size is defined, and the average variance difference corresponding to the time window is calculated.
[0028] S4.3 After obtaining the moving average, a dynamic similarity threshold for each time point is determined through statistical analysis. If the similarity is higher than the dynamic threshold, it is considered that there is a difference between the image at the current time point and the reference image.
[0029] Secondly, the present invention provides an apparatus for improving the accuracy of video image difference detection, and the technical solution adopted to solve the above-mentioned technical problem is as follows:
[0030] An apparatus for improving the accuracy of video image difference detection, comprising:
[0031] The timed frame extraction module is used to extract frames from the monitoring video of the same device at the same location at different times using OpenCV video image processing technology.
[0032] The image enhancement module uses OpenCV image enhancement technology to achieve image enhancement and sharpness enhancement;
[0033] The similarity calculation module is used to calculate the variance of two images using image processing technology, in order to assist the threshold calculation module in determining their similarity.
[0034] The threshold calculation module is used to first perform similarity comparison analysis on images extracted at different time points to generate a historical curve of variance values. Then, based on the historical curve of variance values, a simple moving average algorithm is used to calculate the dynamic threshold for each time point. The dynamic threshold is used to determine whether there is a difference between the image at the current time point and the reference image.
[0035] Optionally, the timed frame extraction module first sets a timed task, then after acquiring the monitoring video of the same device at the same location, executes the timed task, extracting frames from the monitoring video at set intervals to obtain images.
[0036] Optionally, the image enhancement modules involved specifically include:
[0037] The image denoising unit is used to read a grayscale image using OpenCV and apply the GaussianBlur function for denoising.
[0038] The histogram equalization unit is used to perform histogram equalization on an image using OpenCV's equalizeHist function.
[0039] The image sharpening unit is used to perform Laplacian filtering on the image using OpenCV's Laplacian function;
[0040] The mean denoising unit is used to perform nonlocal mean denoising on the image using OpenCV's fastNlMeansDenoising function;
[0041] The adaptive equalization unit is used to create a CLAHE object using OpenCV's createCLAHE function and then use the CLAHE object's apply method to perform adaptive histogram equalization on the image.
[0042] The enhancement filtering unit is used to perform Sobel filtering on the image in the horizontal and vertical directions using OpenCV's Sobel function, and to calculate the combination of these two directions;
[0043] The edge detection unit is used to perform edge detection on the image using OpenCV's Canny function.
[0044] Optionally, the similarity calculation module involved includes:
[0045] The image scaling unit is used to scale the image to be processed to a specified size.
[0046] The grayscale processing unit is used to perform grayscale processing on the image;
[0047] The pixel calculation unit is used to calculate the average value of each row of pixels by iterating through each row of pixels in the image, and each average value is used as a feature representation of the pixels in that row.
[0048] The variance calculation unit is used to calculate the variance of all the obtained average values. The resulting variance is used as the feature value of the image, reflecting the fluctuation of the features of each row of pixels.
[0049] Further optionally, the threshold calculation module involved includes:
[0050] The curve generation unit is used to extract images from the surveillance video of the same device and the same location at different time points. After executing the image enhancement module and the similarity calculation module, the variance value of each image is obtained. These variance values are arranged in chronological order to generate a historical curve of the variance values.
[0051] The mean calculation unit is used to employ a simple moving average algorithm as a dynamic threshold calculation method, remove the highest and lowest values to reduce outlier interference, define a time window size, and calculate the average variance difference corresponding to the time window.
[0052] The threshold calculation unit is used to determine the dynamic similarity threshold for each time point through statistical analysis after obtaining the moving average. If the similarity is higher than the dynamic threshold, it is considered that there is a difference between the image at the current time point and the reference image.
[0053] The method and apparatus for improving the accuracy of video image difference detection according to the present invention have the following advantages compared with the prior art:
[0054] Before performing difference comparison on images captured by a camera, this invention comprehensively utilizes multiple image enhancement techniques to preprocess the images, then calculates the comparison similarity, and finally calculates a dynamic threshold based on the historical trend of the similarity, thereby effectively improving the accuracy of the comparison and reducing the probability of false alarms. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Attachment Figure 1 This is a flowchart illustrating the method implementation of Embodiment 1 of the present invention;
[0056] Attachment Figure 2 This is a module connection block diagram of Embodiment 2 of the present invention. DETAILED DESCRIPTION
[0057] To make the technical solution, the technical problem solved, and the technical effect of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with specific embodiments.
[0058] Example 1:
[0059] Combined with appendix Figure 1This embodiment proposes a method to improve the accuracy of video image difference detection, which includes the following steps:
[0060] S1. Using OpenCV's video image processing technology, frames are extracted from the same monitoring video of the same device at the same location at different times to obtain images. The specific operation is as follows:
[0061] S1.1 First, use a programming language (such as Python) combined with the OpenCV library to build the corresponding video processing program framework; in the program, set a timed task, which can trigger subsequent operations based on the system time or a set start time;
[0062] S1.2 Next, the program retrieves video stream data from the same device at the same location. Here, it's crucial to ensure the program has permission to access the corresponding video storage location or the real-time video stream interface. For example, if the video is stored in a specific local folder, the correct file path must be configured; if it's a real-time video stream from a network camera, the appropriate network connection parameters and authentication information (if required) must be configured to ensure successful reading of the video content.
[0063] S1.3. Then, after successfully acquiring the surveillance video, execute the scheduled task. The time interval setting in the scheduled task can be determined based on actual needs, video frame rate, and focus factors. For example, if the surveillance video has a high frame rate and the main focus is on general image changes over a long period, the time interval can be set to a few minutes (e.g., extracting frames every 5 minutes); while if the video frame rate is low and more detailed image changes need to be captured, the time interval can be shortened to tens of seconds (e.g., extracting frames every 30 seconds). At each set time interval, the video frame reading function in the OpenCV library (e.g., the cv2.VideoCapture class combined with the read method) is called to extract the corresponding frame images from the surveillance video. These acquired images are then saved to a specified local folder according to a certain naming rule for easy processing and viewing in subsequent steps. The naming rule can include a timestamp or other format that reflects the extraction time, such as "device name_location name_extraction time.jpg".
[0064] S2. Using OpenCV's image enhancement technology, image enhancement and sharpness enhancement are achieved, specifically including the following operations:
[0065] S2.1 Image Denoising: Use OpenCV to read a grayscale image and apply the GaussianBlur function for denoising. It's important to note that when using the GaussianBlur function, appropriate parameters need to be passed in. This function generally accepts three main parameters: the input image (usually a pre-read grayscale image or a single-channel grayscale image converted from a color image), the kernel size (often expressed in the form of (ksize_width, ksize_height), such as (3, 3), (5, 5), etc., and the width and height should be positive odd numbers), and the standard deviation in the x and y directions (which can be set to 0, in which case the function will automatically calculate the standard deviation based on the kernel size).
[0066] S2.2 Histogram Equalization: The `equalizeHist` function in OpenCV is used to perform histogram equalization on the image. It's important to note that the `equalizeHist` function primarily operates on single-channel grayscale images. It redistributes the grayscale distribution of the image, making the histogram more uniform, thereby enhancing image contrast and making details more clearly visible. Its input parameter is the single-channel grayscale image to be processed, and the output is the histogram-equalized image.
[0067] S2.3 Image Sharpening: The Laplacian function from OpenCV is used to perform Laplacian filtering on the image. It's important to note that the Laplacian function calculates the second derivative of the image based on the Laplacian operator, which can highlight details such as edges, thus sharpening the image. The input parameters of the Laplacian function include the image to be processed (which can be a grayscale image or a single-channel color image), the data type of the output image (generally set to the same data type as the input image, such as cv2.CV_8U), and the kernel size (an optional parameter with default settings). The image sharpened by Laplacian filtering alone is usually blended with the original image (e.g., by weighted addition) to obtain a visually sharper image.
[0068] S2.4 Non-local means denoising: The OpenCV function `fastNlMeansDenoising` is used to perform non-local means denoising on the image. It's worth noting that `fastNlMeansDenoising` is primarily used to remove complex noise types such as salt-and-pepper noise from images. Compared to some local filtering denoising methods, it considers a larger range of similar regions in the image for denoising, thus better preserving image details. The input parameters for `fastNlMeansDenoising` include the image to be processed (generally a single-channel grayscale image; for color images, OpenCV also has a corresponding function `fastNlMeansDenoisingColored`), the search window size (determines the range of similar pixel blocks to search for, e.g., 15), the block size (determines the size of the local blocks used for comparison, e.g., 7), and a parameter controlling the denoising intensity (generally ranging from 0 to 1; a larger value results in stronger denoising but may also blur more image details). Different images have different noise levels, so it is necessary to select the appropriate parameter combination through experimentation in order to achieve the best noise reduction effect while minimizing the loss of image details.
[0069] S2.5 Adaptive Histogram Equalization: Use OpenCV's `createCLAHE` function to create a `CLAHE` (Contrast Limited Adaptive Histogram Equalization) object, and then use the `apply` method of the `CLAHE` object to perform adaptive histogram equalization on the image. It's important to note that unlike ordinary histogram equalization, `CLAHE` performs histogram equalization within local regions and avoids over-enhancing local contrast, which could lead to noise amplification or image distortion, by setting contrast limit parameters. When creating a `CLAHE` object, you need to pass in contrast limit parameters (generally between 0 and 1, such as 0.01, representing the proportion of contrast limitation) and the size of the regions to be divided (represented in the form of `(tileGridWidth, tileGridHeight)`, for example, `(8, 8)` means dividing the image into 8×8 small regions for histogram equalization). This adaptive approach can better enhance the contrast of different areas of the image, especially suitable for images with uneven lighting, improving the overall image clarity and detail.
[0070] S2.6 Edge Enhancement Filtering: OpenCV's Sobel function is used to perform Sobel filtering on the image in both the horizontal and vertical directions, and the combination of these two directions is calculated. It's important to note that the Sobel function uses the Sobel operator to calculate approximate gradient values in the horizontal and vertical directions to detect and enhance edges. The input parameters for the Sobel function include the image to be processed (which can be a grayscale image or a single-channel image converted from a color image), the data type of the output image (e.g., cv2.CV_8U), the order of the derivative (for Sobel filtering, generally the first derivative is used; setting it to 1 in both the x and y directions indicates calculating the first derivative in the horizontal and vertical directions), and the kernel size (e.g., (3, 3)). This operation highlights the edges in the image, making them clearer and facilitating subsequent extraction and analysis of object contours and other information.
[0071] S2.7 Canny Edge Detection: This section describes how to perform edge detection on an image using OpenCV's Canny function. It's important to note that Canny edge detection is a classic, multi-step algorithm. It first smooths the image (usually using Gaussian filtering to remove noise), then calculates the gradient magnitude and direction, applies non-maximum suppression to refine the edges, and finally uses double thresholding to determine the final edges. When using the Canny function, you need to pass in the image to be processed (usually a grayscale image), a low threshold, and a high threshold. The low threshold is used to initially filter possible edge points, while the high threshold is used to confirm strong edges. Generally, the high threshold is greater than the low threshold; for example, the low threshold could be set to 50, and the high threshold to 150. The specific values need to be adjusted based on factors such as image contrast.
[0072] S3. Using image processing techniques, calculate the variance of the two images to assist in subsequent similarity assessment. This includes the following operations:
[0073] S3.1 Scale the image to be processed to the specified size. In OpenCV, this can be achieved using the cv2.resize function. This function takes the original image to be processed, the specified output size (which can be represented as a tuple, such as (width, height), for example (200, 200) means scaling the image to 200 pixels wide and 200 pixels high), and the scaling interpolation method (common interpolation methods include cv2.INTER_LINEAR (linear interpolation, suitable for enlarging or reducing images) and cv2.INTER_NEAREST (nearest neighbor interpolation, which is fast but may have slightly worse results) as parameters.
[0074] S3.2. Perform grayscale processing on the image. In OpenCV, this can be achieved using the cv2.cvtColor function, which takes the original image and the target color space mode for conversion as parameters (here, the mode for converting to grayscale is cv2.COLOR_BGR2GRAY, assuming the original image is a BGR format color image).
[0075] S3.3 Calculate the average value of each row of pixels by iterating through each row of pixels in the image. Each average value is used as a feature representation of the pixels in that row. The overall features of the image can then be further analyzed based on these row features.
[0076] S3.4. Use NumPy library functions to calculate the variance of the average pixel values of all rows obtained previously. This variance is used as a feature value of the image to measure the fluctuation of the features of each row of pixels. The variance value reflects the degree of dispersion of the image features at the row pixel level. The larger the variance, the greater the fluctuation of features such as the average brightness of each row of pixels. To a certain extent, it can reflect the differences in the content or quality of the image and can be used for subsequent analysis operations such as similarity comparison with other images.
[0077] S4. Repeat steps S1-S3 to generate a historical variance curve by performing similarity comparison analysis on images extracted at different time points. Based on the historical variance curve, a simple moving average algorithm is used to calculate the dynamic threshold for each time point. The dynamic threshold is used to determine whether there is a difference between the image at the current time point and the reference image. This process specifically includes the following operations:
[0078] S4.1 After obtaining images at different time points according to step S1, perform steps S2 and S3 sequentially for each image to obtain the variance value of each image. Arrange these variance values in chronological order to generate a historical curve of the variance values. It should be noted that to easily arrange and visualize the variance values in chronological order (generate a historical curve), data processing libraries (such as pandas) and plotting libraries (such as matplotlib) in Python can be used. This method allows for a clear and intuitive view of the trend of variance values over time, providing basic data support for subsequent analysis of the dynamic changes in image features and determination of dynamic thresholds.
[0079] S4.2 A simple moving average algorithm is used as the dynamic threshold calculation method to remove the highest and lowest values to reduce outlier interference. A time window size is defined, and the average variance difference corresponding to this time window is calculated. It is important to note that the choice of time window size is crucial. Too small a window may not effectively smooth outlier interference, while too large a window may ignore short-term changes in image features. It needs to be reasonably determined based on the characteristics of the actual image data and the analysis requirements. The formulas involved in this process are as follows:
[0080]
[0081] Where n is the defined time window size, t is the comparison time point, and x is the original similarity value of each past comparison.
[0082] S4.3 After obtaining the moving average, a dynamic similarity threshold for each time point is determined through statistical analysis. If the similarity is higher than the dynamic threshold, it is considered that there is a difference between the image at the current time point and the reference image.
[0083] Example 2:
[0084] Combined with appendix Figure 2 This embodiment proposes a device for improving the accuracy of video image difference detection, which includes:
[0085] The timed frame extraction module is used to extract frames from the monitoring video of the same device at the same location at different times using OpenCV video image processing technology.
[0086] The image enhancement module uses OpenCV image enhancement technology to achieve image enhancement and sharpness enhancement;
[0087] The similarity calculation module is used to calculate the variance of two images using image processing technology, in order to assist the threshold calculation module in determining their similarity.
[0088] The threshold calculation module is used to first perform similarity comparison analysis on images extracted at different time points to generate a historical curve of variance values. Then, based on the historical curve of variance values, a simple moving average algorithm is used to calculate the dynamic threshold for each time point. The dynamic threshold is used to determine whether there is a difference between the image at the current time point and the reference image.
[0089] In this embodiment, the timed frame extraction module first sets up a timed task. After acquiring surveillance video from the same device at the same location, it executes the timed task, extracting frames from the video at set intervals to obtain images. Specifically, the timed frame extraction module may perform the following operations: ① First, a corresponding video processing program framework is built using a programming language (e.g., Python) combined with the OpenCV library. A timed task is set in the program, which can trigger subsequent operations based on system time or a set start time. ② Next, the program acquires the video stream data from the same device at the same location. Here, it is necessary to ensure that the program has permission to access the corresponding surveillance video storage location or real-time video stream interface. For example, if the surveillance video is stored in a specific local folder, the correct file path must be configured; if it is a real-time video stream from a network camera, the corresponding network connection parameters and authentication information (if required) must be configured to ensure successful reading of the video content. ③ Then, after successfully acquiring the surveillance video, the timed task is executed. The time interval setting in the scheduled task can be determined by considering factors such as actual needs, video frame rate, and focus. For example, if the monitored video has a high frame rate and the main focus is on the general changes in the scene over a long period, the time interval can be set to a few minutes (e.g., extracting frames every 5 minutes). If the video frame rate is low and more detailed capture of scene changes is required, the time interval can be shortened to tens of seconds (e.g., extracting frames every 30 seconds). At each set time interval, the video frame reading function in the OpenCV library (e.g., the cv2.VideoCapture class combined with the read method) is called to extract the corresponding frame image from the monitored video. These obtained images are then saved to a specified local folder according to a certain naming rule for easy processing and viewing in subsequent steps. The naming rule can include a timestamp or other format that reflects the extraction time, such as "device name_location name_extraction time.jpg".
[0090] In this embodiment, the image enhancement module specifically includes:
[0091] The image denoising unit is used to read a grayscale image using OpenCV and apply the GaussianBlur function for denoising. It should be noted that the image denoising unit needs to pass appropriate parameters when using the GaussianBlur function. This function generally accepts three main parameters: the input image (usually a grayscale image that has already been read or a single-channel grayscale image converted from a color image), the kernel size (usually expressed in the form of (ksize_width, ksize_height), such as (3, 3), (5, 5), etc., and the width and height should be positive odd numbers), and the standard deviation in the x and y directions (can be set to 0, in which case the function will automatically calculate the standard deviation based on the kernel size).
[0092] The Histogram Equalization unit is used to perform histogram equalization on an image using OpenCV's equalizeHist function. It's worth noting that the equalizeHist function primarily operates on single-channel grayscale images. It redistributes the grayscale distribution of the image, making the histogram more uniform, thereby enhancing image contrast and making details more clearly visible. Its input parameter is the single-channel grayscale image to be processed, and its output is the histogram-equalized image.
[0093] The image sharpening unit is used to perform Laplacian filtering on an image using OpenCV's Laplacian function. It's worth noting that the Laplacian function calculates the second derivative of an image based on the Laplacian operator, highlighting details such as edges to achieve image sharpening. The input parameters of the Laplacian function include the image to be processed (which can be a single channel of a grayscale or color image), the data type of the output image (generally set to the same data type as the input image, such as cv2.CV_8U), and the kernel size (an optional parameter with default settings). The image sharpened by Laplacian filtering alone is usually blended with the original image (e.g., through weighted addition) to obtain a more visually sharpened image.
[0094] The mean denoising unit is used to perform non-local mean denoising on images using OpenCV's `fastNlMeansDenoising` function. It's worth noting that `fastNlMeansDenoising` is primarily used to remove complex noise types such as salt-and-pepper noise from images. Compared to some local filtering denoising methods, it considers a larger area of similar regions in the image for denoising, thus better preserving image details. The input parameters for `fastNlMeansDenoising` include the image to be processed (generally a single-channel grayscale image; for color images, OpenCV also has a corresponding function, `fastNlMeansDenoisingColored`), the search window size (determines the range of similar pixel blocks to search for, e.g., 15), the block size (determines the size of the local blocks used for similarity comparison, e.g., 7), and a parameter controlling the denoising intensity (generally ranging from 0 to 1; a larger value results in stronger denoising but may also blur more image details). Different images have different noise levels, requiring experimentation to select the appropriate parameter combination to achieve the best denoising effect while minimizing the loss of image details.
[0095] The Adaptive Equalization Unit uses OpenCV's `createCLAHE` function to create a `CLAHE` (ContrastLimited Adaptive Histogram Equalization) object, and then uses the `apply` method of the `CLAHE` object to perform adaptive histogram equalization on the image. It's important to note that unlike ordinary histogram equalization, `CLAHE` performs histogram equalization within local regions and avoids excessive enhancement of local contrast, which could lead to noise amplification or image distortion, by setting contrast limit parameters. When creating a `CLAHE` object, you need to pass in contrast limit parameters (generally between 0 and 1, such as 0.01, representing the proportion of contrast limitation) and the size of the regions to be divided (represented in the form of `(tileGridWidth, tileGridHeight)`, for example, `(8, 8)` means dividing the image into 8×8 small regions for histogram equalization). This adaptive approach can better enhance the contrast of different regions of the image, especially suitable for images with uneven lighting, improving the overall image clarity and detail.
[0096] The enhancement filtering unit uses OpenCV's Sobel function to perform Sobel filtering on the image in both the horizontal and vertical directions, and calculates the combination of these two directions. It's worth noting that the Sobel function calculates approximate gradients in the horizontal and vertical directions based on the Sobel operator, thereby detecting and enhancing edges. The input parameters of the Sobel function include the image to be processed (which can be a grayscale image or a single-channel image converted from a color image), the data type of the output image (e.g., cv2.CV_8U), the order of the derivative (for Sobel filtering, generally the first derivative is used; setting it to 1 in both the x and y directions indicates calculating the first derivative in the horizontal and vertical directions), and the kernel size (e.g., (3, 3)). This operation highlights the edges in the image, making them clearer and facilitating subsequent extraction and analysis of object contours and other information.
[0097] The edge detection unit is used to perform edge detection on the image using OpenCV's Canny function. It's worth noting that Canny edge detection is a classic, multi-step edge detection algorithm. It first smooths the image (usually using Gaussian filtering to remove noise), then calculates the gradient magnitude and direction, applies non-maximum suppression to refine the edges, and finally determines the final edges through double thresholding. When using the Canny function, you need to pass in the image to be processed (usually a grayscale image), a low threshold, and a high threshold (the low threshold is used to initially filter possible edge points, and the high threshold is used to confirm strong edges; generally, the high threshold is greater than the low threshold, for example, the low threshold is set to 50 and the high threshold to 150, etc. The specific values need to be adjusted according to factors such as image contrast).
[0098] In this embodiment, the similarity calculation module includes:
[0099] The image scaling unit is used to scale the image to a specified size. It's worth noting that in OpenCV, the image scaling unit can be implemented using the `cv2.resize` function. This function takes the original image to be processed, the specified output size (which can be represented as a tuple, such as `(width, height)`, for example, `(200, 200)` means scaling the image to 200 pixels wide and 200 pixels high), and the scaling interpolation method (common interpolation methods include `cv2.INTER_LINEAR` (linear interpolation, suitable for enlarging or reducing images) and `cv2.INTER_NEAREST` (nearest neighbor interpolation, fast but potentially less effective)) as parameters.
[0100] The grayscale processing unit is used to perform grayscale processing on the image. It should be noted that in OpenCV, the grayscale processing unit can be implemented using the cv2.cvtColor function, which takes the original image and the target color space mode for conversion as parameters (here, the mode corresponding to converting to grayscale is cv2.COLOR_BGR2GRAY, assuming the original image is a color image in BGR format).
[0101] The pixel calculation unit is used to calculate the average value of each row of pixels by iterating through each row of pixels in the image. Each average value is used as a feature representation of the pixels in that row. The overall features of the image can then be further analyzed based on these row features.
[0102] The variance calculation unit uses NumPy library functions to calculate the variance of the average values of all previously obtained row pixels. This variance is used as a feature value of the image to measure the fluctuation of the features of each row pixel. It should be noted that the variance value reflects the dispersion of the image features at the row pixel level. The larger the variance, the greater the fluctuation of features such as the average brightness of each row pixel. To a certain extent, it can reflect the differences in the content or quality of the image and can be used for subsequent analysis operations such as similarity comparison with other images.
[0103] In this embodiment, the threshold calculation module includes:
[0104] The curve generation unit, after acquiring images at different time points according to the timed frame extraction module, sequentially executes the image enhancement module and the similarity calculation module for each image to obtain the variance value of each image. These variance values are then arranged in chronological order to generate a historical curve of the variance values. It should be noted that, to facilitate the chronological arrangement and visualization of the variance values (generating the historical curve), the curve generation unit can specifically use data processing libraries (such as pandas) and plotting libraries (such as matplotlib) in Python. In this way, the changing trend of the variance values over time can be clearly and intuitively observed, providing basic data support for subsequent analysis of the dynamic changes of image features and the determination of dynamic thresholds.
[0105] The mean calculation unit uses a simple moving average algorithm as a dynamic threshold calculation method to remove the highest and lowest values to reduce outlier interference. It defines a time window size and calculates the average variance difference corresponding to that time window. It's important to note that the choice of time window size is crucial; too small a window may fail to effectively smooth outlier interference, while too large a window may ignore short-term changes in image features. The size needs to be reasonably determined based on the characteristics of the actual image data and the analysis requirements. The formulas involved in this process are as follows:
[0106]
[0107] Where n is the defined time window size, t is the comparison time point, and x is the original similarity value of each past comparison;
[0108] The threshold calculation unit is used to determine the dynamic similarity threshold for each time point through statistical analysis after obtaining the moving average. If the similarity is higher than the dynamic threshold, it is considered that there is a difference between the image at the current time point and the reference image.
[0109] In summary, the method and apparatus of the present invention for improving the accuracy of video image difference detection can effectively improve the accuracy of image difference comparison and avoid false alarms.
[0110] The above specific examples illustrate the principles and implementation methods of the present invention in detail. These embodiments are merely for the purpose of helping to understand the core technical content of the present invention. Based on the above specific embodiments of the present invention, any improvements and modifications made to the present invention by those skilled in the art without departing from the principles of the present invention should fall within the patent protection scope of the present invention.
Claims
1. A method for improving the accuracy of video image difference detection, characterized in that: The steps include: S1. Use OpenCV video image processing technology to extract frames from surveillance videos of the same device at the same location at different time points to obtain images; S2, using OpenCV's image enhancement technology to achieve image enhancement and clarity enhancement; S3, using image processing technology to calculate the variance value of the two images, which is used to assist in the subsequent evaluation of their similarity; S4. Repeat steps S1-S3 to generate a historical curve of variance values by performing similarity comparison analysis on images extracted at different time points; based on the historical curve of variance values, a simple moving average algorithm is used to calculate the dynamic threshold of each time point, and the dynamic threshold is used to determine whether there is a difference between the image at the current time point and the reference image.
2. A method for improving the accuracy of video image difference detection according to claim 1, characterized in that: When executing step S1, a timed task is set; after obtaining the surveillance video of the same device at the same location, the surveillance video is framed at set intervals to obtain images by executing the timed task.
3. The method for improving the accuracy of video image difference detection according to claim 1, characterized in that: The step S2 specifically includes the following operations: S2.1, Image denoising: Use OpenCV to read a grayscale image and apply GaussianBlur function to denoise; S2.2, histogram equalization: use OpenCV's equalizeHist function to perform histogram equalization on the image; S2.3, Image sharpening: Use OpenCV's Laplacian function to perform Laplacian filtering on the image; S2.4, non-local mean denoising: use OpenCV's fastNlMeansDenoising function to perform non-local mean denoising on the image; S2.5, Adaptive histogram equalization: Use the createCLAHE function of OpenCV to create a CLAHE object, and use the apply method of the CLAHE object to perform adaptive histogram equalization on the image; S2.6, edge enhancement filtering: Use OpenCV's Sobel function to perform Sobel filtering on the image in the horizontal and vertical directions respectively, and calculate the combination of these two directions; S2.
7. Canny edge detection: Use OpenCV's Canny function to perform edge detection on the image.
4. The method for improving the accuracy of video image difference detection according to claim 1, characterized in that: The step S3 specifically includes the following operations: S3.1, scaling the image to be processed to a specified size; S3.2, grayscale processing of the image; S3.3, by looping through each row of pixels in the image, calculating the average value of each row of pixels, each average value is used as a feature representation of the row of pixels; S3.
4. Calculate the variance of all the average values obtained, and use the obtained variance as the characteristic value of the image, reflecting the fluctuation of the pixel characteristics of each row.
5. The method for improving the accuracy of video image difference detection according to claim 4, characterized in that: The step S4 specifically includes the following operations: S4.1, after obtaining images at different time points according to step S1, perform steps S2 and S3 in sequence for each image to obtain the variance value of each image, and arrange these variance values in chronological order, that is, generate a historical curve of the variance values; S4.
2. Use the simple moving average algorithm as the dynamic threshold calculation method, remove the highest and lowest values to reduce the interference of abnormal points, define a time window size, and calculate the average value of the variance difference corresponding to the time window; S4.
3. After obtaining the moving average, a dynamic threshold of similarity at each time point is determined through statistical analysis. If the dynamic threshold is higher than the dynamic threshold, it is considered that there is a difference between the image at the current time point and the reference image.
6. A device for improving the accuracy of video image difference detection, characterized in that: It includes: The timed frame extraction module is used to extract frames from the surveillance video of the same device at the same location at different time points using OpenCV's video image processing technology to obtain images; Image enhancement module, used to use OpenCV's image enhancement technology to achieve image enhancement and clarity enhancement; A similarity calculation module is used to calculate the variance value of two images using image processing technology to assist the threshold calculation module in determining their similarity; The threshold calculation module is used to first generate a historical curve of variance values by performing similarity comparison analysis on images extracted at different time points, and then calculate the dynamic threshold of each time point using a simple moving average algorithm based on the historical curve of variance values. The dynamic threshold is used to determine whether there is a difference between the image at the current time point and the reference image.
7. The device for improving the accuracy of video image difference detection according to claim 6, characterized in that: The timed frame extraction module first sets a timed task, then obtains the surveillance video of the same device at the same location, executes the timed task, and extracts frames from the surveillance video at set intervals to obtain images.
8. The device for improving the accuracy of video image difference detection according to claim 6, characterized in that: The image enhancement module specifically includes: Image denoising unit, used to read a grayscale image using OpenCV and apply GaussianBlur function for denoising; The histogram equalization unit is used to perform histogram equalization on the image using the equalizeHist function of OpenCV; Image sharpening unit, used to perform Laplacian filtering on images using OpenCV's Laplacian function; Mean denoising unit, used to perform non-local mean denoising on images using OpenCV's fastNlMeansDenoising function; The adaptive equalization unit is used to create a CLAHE object using the createCLAHE function of OpenCV, and use the apply method of the CLAHE object to perform adaptive histogram equalization on the image; Enhanced filter unit, used to use OpenCV's Sobel function to perform Sobel filtering on the image in the horizontal and vertical directions respectively, and calculate the combination of these two directions; The edge detection unit is used to perform edge detection on the image using the Canny function of OpenCV.
9. The device for improving the accuracy of video image difference detection according to claim 6, characterized in that: The similarity calculation module comprises: An image scaling unit, used for scaling the image to be processed to a specified size; A grayscale processing unit, used for performing grayscale processing on an image; A pixel calculation unit, used for calculating the average value of each row of pixels by looping through each row of pixels of the image, each average value being used as a feature representation of the row of pixels; The variance calculation unit is used to calculate the variance of all the obtained average values. The obtained variance is used as the characteristic value of the image, reflecting the fluctuation of the pixel characteristics of each row.
10. The device for improving the accuracy of video image difference detection according to claim 9, characterized in that: The threshold calculation module comprises: The curve generating unit is used to extract frames of surveillance videos of the same device and the same location at different time points, execute the image enhancement module and the similarity calculation module, obtain the variance value of each image, and arrange these variance values in chronological order, that is, generate a historical curve of the variance value; The mean calculation unit is used to use a simple moving average algorithm as a dynamic threshold calculation method, remove the highest and lowest values to reduce the interference of abnormal points, define a time window size, and calculate the average value of the variance difference corresponding to the time window; The threshold calculation unit is used to determine the dynamic threshold of similarity at each time point through statistical analysis after obtaining the moving average value. If the dynamic threshold is higher than the dynamic threshold, it is considered that there is a difference between the image at the current time point and the reference image.