Drug image detection method based on improved Canny operator
Through the improved bilateral filter and four-direction Soble operator combined with non-maximum suppression and Otsu adaptive threshold extraction method, the drug image is processed, solving the problem of edge blur and detail loss in noise complex image processing in the prior art. , achieving higher edge detection accuracy and detail retention.
Patent Information
- Application Number
- CN202411484262.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2025-02-07
AI Technical Summary
When the prior art deals with drug images with complex noise and many edge details, it is difficult to effectively suppress noise and retain target edges and detailed features of the image.
The improved bilateral filter and four-direction Soble operator are used to filter and gradient extraction of drug images, combining improved non-maximum suppression and an Otsu adaptive threshold extraction method based on gradient edge information.
Effectively suppress noise, improve the edge detection accuracy of the image, retain rich weak edge information, and enhance the segmentation accuracy of image details and edges.
Smart Images

Figure CN119399157A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a medicine image detection method based on an improved Canny operator. Background Art
[0002] Controlling the quality of drugs is extremely important. By testing drugs, the drug production qualification rate can be improved, thereby eliminating the possibility of consumers buying drugs with quality problems.
[0003] Image edge detection technology is one of the important technologies in digital image processing, which is used to detect and enhance the edges and contours of objects in images. Image edge refers to the place where the gray value in the image changes suddenly, usually corresponding to the edge or contour between objects. Edge information plays a key role in applications such as target detection, image segmentation, and feature extraction. In image edge processing, commonly used first-order differential operators include Roberts operator, Kirsch operator, Sobel operator, etc., and second-order differential operators include Laplace operator, LoG operator, DoG operator, etc. Among them, the first-order operator is more universal, but it cannot meet the requirements for processing images with complex noise and more edge details. The second-order operator generally uses Gaussian filtering, which has a better filtering effect on Gaussian noise. When the image contains a fusion of multiple noises, Gaussian filtering will cause edge blur. Based on this, the present invention designs a drug image detection method based on an improved Canny operator. Summary of the invention
[0004] The present invention aims to solve the problems existing in the above-mentioned prior art and provide a drug image detection method based on an improved Canny operator.
[0005] The technical solutions adopted in the present invention are:
[0006] A drug image detection method based on an improved Canny operator comprises the following steps:
[0007] 1) Collect drug images and perform grayscale processing on the drug images;
[0008] 2) Using an improved bilateral filter to filter the grayscaled drug image and suppress noise;
[0009] 3) Use the four-directional Soble operator to extract the gradient amplitude and phase of the drug image;
[0010] 4) Improved non-maximum suppression is used to perform non-maximum suppression on the extracted gradient amplitude and phase to accurately locate the edge of the target;
[0011] 5) Using the improved Otsu based on gradient edge information, adaptive threshold extraction is performed to obtain the edge contour of the drug image;
[0012] 6) Output drug images.
[0013] Furthermore, in step 2), the improvement of the bilateral filter is to improve the weighted function coefficients in the bilateral filter, and the process is:
[0014] In a bilateral filter, the weighted value of the neighborhood pixel values determines the value of the output pixel, as expressed below:
[0015]
[0016] in,
[0017] (i, j) is the coordinate point of other coefficients of the template window, (k, l) is the coordinate point of the center of the template window, f(i, j) is the pixel value of point (i, j), f(k, l) is the pixel value of point (k, l), ω(i, j, k, l) is the weighting coefficient; σ d is the spatial standard deviation, σ r is the grayscale standard deviation;
[0018] The weighted function coefficient ω(i, j, k, l) is obtained by multiplying the domain kernel with the improved range kernel. The domain kernel is as follows:
[0019]
[0020] The improvement of the range kernel is:
[0021] Construct bilateral filter threshold:
[0022] T=exp(-σ)
[0023] T is the bilateral filtering threshold, σ is the standard deviation of the image;
[0024] The improved range kernel function is as follows:
[0025]
[0026] Δ is the difference between the grayscale values of the center pixel and other pixels, and L is the grayscale level of the image;
[0027] The weighted function coefficient ω(i, j, k, l) is specifically as follows:
[0028]
[0029] Furthermore, the specific process of step 3) is as follows:
[0030] The 2×2 convolution template in the Canny operator is expanded to a 3×3 Soble convolution template, and two gradient directions of 45° and 135° are added on the basis of the original horizontal and vertical gradient directions. The matrix templates of the four directions are as follows:
[0031]
[0032] The pixel gray value f(x, y) is traversed through four matrix templates, and the calculation formula is as follows:
[0033] G x =(f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1))-
[0034] (f(x-1,y-1)+2f(x-1,y)+f(x-1,y+1))
[0035] G y =(f(x-1,y-1)+2f(x,y-1)+f(x+1,y-1))-
[0036] (f(x-1,y+1)+2f(x,y+1)+f(x+1,y+1))
[0037] G 45° =(f(x,y-1)+2f(x+1,y-1)+f(x+1,y))-
[0038] (f(x-1,y)+2f(x-1,y+1)+f(x,y+1))
[0039] G 135° =(f(x-1,y)+2f(x-1,y-1)+f(x,y-1))-
[0040] (f(x+1,y)+2f(x+1,y+1)+f(x,y+1))
[0041] Get the gradient components in four directions, represented as G x , G y , G 45° and G 135° , and finally calculate the gradient amplitude and gradient direction through these four gradient components:
[0042]
[0043] Further, in step 4), the non-maximum suppression method is improved to include:
[0044] In the non-maximum suppression process of the Canny operator, the 8-neighborhood is expanded to 16-neighborhood, and the number of pixels corresponding to the gradient direction angle is increased from 4 to 8, while satisfying:
[0045] P(x,y)>M(x+i,y+j)+D(x+i,y+j)
[0046] P(x,y)>M(xi,yj)+D(xi,yj)
[0047] in,
[0048]
[0049] P(x, y) is the coordinate of the pixel to be judged, D is the mean of the local neighborhood deviation values of each point in the 3×3 neighborhood, and M is the corresponding point replaced by the mean value in the neighborhood.
[0050] Further, in step 5), the improved Otsu adaptive threshold extraction is specifically as follows:
[0051] All unsuppressed edge points are divided into foreground target class and background class according to grayscale value, and unsuppressed points are evenly divided into 64 levels according to gradient amplitude;
[0052]
[0053] Where: S—the number of all pixels that are not suppressed;
[0054] n i —The number of pixels at level i;
[0055] p i —The probability of the i-th level among all extreme points;
[0056] The target gradient mean and background gradient mean correspond to:
[0057]
[0058] Where: T—threshold for distinguishing target points from background points;
[0059] — target gradient probability;
[0060] —Background gradient probability;
[0061] The mean gradient of all unsuppressed edge points is:
[0062] u=u b (T)ω b (T)+u o (T)ω o (T)
[0063] The variance between target points and background points is:
[0064] σ 2 (T) = ω b (T)[u b (T)-u] 2 +ω o(T)[u o (T)-u] 2
[0065] When the inter-class variance is the largest, the probability of misclassification between background points and target points is the smallest;
[0066] Select the gradient amplitude T corresponding to the maximum inter-class variance h as a high threshold;
[0067]
[0068] If T h If the number is greater than 1, select T h The minimum value of the high threshold value is T l Select T l =T h *0.4.
[0069] The present invention has the following beneficial effects:
[0070] 1) The improved bilateral filter filters the image, and adjusts the weight value of the edge pixel points identified by the bilateral filter to zero, effectively reducing the interference to the central pixel points, thereby maximally retaining the target edge and detail features of the image;
[0071] 2) Improved non-maximum suppression method to improve the accuracy of non-edge point judgment and avoid edge point suppression;
[0072] 3) Improved Otsu adaptive threshold selection improves the segmentation accuracy of image details and edges and retains rich weak edge information. BRIEF DESCRIPTION OF THE DRAWINGS
[0073] Figure 1 It is a flow chart of the present invention.
[0074] Figure 2 is the gradient direction corresponding to the (3×3) neighborhood.
[0075] Figure 3 is the number of the neighborhood center point corresponding to the gradient direction θ.
[0076] Figure 4 Drug image after filtering.
[0077] Figure 5 The edge contour map is extracted after non-maximum suppression and threshold processing. DETAILED DESCRIPTION
[0078] The present invention will be further described below in conjunction with the accompanying drawings.
[0079] like Figure 1As shown, the present invention provides a drug image detection method based on an improved Canny operator, and the steps are as follows:
[0080] 1) After the drug processing is completed, it is transported to the bottom of the CCD camera via a conveyor belt to collect images and then fed back to the PC for grayscale processing.
[0081] 2) An improved bilateral filter is used to filter the grayscaled drug image to suppress noise.
[0082] In a bilateral filter, the weighted value of the neighborhood pixel values determines the value of the output pixel, as expressed below:
[0083]
[0084] in,
[0085] (i, j) is the coordinate point of other coefficients of the template window, (k, l) is the coordinate point of the center of the template window, f(i, j) is the pixel value of point (i, j), f(k, l) is the pixel value of point (k, l), ω(i, j, k, l) is the weighting coefficient; σ d is the spatial standard deviation, σ r is the grayscale standard deviation.
[0086] The weighted function coefficient ω(i, j, k, l) is obtained by multiplying the domain kernel with the improved range kernel. The domain kernel is as follows:
[0087]
[0088] Since the bilateral filter takes into account the weight of the grayscale information of the pixel, in order to avoid each pixel participating in the filtering with a certain weight, thereby causing an adverse effect on the center point, the range kernel function is improved.
[0089] Construct bilateral filter threshold:
[0090] T=exp(-σ)
[0091] T is the bilateral filter threshold, and σ is the standard deviation of the image.
[0092] The improved range kernel function is as follows:
[0093]
[0094] Δ is the difference between the grayscale values of the central pixel and other pixels, and L is the grayscale level of the image.
[0095] The domain kernel is multiplied by the improved range kernel to obtain the weighted function coefficient ω(i, j, k, l), as shown below:
[0096]
[0097] By adjusting the weight values of edge pixels identified by the bilateral filter to zero, the interference to the central pixels is effectively reduced, thereby maximally retaining the target edges and detail features of the image.
[0098] 3) Use four-directional Soble operator to extract gradient amplitude and phase of drug images;
[0099] The 2×2 convolution template in the traditional Canny edge detection algorithm is expanded to a 3×3 Soble convolution template, and two gradient directions of 45° and 135° are added to the original horizontal and vertical gradient directions to retain more edge information. The matrix templates in the four directions are as follows:
[0100]
[0101] The pixel gray value f(x, y) is traversed through the above four matrix templates, and the specific calculation formula is as follows:
[0102] G x =(f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1))-
[0103] (f(x-1,y-1)+2f(x-1,y)+f(x-1,y+1))
[0104] G y =(f(x-1,y-1)+2f(x,y-1)+f(x+1,y-1))-
[0105] (f(x-1,y+1)+2f(x,y+1)+f(x+1,y+1))
[0106] G 45° =(f(x,y-1)+2f(x+1,y-1)+f(x+1,y))-
[0107] (f(x-1,y)+2f(x-1,y+1)+f(x,y+1))
[0108] G 135° =(f(x-1,y)+2f(x-1,y-1)+f(x,y-1))-
[0109] (f(x+1,y)+2f(x+1,y+1)+f(x,y+1))
[0110] Get the gradient components in four directions, represented as G x , G y , G 45° and G 135°, and finally calculate the gradient amplitude and gradient direction through these four components:
[0111]
[0112] 4) Improved non-maximum suppression is used to perform non-maximum suppression on the gradient amplitude, eliminating some non-edge points and accurately locating the target edge.
[0113] like Figure 2 and Figure 3 , using non-maximum suppression can eliminate some non-edge points. Direct comparison within the 8-neighborhood is likely to cause edge points to be suppressed by noise gradient points, leading to misjudgment. To address this problem, the 8-neighborhood is expanded to 16-neighborhood. The present invention increases the number of pixel points corresponding to the gradient direction angle from 4 to 8. Using the corresponding pixel point (3×3) neighborhood (such as Figure 2 The corresponding point is replaced by the internal mean M as shown in the figure, and the local neighborhood deviation mean D of each point in the (3×3) neighborhood of the corresponding point is calculated.
[0114] The edge point to be judged is compared with the sum of the mean of multiple points in the neighborhood in the gradient direction and the mean of the local neighborhood deviation. If the edge point to be judged is a maximum value, the edge point is retained.
[0115] P(x,y)>M(x+i,y+j)+D(x+i,y+j)
[0116] P(x,y)>M(xi,yj)+D(xi,yj)
[0117] in,
[0118]
[0119] 5) Use the improved Otsu based on gradient edge information to perform adaptive threshold extraction, obtain the edge contour of the drug image, and finally output the drug image. Figure 4 is a drug image, Figure 5 The edge contour map is extracted after non-maximum suppression and threshold processing.
[0120] All unsuppressed edge points are divided into foreground target class and background class according to grayscale value. Unsuppressed points are evenly divided into 64 levels according to gradient amplitude.
[0121]
[0122] Where: S—the number of all pixels that are not suppressed
[0123] n i —The number of pixels at level i;
[0124] p i —The probability of the i-th level being among all extreme points.
[0125] The target gradient mean and background gradient mean are:
[0126]
[0127] Where: T—threshold for distinguishing target points from background points;
[0128] — target gradient probability;
[0129] —Background gradient probability.
[0130] The mean gradient of all unsuppressed edge points is:
[0131] u=u b (T)ω b (T)+u o (T)ω o (T)
[0132] The variance between target points and background points is:
[0133] σ 2 (T) = ω b (T)[u b (T)-u] 2 +ω o (T)[u o (T)-u] 2
[0134] When the inter-class variance is the largest, the probability of misclassification between background points and target points is the smallest.
[0135] Select the gradient amplitude T corresponding to the maximum inter-class variance h as the high threshold.
[0136]
[0137] If T h The number is greater than 1, in order to obtain rich weak edge information, select T h The minimum value of is the high threshold. The low threshold T l Select T l =T h *0.4.
[0138] The above description is only a preferred embodiment of the present invention. It should be pointed out that a person skilled in the art can make several improvements without departing from the principle of the present invention, and these improvements should also be regarded as within the protection scope of the present invention.
Claims
1. A drug image detection method based on an improved Canny operator, characterized in that: The steps include: 1) Collect drug images and perform grayscale processing on the drug images; 2) Using an improved bilateral filter to filter the grayscaled drug image and suppress noise; 3) Use the four-directional Soble operator to extract the gradient amplitude and phase of the drug image; 4) Improved non-maximum suppression is used to perform non-maximum suppression on the extracted gradient amplitude and phase to accurately locate the edge of the target; 5) Using the improved Otsu based on gradient edge information, adaptive threshold extraction is performed to obtain the edge contour of the drug image; 6) Output the edge contour of the drug image.
2. The drug image detection method based on the improved Canny operator as claimed in claim 1, characterized in that: In step 2), the improvement of the bilateral filter is to improve the weighted function coefficients in the bilateral filter, and the process is: In a bilateral filter, the weighted value of the neighborhood pixel values determines the value of the output pixel, as expressed below: in, (i, j) is the coordinate point of other coefficients of the template window, (k, l) is the coordinate point of the center of the template window, f(i, j) is the pixel value of point (i, j), f(k, l) is the pixel value of point (k, l), ω(i, j, k, l) is the weighting coefficient; σ d is the spatial standard deviation, σ r is the grayscale standard deviation; The weighted function coefficient ω(i, j, k, l) is obtained by multiplying the domain kernel with the improved range kernel. The domain kernel is as follows: The improvement of the range kernel is: Construct bilateral filter threshold: T=exp(-σ) T is the bilateral filtering threshold, σ is the standard deviation of the image; The improved range kernel function is as follows: Δ is the difference between the grayscale values of the center pixel and other pixels, and L is the grayscale level of the image; The weighted function coefficient ω(i, j, k, l) is specifically as follows:
3. The drug image detection method based on the improved Canny operator as claimed in claim 1, characterized in that: Step 3) The specific process is: The 2×2 convolution template in the Canny operator is expanded to a 3×3 Soble convolution template, and two gradient directions of 45° and 135° are added on the basis of the original horizontal and vertical gradient directions. The matrix templates of the four directions are as follows: The pixel gray value f(x, y) is traversed through four matrix templates, and the calculation formula is as follows: G x (f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1))-(f(x-1,y-1)+2f(x-1,y)+f(x-1,y+1)) G y (f(x-1,y-1)+2f(x,y-1)+f(x+1,y-1))-(f(x-1,y+1)+2f(x,y+1)+f(x+1,y+1)) G 45° (f(x,y-1)+2f(x+1,y-1)+f(x+1,y))-(f(x-1,y)+2f(x-1,y+1)+f(x,y+1)) G 135° (f(x-1,y)+2f(x-1,y-1)+f(x,y-1))-(f(x+1,y)+2f(x+1,y+1)+f(x,y+1)) Get the gradient components in four directions, represented as G x , G y , G 45° and G 135° , and finally calculate the gradient amplitude and gradient direction through these four gradient components:
4. The drug image detection method based on the improved Canny operator as claimed in claim 1, characterized in that: In step 4), the non-maximum suppression method is improved to include: In the non-maximum suppression process of the Canny operator, the 8-neighborhood is expanded to 16-neighborhood, and the number of pixels corresponding to the gradient direction angle is increased from 4 to 8, while satisfying: P(x,y)>M(x+i,y+j)+D(x+i,y+j) P(x,y)>M(xi,yj)+D(xi,yj) in, P(x, y) is the coordinate of the pixel to be judged, D is the mean of the local neighborhood deviation values of each point in the 3×3 neighborhood, and M is the corresponding point replaced by the mean value in the neighborhood.
5. The drug image detection method based on the improved Canny operator as claimed in claim 1, characterized in that: In step 5), the improved Otsu adaptive threshold extraction is specifically as follows: All unsuppressed edge points are divided into foreground target class and background class according to grayscale value, and unsuppressed points are evenly divided into 64 levels according to gradient amplitude; Where: S—the number of all pixels that are not suppressed; n i —The number of pixels at level i; p i —The probability of the i-th level among all extreme points; The target gradient mean and background gradient mean correspond to: Where: T—threshold for distinguishing target points from background points; — target gradient probability; —Background gradient probability; The mean gradient of all unsuppressed edge points is: u=u b (T)ω b (T)+u o (T)ω o (T) The variance between target points and background points is: σ 2 (T)=ω b (T)[u b (T)-u] 2 +ω o (T)[u o (T)-u] When the inter-class variance is the largest, the probability of misclassification between background points and target points is the smallest; Select the gradient amplitude T corresponding to the maximum inter-class variance h as a high threshold; If T h If the number is greater than 1, select T h The minimum value of the high threshold value is T l Select T l =T h *0.4.
Citation Information
Cited By
Silver electrode substrate image quality detection method based on machine vision
CN120088262A
A method for detecting the image quality of a silver electrode substrate based on machine vision
CN120088262B
Star map stray light processing method based on multidirectional gradient
CN121074076A
Flour impurity screening method and system based on image processing
CN121391882A
Scene filtering enhancement method and device under sky background based on thermal imaging and medium
CN121563782A