A method and system for detecting foreign matter in a zongzi
Patent Information
- Application Number
- CN202610797773.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-04
- Publication Date
- 2026-10-09
AI Technical Summary
[0003]现有食品内部异物检测方式中,通常采用人工抽检、金属探测或X射线透视检测等方式,人工抽检依赖经验判断,难以对每个待检测的粽子进行连续检测;金属探测主要针对金属类异物,对非金属异物的适应性有限;普通X射线透视检测虽然能够形成食品内部影像,但在粽子内部食材密度差异较大、粽叶包裹导致边缘纹理复杂、馅料与异物灰度差异局部相近的情况下,容易出现边界识别不稳定、疑似区域连接不完整以及异物区域误判的问题
[0048]本发明通过控制X射线发生装置向待检测的粽子发射X射线,并通过接收装置获取穿透能量,生成粽子X射线透视图,使后续检测基于能够反映待检测的粽子内部密度差异的图像数据进行;再提取各个像素点的灰度值及预设邻域内的相邻像素点的灰度值,计算灰度值梯度差值,得到灰度变化梯度,形成连续的数据处理链路。
Smart Images

Figure CN122882485A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of detection technology, specifically a method and system for detecting foreign objects inside a zongzi (sticky rice dumpling). Background Technology
[0002] As a food containing glutinous rice, fillings, bamboo leaves, and binding materials, zongzi typically has a strong wrapping effect, complex layers of ingredients, and uneven density distribution. During the production, packaging, and distribution testing processes, it is necessary to test whether the zongzi contain stones, metal fragments, hard bone fragments, or other high-density foreign objects to reduce the risk of zongzi containing foreign objects entering subsequent distribution stages.
[0003] Existing methods for detecting foreign objects inside food typically involve manual sampling, metal detection, or X-ray inspection. Manual sampling relies on experience and judgment, making it difficult to continuously inspect each rice dumpling. Metal detection is mainly for metallic foreign objects and has limited adaptability to non-metallic foreign objects. Although ordinary X-ray inspection can create images of the food's interior, it is prone to problems such as unstable boundary recognition, incomplete connection of suspected areas, and misjudgment of foreign object areas when there are large differences in the density of ingredients inside the rice dumpling, complex edge textures caused by the wrapping of the rice dumpling leaves, and similar grayscale differences between the filling and the foreign object. Summary of the Invention
[0004] The purpose of this invention is to address the aforementioned problems in existing technologies by proposing a method and system for detecting foreign objects inside zongzi (sticky rice dumplings).
[0005] The objective of this invention can be achieved through the following technical solutions:
[0006] Firstly, in order to solve the above-mentioned technical problems, the present invention provides a method for detecting foreign objects inside a zongzi (sticky rice dumpling), comprising:
[0007] The X-ray generator is controlled to emit X-rays towards the rice dumpling to be inspected, and the penetrating energy is obtained through the receiving device to generate an X-ray perspective view of the rice dumpling;
[0008] Extract the grayscale value of each pixel in the X-ray perspective image of the zongzi, and obtain the grayscale value of adjacent pixels in a preset neighborhood;
[0009] Calculate the grayscale gradient difference between the pixel and its neighboring pixels to obtain the grayscale change gradient of each pixel.
[0010] Pixels whose grayscale change gradient is greater than a preset gradient threshold are marked as suspected boundary points;
[0011] Connect the suspected boundary points whose spatial distance is less than a preset distance threshold to construct a closed connected region;
[0012] Extract the grayscale values of all pixels within the connected region, calculate the average grayscale value of the region, and calculate the pixel area of the connected region;
[0013] When the average gray value of the area is lower than the preset lower limit of food gray value, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains foreign objects.
[0014] Furthermore, before extracting the grayscale values of each pixel in the X-ray perspective image of the zongzi, the method further includes:
[0015] The X-ray perspective view of the zongzi is smoothed and denoised using a median filtering algorithm with a preset window size to eliminate impulse noise, resulting in a denoised X-ray perspective view of the zongzi.
[0016] Furthermore, the method also includes:
[0017] The foreground mask of the denoised X-ray perspective view of the zongzi was obtained by threshold segmentation using the Otsu method;
[0018] The background of the zongzi X-ray perspective image is removed using the foreground mask to extract the effective detection area that mainly contains the zongzi to be detected;
[0019] The step of extracting the grayscale value of each pixel in the X-ray perspective image of the zongzi specifically involves: extracting the grayscale value of each pixel within the effective detection area.
[0020] Further, the grayscale gradient difference between the pixel and its neighboring pixels is calculated to obtain the grayscale change gradient of each pixel, including:
[0021] The Sobel edge detection operator is used to perform convolution operations on the gray values of adjacent pixels within the preset neighborhood.
[0022] The first gray-level gradient component in the horizontal direction and the second gray-level gradient component in the vertical direction of the pixel are extracted respectively;
[0023] The first gray-level gradient component and the second gray-level gradient component are combined by amplitude synthesis to calculate the gray-level change gradient of each pixel.
[0024] Furthermore, connecting the suspected boundary points whose spatial distance is less than the preset distance threshold to construct the closed connected region includes:
[0025] Define a morphological structural element that contains multiple pixels;
[0026] The morphological structuring elements are used to perform morphological closing operations on all the marked suspected boundary points.
[0027] The morphological closing operation is used to bridge the gaps of broken edges whose spatial distance is less than the preset distance threshold, and the region within the closed edge contour after connection is marked as the connected region.
[0028] Furthermore, after calculating the average gray value of the region, the method further includes:
[0029] Calculate the grayscale variance of the grayscale values of all pixels within the connected region;
[0030] When the average grayscale value of the area is lower than the preset lower limit of food grayscale, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains a foreign object, including:
[0031] When the average gray value of the region is lower than the preset lower limit of food gray value, and the pixel area is within the preset foreign object area range, the connected region is determined to be a suspected foreign object region.
[0032] Determine whether the grayscale variance of the suspected foreign object region is less than a preset homogeneity variance threshold;
[0033] If the grayscale variance is less than the preset homogeneity variance threshold, then the suspected foreign object area is confirmed to be a homogeneous high-density foreign object, and the rice dumpling to be detected is determined to contain a foreign object.
[0034] Furthermore, after extracting the grayscale values of all pixels within the connected region, the method further includes:
[0035] Obtain the position coordinates of the connected region within the effective detection region;
[0036] Based on the pixel distance from the position coordinates to the edge of the effective detection area, retrieve the corresponding thickness compensation coefficient;
[0037] The average gray value of the region is weighted and corrected using the thickness compensation coefficient to obtain the corrected average gray value of the region.
[0038] When the average gray value of the corrected area is lower than the preset lower limit of gray value of the food ingredient, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains a foreign object.
[0039] Secondly, in order to solve the above-mentioned technical problems, the present invention provides a foreign object detection system for zongzi (sticky rice dumplings), comprising:
[0040] The perspective view generation module is used to control the X-ray generator to emit X-rays towards the rice dumpling to be inspected, and to obtain the penetration energy through the receiving device to generate an X-ray perspective view of the rice dumpling.
[0041] The grayscale extraction module is used to extract the grayscale value of each pixel in the X-ray perspective image of the zongzi and obtain the grayscale value of adjacent pixels in a preset neighborhood.
[0042] The gradient calculation module is used to calculate the gradient difference of gray values between the pixel and its neighboring pixels, so as to obtain the gray value change gradient of each pixel.
[0043] The boundary point marking module is used to mark the pixels whose grayscale change gradient is greater than a preset gradient threshold as suspected boundary points.
[0044] The region construction module is used to connect the suspected boundary points whose spatial distance is less than a preset distance threshold to construct a closed connected region.
[0045] The feature calculation module is used to extract the gray values of all pixels in the connected region, calculate the average gray value of the region, and calculate the pixel area of the connected region.
[0046] The foreign object detection module is used to confirm that the rice dumpling to be detected contains foreign objects when the average gray value of the area is lower than the preset lower limit of food gray value and the pixel area is within the preset foreign object area range.
[0047] Compared with the prior art, this application has the following advantages:
[0048] This invention controls an X-ray generator to emit X-rays onto a rice dumpling to be inspected, and obtains the penetration energy through a receiving device to generate an X-ray perspective view of the rice dumpling. This allows subsequent inspections to be based on image data that reflects the density differences inside the rice dumpling. The gray values of each pixel and the gray values of adjacent pixels in a preset neighborhood are then extracted, and the gray value gradient difference is calculated to obtain the gray value change gradient, forming a continuous data processing link.
[0049] This invention marks pixels with grayscale gradients greater than a preset gradient threshold as suspected boundary points, and connects suspected boundary points with spatial distances less than a preset distance threshold to construct closed connected regions. This transforms the judgment object from scattered pixels into a region object with a spatial range, reducing the impact of isolated noise points on foreign object judgment. At the same time, it can be combined with median filtering algorithm, Otsu's threshold segmentation, Sobel edge detection operator, and morphological closing operation to improve the stability of suspected boundary point marking and connected region construction.
[0050] This invention extracts the grayscale values of all pixels within a connected region, calculates the region's average grayscale value, pixel area, and grayscale variance, and confirms that the zongzi to be detected contains foreign objects when the region's average grayscale value is lower than a preset lower limit for food grayscale, the pixel area is within a preset foreign object area range, and the grayscale variance is less than a preset homogeneity variance threshold. Furthermore, it can retrieve a thickness compensation coefficient based on the location coordinates to weight and correct the region's average grayscale value, reducing the impact of variations in the thickness of the zongzi on the judgment result. Attached Figure Description
[0051] Figure 1 This is a schematic diagram of the overall process of a method for detecting foreign objects inside a zongzi (sticky rice dumpling) according to the present invention;
[0052] Figure 2 This is a schematic diagram of the module structure of a foreign object detection system inside a zongzi (sticky rice dumpling) according to the present invention. Detailed Implementation
[0053] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.
[0054] The overall objective of this application is to transform the detection process of foreign objects inside a zongzi (sticky rice dumpling) from a simple grayscale judgment into a continuous processing chain involving "generating an X-ray perspective image of the zongzi, extracting grayscale values, calculating grayscale gradients, marking suspected boundary points, constructing connected regions, and comprehensively determining the average grayscale value and pixel area of the region." This overall chain starts with the image input formed by X-ray penetration energy, gradually transitions to pixel-level grayscale difference analysis, then to region-level feature calculation, and finally outputs a determination result as to whether the zongzi contains foreign objects. The foreign objects described in this application are those with a high density characteristic relative to normal ingredients.
[0055] A method for detecting foreign objects inside a zongzi (sticky rice dumpling), comprising:
[0056] The X-ray generator is controlled to emit X-rays towards the rice dumpling to be inspected, and the penetrating energy is obtained through the receiving device to generate an X-ray perspective view of the rice dumpling;
[0057] An X-ray generator emits X-rays towards the rice dumpling to be inspected. The X-rays pass through the dumpling and are received by a receiving device, which then generates a corresponding grayscale distribution based on the penetration energy at different locations. The output is an X-ray perspective view of the rice dumpling. The penetration energy reflects the energy changes of the X-rays after passing through different internal regions of the rice dumpling, and the X-ray perspective view of the rice dumpling carries the pixel data required for subsequent grayscale value extraction.
[0058] In some embodiments, the X-ray perspective image of the zongzi can be a single-channel grayscale image, with each pixel corresponding to a grayscale value. If a high-density foreign object exists within a certain area, the absorption or attenuation of X-rays in that area will differ from that of the surrounding food area, thus creating a corresponding grayscale difference in the X-ray perspective image of the zongzi. This step does not directly identify the foreign object, but rather generates an image basis that can be used for pixel grayscale value analysis in subsequent steps.
[0059] For example, a rice dumpling to be inspected is placed at the inspection conveyor position, and an X-ray generator is controlled to emit X-rays onto the rice dumpling with preset exposure parameters. A receiving device acquires the penetration energy on the other side of the rice dumpling. The receiving device converts the penetration energy into an X-ray perspective view of the rice dumpling with a size of 1024×1024 pixels, where the grayscale value of each pixel ranges from 0 to 255.
[0060] Extract the grayscale value of each pixel in the X-ray perspective image of the zongzi, and obtain the grayscale value of adjacent pixels in a preset neighborhood;
[0061] Iterate through each pixel in the X-ray perspective view of the zongzi, read the gray value of each pixel, determine a preset neighborhood with the current pixel as the center, and obtain the gray values of the adjacent pixels in the preset neighborhood.
[0062] The pixel is the smallest image unit in the X-ray perspective view of the zongzi (sticky rice dumpling); the grayscale value is used to represent the image intensity at the corresponding position of the pixel; the preset neighborhood is a local pixel range selected around the current pixel; the adjacent pixels are pixels located within the preset neighborhood and spatially adjacent to the current pixel. In some embodiments, the preset neighborhood can be a 3×3 pixel window, with the current pixel located at the center of the 3×3 pixel window, and the adjacent pixels being all pixels in the window except the current pixel.
[0063] In this step, obtaining the gray values of adjacent pixels is to enable the subsequent calculation of the gray value change between the current pixel and the surrounding local area. The output of this step includes not only the gray value of a single pixel, but also the local gray value distribution centered on that pixel, so that the subsequent calculation of gray value gradient difference has a data basis.
[0064] For example, in the aforementioned 1024×1024 pixel X-ray perspective image of a zongzi (rice dumpling), the pixel with coordinates (480, 520) and a grayscale value of 62 is selected. A 3×3 preset neighborhood is established with this pixel as the center, and the grayscale values of the adjacent pixels are obtained as 89, 91, 85, 64, 88, 90, 86, and 87, respectively.
[0065] Calculate the grayscale gradient difference between the pixel and its neighboring pixels to obtain the grayscale change gradient of each pixel.
[0066] The grayscale gradient difference is calculated based on the grayscale difference between a pixel and its neighboring pixels, and this grayscale gradient difference is converted into a grayscale gradient to characterize the intensity of local grayscale changes.
[0067] The grayscale gradient difference is used to represent the magnitude of the grayscale value change between a pixel and its neighboring pixels; the grayscale change gradient is used to represent the degree of grayscale abrupt change of the pixel's location relative to a preset neighborhood. The larger the grayscale change gradient, the more obvious the grayscale change near the pixel, and the more likely that location corresponds to a boundary change of the internal structure.
[0068] In some embodiments, the grayscale gradient can be calculated using the following formula:
[0069]
[0070] in, Indicates coordinates as The grayscale gradient of the pixels; Indicates coordinates as The grayscale value of the pixel; Represented by coordinates The grayscale values of adjacent pixels in the horizontal direction; Represented by coordinates The grayscale value of a pixel is the grayscale value of its vertically adjacent pixels. This formula illustrates that the grayscale gradient can be synthesized from the difference in grayscale gradient values in adjacent directions.
[0071] For example, continuing with the pixel at coordinates (480, 520), if I(480, 520) = 62, I(479, 520) = 89, and I(480, 519) = 91, then the grayscale gradient corresponding to this pixel is:
[0072] .
[0073] The grayscale gradient is used as input to determine whether a pixel is a suspected boundary point.
[0074] Pixels whose grayscale change gradient is greater than a preset gradient threshold are marked as suspected boundary points;
[0075] The grayscale gradient of each pixel is compared with a preset gradient threshold; when the grayscale gradient is greater than the preset gradient threshold, the pixel is marked as a suspected boundary point.
[0076] The preset gradient threshold is used to define the criteria for judging whether the grayscale change gradient reaches a suspected boundary point; the suspected boundary point is a pixel whose grayscale change gradient meets the condition of being greater than the preset gradient threshold. By comparing the threshold, pixels with prominent local grayscale changes are selected, so that the subsequent construction of connected regions is based on suspected boundary points, rather than based on all pixels of the entire X-ray perspective view of the zongzi.
[0077] For example, the preset gradient threshold is set to 30. The grayscale gradient of the pixel at coordinates (480, 520) is 39.62, which is greater than the preset gradient threshold of 30, so this pixel is marked as a suspected boundary point. If the grayscale gradient of the pixel at coordinates (481, 520) is 18.40, then this pixel is not marked as a suspected boundary point. After traversal, a set of suspected boundary points is output, consisting of multiple suspected boundary points.
[0078] Connect the suspected boundary points whose spatial distance is less than a preset distance threshold to construct a closed connected region;
[0079] Calculate the spatial distance between suspected boundary points. When the spatial distance between two or more suspected boundary points is less than a preset distance threshold, connect these suspected boundary points and form a closed boundary through the connection relationship, thereby constructing a closed connected region. The output result is one or more closed connected regions.
[0080] The spatial distance is the pixel distance between suspected boundary points in the X-ray perspective view of the rice dumpling; the preset distance threshold is used to limit the maximum distance between suspected boundary points that can be connected; the connected region is the area enclosed by the closed boundary formed by the connected suspected boundary points. This step can organize scattered suspected boundary points into region objects, so that the subsequent calculation of the average gray value and pixel area of the region has a clear spatial range.
[0081] In some embodiments, two suspected boundary points P1( ) and P2( The spatial distance d between them can be calculated using the following formula:
[0082]
[0083] in, Indicates spatial distance; Indicates the lateral coordinates of the suspected boundary point P1; Represents the longitudinal coordinates of the suspected boundary point P1; Indicates the lateral coordinates of the suspected boundary point P2; This represents the longitudinal coordinates of the suspected boundary point P2.
[0084] For example, the preset distance threshold is 4 pixels. If the coordinates of suspected boundary point P1 are (480, 520) and the coordinates of suspected boundary point P2 are (483, 521), then the spatial distance... The distance is less than the preset distance threshold of 4, therefore P1 and P2 are connected. By performing similar connection processing on the suspected boundary point set, a closed connected region is formed, the boundary of which covers the locations with large grayscale gradients. The size of the morphological structural element or the number of closing operation iterations is determined according to the preset distance threshold.
[0085] Extract the grayscale values of all pixels within the connected region, calculate the average grayscale value of the region, and calculate the pixel area of the connected region;
[0086] Identify all pixels within a connected region, extract the grayscale values of these pixels, average all grayscale values to obtain the average grayscale value of the region, and count the number of pixels within the connected region to obtain the pixel area.
[0087] The average grayscale value of the region is used to characterize the overall grayscale level of the connected region; the pixel area is used to characterize the spatial occupancy of the connected region in the X-ray perspective image of the zongzi. This step transforms the pixel-level grayscale information of the connected region into region-level feature information, enabling subsequent judgments to be made by combining grayscale and area.
[0088] In some embodiments, the average gray value of a region can be calculated using the following formula:
[0089]
[0090] in, This represents the average gray value of the area. This represents the grayscale value of the i-th pixel within a connected region; Σ represents the number of all pixels in the connected region; Σ represents the sum of the gray values of all pixels in the connected region; i represents the index of the pixel in the connected region.
[0091] Pixel area can be calculated using the following formula:
[0092]
[0093] in, Indicates pixel area; This represents the number of pixels within a connected region. Without considering physical size conversion, pixel area is expressed as the number of pixels.
[0094] For example, the aforementioned connected region contains a total of 180 pixels, i.e., pixel area A = 180. After extracting the grayscale values of these 180 pixels and summing them, we get 9720. Therefore, the average grayscale value of the region M = 9720 / 180 = 54. Thus, we obtain the average grayscale value of the region as 54 and the pixel area as 180.
[0095] When the average gray value of the area is lower than the preset lower limit of food gray value, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains foreign objects.
[0096] The average gray value of the region is compared with the preset lower limit of gray value of the food, and it is determined whether the pixel area is within the preset foreign object area range. When the average gray value of the region is lower than the preset lower limit of gray value of the food, and the pixel area is within the preset foreign object area range, the result confirming that the rice dumpling to be detected contains foreign objects is output.
[0097] The preset lower limit of food grayscale is used to define the lower boundary of the grayscale value of the normal food area; the preset foreign object area range is used to define the range of pixel areas that may correspond to the foreign object area in the X-ray perspective image of the zongzi; the conclusion of this step is that the zongzi to be detected contains a foreign object. This step requires that the average grayscale value of the area and the pixel area simultaneously meet the corresponding conditions to avoid misjudgment caused by relying solely on grayscale value or solely on area.
[0098] For example, the preset lower limit of grayscale for food ingredients is 70, and the preset range of foreign object area is 80 to 600 pixels. The average grayscale value of the aforementioned connected region is 54, which is lower than the preset lower limit of grayscale for food ingredients of 70; the pixel area is 180, which is within the preset range of 80 to 600 pixels for foreign object area. Therefore, it is confirmed that the rice dumpling to be detected contains foreign objects, and the foreign object detection result of the rice dumpling to be detected is output.
[0099] Furthermore, before extracting the grayscale values of each pixel in the X-ray perspective image of the zongzi, the method further includes:
[0100] The X-ray perspective view of the zongzi is smoothed and denoised using a median filtering algorithm with a preset window size to eliminate impulse noise, resulting in a denoised X-ray perspective view of the zongzi.
[0101] The X-ray perspective view of the zongzi is traversed with a preset window size. The gray values of the pixels in each window are sorted, and the gray value of the center pixel of the window is replaced with the sorted middle gray value, thereby eliminating impulse noise.
[0102] The preset window size is used to limit the range of pixels that participate in the sorting of the median filtering algorithm each time; the median filtering algorithm is used to suppress local abnormal bright spots or dark spots while preserving edge information; the impulse noise is the abnormal gray-scale point that stands out locally in the X-ray perspective image of the zongzi; the noise-reduced X-ray perspective image of the zongzi is used to replace the original X-ray perspective image of the zongzi in the subsequent gray-scale value extraction step.
[0103] In some embodiments, the preset window size can be a 3×3 pixel window. For the center pixel of the window, if the gray values within the 3×3 pixel window are 62, 64, 63, 250, 65, 64, 63, 62, and 64, where 250 is an abnormal gray value caused by impulse noise, then the sorted middle gray value is 64. This 64 is used to replace the original gray value of the center pixel, resulting in a smoother local gray value. For example, in the aforementioned 1024×1024 pixel X-ray perspective view of a rice dumpling, there are several isolated pixels with gray values of 0 or 255 in some areas. After smoothing and denoising using a median filtering algorithm with a preset window size of 3×3, these isolated pixels are replaced by the median gray value of their neighborhood, resulting in a denoised X-ray perspective view of the rice dumpling. Subsequently, the gray values of each pixel in the denoised X-ray perspective view of the rice dumpling are extracted, and the gray values of adjacent pixels within a preset neighborhood are further obtained.
[0104] Furthermore, the method also includes:
[0105] The foreground mask of the denoised X-ray perspective view of the zongzi was obtained by threshold segmentation using the Otsu method;
[0106] The background of the zongzi X-ray perspective image is removed using the foreground mask to extract the effective detection area that mainly contains the zongzi to be detected;
[0107] The step of extracting the grayscale value of each pixel in the X-ray perspective image of the zongzi specifically involves: extracting the grayscale value of each pixel within the effective detection area.
[0108] Building upon the previous step, a grayscale threshold that distinguishes the foreground from the background is calculated using the Otsu method for threshold segmentation. Based on this grayscale threshold, the pixels in the X-ray perspective view of the zongzi (sticky rice dumpling) are divided into foreground and background pixels, and a foreground mask is output. The foreground mask is applied to the X-ray perspective view of the zongzi, retaining the pixel positions corresponding to the zongzi to be detected while removing background pixels, thus extracting the effective detection region that mainly contains the zongzi to be detected. Each pixel within the effective detection region is traversed, and its grayscale value is extracted. The grayscale values of adjacent pixels within a preset neighborhood are also obtained, ultimately yielding the grayscale values of each pixel and its adjacent pixels within the effective detection region.
[0109] The Otsu threshold segmentation method is used to automatically determine the segmentation threshold based on the image grayscale distribution; the foreground mask is used to identify the foreground region corresponding to the rice dumpling to be detected in the X-ray perspective image of the rice dumpling; the pixel values in the foreground mask can be used to indicate whether the corresponding position belongs to the area where the rice dumpling to be detected is located; the background removal is used to remove background content in the X-ray perspective image of the rice dumpling that does not belong to the rice dumpling to be detected; the effective detection region is the image region that only contains the rice dumpling to be detected; the effective detection region is used to reduce the participation of background pixels in subsequent grayscale value extraction, grayscale change gradient calculation and connected component construction.
[0110] For example, applying Otsu's thresholding method to the denoised X-ray perspective image of a zongzi (sticky rice dumpling) yields a threshold of 118. Pixels whose grayscale values meet the foreground determination criteria are assigned a value of 1 in the foreground mask, while other pixels are assigned a value of 0. This creates a foreground mask used to distinguish the zongzi from the background area. The foreground mask is then multiplied at corresponding positions by the denoised X-ray perspective image of the zongzi. Positions with a value of 0 in the foreground mask are removed, while positions with a value of 1 are retained. After background removal, the effective detection area contains approximately 360,000 pixels. The system extracts grayscale values only from these 360,000 pixels, omitting grayscale value extraction from pixels in the background area. Therefore, when calculating the grayscale gradient, the input comes from pixels within the effective detection area and their neighboring pixels in a preset neighborhood.
[0111] Further, the grayscale gradient difference between the pixel and its neighboring pixels is calculated to obtain the grayscale change gradient of each pixel, including:
[0112] The Sobel edge detection operator is used to perform convolution operations on the gray values of adjacent pixels within the preset neighborhood.
[0113] The first gray-level gradient component in the horizontal direction and the second gray-level gradient component in the vertical direction of the pixel are extracted respectively;
[0114] The first gray-level gradient component and the second gray-level gradient component are combined by amplitude synthesis to calculate the gray-level change gradient of each pixel.
[0115] The Sobel edge detection operator is used to perform convolution operations on gray values within a preset neighborhood, and the output is a convolution result that describes the local gray value changes in the horizontal and vertical directions. The first gray value gradient component in the horizontal direction and the second gray value gradient component in the vertical direction of the pixel are obtained from the convolution operation result. The first gray value gradient component and the second gray value gradient component are combined to obtain the gray value change gradient of the pixel.
[0116] The Sobel edge detection operator is used to perform weighted calculations on the gray-level distribution within a preset neighborhood to enhance the gray-level difference in the edge direction; the first gray-level gradient component represents the intensity of gray-level change of a pixel in the horizontal direction; the second gray-level gradient component represents the intensity of gray-level change of a pixel in the vertical direction; by extracting the gradient components in the two directions respectively, the gray-level changes of boundaries in different directions can be included in the same gray-level change gradient calculation process.
[0117] For example, within a 3×3 preset neighborhood, the system reads the gray values of adjacent pixels and the center pixel, and performs convolution operations using the horizontal Sobel edge detection operator and the vertical Sobel edge detection operator respectively, to obtain intermediate results for extracting the first gray-level gradient component and the second gray-level gradient component.
[0118] For a pixel at coordinates (480, 520), after convolution using the Sobel edge detection operator, the first gray-level gradient component is 24, and the second gray-level gradient component is 31. These first and second gray-level gradient components are used as input for amplitude synthesis.
[0119] In some embodiments, amplitude synthesis can be calculated using the following formula:
[0120]
[0121] in, Represents the gradient of grayscale changes; This represents the first grayscale gradient component; This represents the second grayscale gradient component.
[0122] For example, the first grayscale gradient component of the aforementioned pixel. =24, second grayscale gradient component =31, then the grayscale gradient The grayscale gradient is compared with a preset gradient threshold to determine whether the pixel is marked as a suspected boundary point.
[0123] Furthermore, connecting the suspected boundary points whose spatial distance is less than the preset distance threshold to construct the closed connected region includes:
[0124] Define a morphological structural element that contains multiple pixels;
[0125] The morphological structuring elements are used to perform morphological closing operations on all the marked suspected boundary points.
[0126] The morphological closing operation is used to bridge the gaps of broken edges whose spatial distance is less than the preset distance threshold, and the region within the closed edge contour after connection is marked as the connected region.
[0127] A morphological structuring element containing multiple pixels is set up to represent suspected boundary points as foreground points in a binary image. The morphological structuring element is used to perform morphological closing operations on the binary image. The morphological closing operations are used to bridge the gaps of broken edges whose spatial distance is less than a preset distance threshold, so that the edges form closed edge contours. The regions within the connected closed edge contours are marked as connected regions.
[0128] The morphological structural elements are used to determine the expansion and connection range of the neighborhood of suspected boundary points in subsequent morphological closing operations. The morphological closing operations typically include a process logic of first dilation and then erosion, used to connect closely adjacent foreground structures and fill small gaps. This step does not change the foreign object determination conditions, but provides a more continuous closed edge contour for the construction of connected regions.
[0129] For example, a 3×3 pixel morphological structuring element containing 9 pixels is defined. This morphological structuring element is used to perform subsequent morphological closing operations on the binary image formed by suspected boundary points. The set of suspected boundary points is converted into a binary boundary image, where the positions of suspected boundary points are assigned a value of 1, and other positions are assigned a value of 0. After performing morphological closing operations using the 3×3 pixel morphological structuring element, the broken edges that were originally separated by 2 pixels are connected. After morphological closing operations, the discontinuous suspected boundary points located at a certain position inside the rice dumpling to be detected are connected into a closed edge contour. The system marks the region within this closed edge contour as a connected region and counts the pixels within this connected region so that the gray values of all pixels within the connected region can be extracted, and the average gray value and pixel area of the region can be calculated. Based on the determination of the average gray value and pixel area of the region, this scheme further determines whether the suspected foreign object region is a homogeneous high-density foreign object.
[0130] Furthermore, after calculating the average gray value of the region, the method further includes:
[0131] Calculate the grayscale variance of the grayscale values of all pixels within the connected region;
[0132] The grayscale variance is calculated based on the grayscale values of all pixels within the connected region and the average grayscale value of the region.
[0133] The gray-level variance is used to characterize the dispersion of gray-level values within a connected region. The smaller the gray-level variance, the more uniform the gray-level distribution of pixels within the connected region; the larger the gray-level variance, the more complex the texture changes that may exist within the connected region.
[0134] In some embodiments, the grayscale variance can be calculated using the following formula:
[0135]
[0136] in, Indicates the variance of gray levels; This represents the grayscale value of the i-th pixel within the connected region; This represents the average gray value of the area. Σ represents the total number of pixels in the connected region; Σ represents the summation of all pixels in the connected region; i represents the index of the pixel in the connected region.
[0137] For example, there are 180 pixels in the aforementioned connected region, and the average gray value of the region is M=54; by substituting the gray values of these 180 pixels into the gray variance formula, the gray variance V=18.6 is calculated.
[0138] When the average grayscale value of the area is lower than the preset lower limit of food grayscale, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains a foreign object, including:
[0139] When the average gray value of the region is lower than the preset lower limit of food gray value, and the pixel area is within the preset foreign object area range, the connected region is determined to be a suspected foreign object region.
[0140] Determine whether the grayscale variance of the suspected foreign object region is less than a preset homogeneity variance threshold;
[0141] If the grayscale variance is less than the preset homogeneity variance threshold, then the suspected foreign object area is confirmed to be a homogeneous high-density foreign object, and the rice dumpling to be detected is determined to contain a foreign object.
[0142] When the average grayscale value of a region is lower than the preset lower limit for food grayscale, and the pixel area is within the preset foreign object area range, the connected region is identified as a suspected foreign object region. The grayscale variance is compared with a preset homogeneity variance threshold to determine whether the grayscale variance is less than the preset homogeneity variance threshold. When the grayscale variance is less than the preset homogeneity variance threshold, the suspected foreign object region is confirmed as a homogeneous high-density foreign object, and the rice dumpling to be tested is determined to contain a foreign object.
[0143] The suspected foreign object region is a connected region that simultaneously satisfies both grayscale and area conditions. This determination is a prerequisite for subsequent grayscale variance judgment. Only connected regions that are determined to be suspected foreign object regions will continue to undergo homogeneity judgment. The preset homogeneity variance threshold is used to limit the judgment criteria for grayscale homogeneity within the suspected foreign object region. If the grayscale variance is less than the preset homogeneity variance threshold, it indicates that the grayscale distribution within the suspected foreign object region is relatively concentrated, which meets the image characteristics of homogeneous high-density foreign objects. The term "homogeneous high-density foreign object" is used to represent a foreign object region that satisfies the requirement of a low regional average grayscale value and homogeneity in its internal grayscale distribution.
[0144] For example, the average grayscale value of the region is 54, and the preset lower limit of grayscale for food ingredients is 70; the pixel area is 180, and the preset range for foreign object area is 80 to 600 pixels. Since both conditions are met, the system determines the connected region as a suspected foreign object region; the preset homogeneity variance threshold is set to 25, and the grayscale variance of the suspected foreign object region is 18.6, which is less than the preset homogeneity variance threshold of 25. Therefore, the system outputs the judgment result that the grayscale variance is less than the preset homogeneity variance threshold, thus confirming that the suspected foreign object region is a homogeneous high-density foreign object, and outputs the detection conclusion that the zongzi to be detected contains a foreign object.
[0145] Furthermore, after extracting the grayscale values of all pixels within the connected region, the method further includes:
[0146] Obtain the position coordinates of the connected region within the effective detection region;
[0147] Based on the pixel distance from the position coordinates to the edge of the effective detection area, retrieve the corresponding thickness compensation coefficient;
[0148] The average gray value of the region is weighted and corrected using the thickness compensation coefficient to obtain the corrected average gray value of the region.
[0149] When the average gray value of the corrected area is lower than the preset lower limit of gray value of the food ingredient, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains a foreign object.
[0150] The position coordinates of the connected region within the effective detection area are determined, and the output result is the position coordinates. The position coordinates can be calculated based on the coordinates of all pixels within the connected region and are used to characterize the position of the connected region within the effective detection area. The pixel distance from the position coordinates to the edge of the effective detection area is calculated, and the corresponding thickness compensation coefficient is retrieved based on the pixel distance. The average gray value of the region is weighted and corrected using the thickness compensation coefficient. It is determined whether the average gray value of the region after correction is lower than the preset lower limit of food gray value, and whether the pixel area is within the preset foreign object area range. If both conditions are met, it is confirmed that the zongzi to be detected contains foreign objects.
[0151] The pixel distance is used to represent the spatial relationship between the location coordinates of the connected region and the edge of the effective detection region; the thickness compensation coefficient is used to perform position-related correction on the average gray value of the region. Since different positions of the rice dumpling to be detected may correspond to different thicknesses, the pixel distance from the location coordinates to the edge of the effective detection region can be used as the basis for retrieving the thickness compensation coefficient.
[0152] In some embodiments, the position coordinates can be represented by the geometric center of the coordinates of all pixels within a connected region:
[0153]
[0154] in, Indicates position coordinates; This represents the coordinates of the i-th pixel within the connected region; Σ represents the number of all pixels in the connected region; Σ represents the sum of the coordinates of all pixels in the connected region; i represents the index of the pixel in the connected region.
[0155] For example, averaging the coordinates of the 180 pixels within the aforementioned connected region yields the position coordinates C = (480, 520). These position coordinates indicate the corresponding location of the connected region within the effective detection region and serve as input for subsequent calculations of the pixel distance to the edge of the effective detection region.
[0156] The edge of the effective detection area is determined by the foreground mask. The pixel distance from the position coordinates (480, 520) to the nearest edge of the effective detection area is 95 pixels. Based on the preset correspondence between pixel distance and thickness compensation coefficient, the thickness compensation coefficient is set to 0.92. The thickness compensation coefficient is obtained through standard sample calibration; when the thickness causes the grayscale value to be low, the thickness compensation coefficient can be greater than 1; when the thickness causes the grayscale value to be high, the thickness compensation coefficient can be less than 1.
[0157] In some embodiments, the corrected average gray value of the region can be calculated using the following formula:
[0158]
[0159] in, This represents the average gray value of the corrected area. Indicates the thickness compensation coefficient; This represents the average gray value of the area.
[0160] For example, if the average gray value of the region is M=54 and the thickness compensation coefficient is K=0.92, then the corrected average gray value of the region is M c =0.92×54=49.68, and the average gray value of the corrected area is used as the gray value input for subsequent foreign object detection.
[0161] For example, the corrected average grayscale value of the region is 49.68, which is lower than the preset lower limit of grayscale for food ingredients of 70; the pixel area is 180, which is within the preset foreign object area range of 80 to 600 pixels; therefore, the system confirms that the zongzi to be detected contains foreign objects; this result forms a continuous implementation link with the aforementioned connected region construction, average grayscale value calculation and pixel area calculation.
[0162] A foreign object detection system for the inside of a zongzi (sticky rice dumpling), characterized in that it includes:
[0163] The perspective view generation module is used to control the X-ray generator to emit X-rays towards the rice dumpling to be inspected, and to obtain the penetration energy through the receiving device to generate an X-ray perspective view of the rice dumpling.
[0164] The grayscale extraction module is used to extract the grayscale value of each pixel in the X-ray perspective image of the zongzi and obtain the grayscale value of adjacent pixels in a preset neighborhood.
[0165] The gradient calculation module is used to calculate the gradient difference of gray values between the pixel and its neighboring pixels, so as to obtain the gray value change gradient of each pixel.
[0166] The boundary point marking module is used to mark the pixels whose grayscale change gradient is greater than a preset gradient threshold as suspected boundary points.
[0167] The region construction module is used to connect the suspected boundary points whose spatial distance is less than a preset distance threshold to construct a closed connected region.
[0168] The feature calculation module is used to extract the gray values of all pixels in the connected region, calculate the average gray value of the region, and calculate the pixel area of the connected region.
[0169] The foreign object detection module is used to confirm that the rice dumpling to be detected contains foreign objects when the average gray value of the area is lower than the preset lower limit of food gray value and the pixel area is within the preset foreign object area range.
[0170] It should be noted that all directional indications in the embodiments of the present invention, such as up, down, left, right, front, back, etc., are only used to explain the relative positional relationship and movement of the components in a specific posture, as shown in the attached figure. If the specific posture changes, the directional indication will also change accordingly.
[0171] Furthermore, the use of terms such as "first" and "second" in this invention is for descriptive purposes only and should not be construed as indicating or implying their relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined with "first" or "second" may explicitly or implicitly include at least one of those features. Meanwhile, the word "and / or" throughout the text means including three solutions; for example, "A and / or B" includes solution A, solution B, or a solution that simultaneously satisfies A and B. Additionally, the technical solutions of the various embodiments can be combined with each other, but this must be based on the ability of those skilled in the art to implement them. When the combination of technical solutions is contradictory or impossible to implement, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection claimed by this invention.
[0172] All of the above components are general standard parts or components known to those skilled in the art. Their structure and principles can be learned by those skilled in the art through technical manuals or conventional experimental methods.
[0173] For example, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to complete the present invention. The one or more modules / units may be a series of computer program instruction segments capable of performing a specific function, which describe the execution process of the computer program in the electronic device.
[0174] The electronic device may be a desktop computer, laptop, handheld computer, or smart tablet, etc. The electronic device may include, but is not limited to, a processor and memory. Those skilled in the art will understand that the above components are merely examples of electronic devices and do not constitute a limitation on the electronic device. It may include more or fewer components than described above, or combine certain components, or different components. For example, the electronic device may also include input / output devices, network access devices, buses, etc.
[0175] The processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor. The processor is the control center of the electronic device, connecting all parts of the electronic device via various interfaces and lines.
[0176] The memory can be used to store the computer programs and / or modules. The processor implements various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory and by calling data stored in the memory. The memory may mainly include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, phonebook, etc.). In addition, the memory may include high-speed random access memory, and may also include non-volatile memory, such as hard disk, memory, plug-in hard disk, smart media card (SMC), secure digital (SD) card, flash card, at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0177] Wherein, if the modules / units integrated in the electronic device are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments of the present invention can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium can be appropriately added or removed according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electrical carrier signals and telecommunication signals.
[0178] It should be noted that the device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Furthermore, in the accompanying drawings of the device embodiments provided by this invention, the connection relationships between modules indicate that they have communication connections, which can be specifically implemented as one or more communication buses or signal lines. Those skilled in the art can understand and implement this without any creative effort.
[0179] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above descriptions are merely specific embodiments of the present invention and are not intended to limit the scope of protection of the present invention. In particular, it should be noted that any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention for those skilled in the art.
Claims
1. A method for detecting foreign objects inside a zongzi (sticky rice dumpling), characterized in that, include: The X-ray generator is controlled to emit X-rays towards the rice dumpling to be inspected, and the penetrating energy is obtained through the receiving device to generate an X-ray perspective view of the rice dumpling; Extract the grayscale value of each pixel in the X-ray perspective image of the zongzi, and obtain the grayscale value of adjacent pixels in a preset neighborhood; Calculate the grayscale gradient difference between the pixel and its neighboring pixels to obtain the grayscale change gradient of each pixel. Pixels whose grayscale change gradient is greater than a preset gradient threshold are marked as suspected boundary points; Connect the suspected boundary points whose spatial distance is less than a preset distance threshold to construct a closed connected region; Extract the grayscale values of all pixels within the connected region, calculate the average grayscale value of the region, and calculate the pixel area of the connected region; When the average gray value of the area is lower than the preset lower limit of food gray value, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains foreign objects.
2. The method for detecting foreign objects inside a zongzi (sticky rice dumpling) according to claim 1, characterized in that, Before extracting the grayscale values of each pixel in the X-ray perspective image of the zongzi, the method further includes: The X-ray perspective view of the zongzi is smoothed and denoised using a median filtering algorithm with a preset window size to eliminate impulse noise, resulting in a denoised X-ray perspective view of the zongzi.
3. The method for detecting foreign objects inside a zongzi (sticky rice dumpling) according to claim 2, characterized in that, The method further includes: The foreground mask of the denoised X-ray perspective view of the zongzi was obtained by threshold segmentation using the Otsu method; The background of the zongzi X-ray perspective image is removed using the foreground mask to extract the effective detection area that mainly contains the zongzi to be detected; The step of extracting the grayscale value of each pixel in the X-ray perspective image of the zongzi specifically involves: extracting the grayscale value of each pixel within the effective detection area.
4. The method for detecting foreign objects inside a zongzi (sticky rice dumpling) according to claim 1, characterized in that, Calculating the grayscale gradient difference between the pixel and its neighboring pixels to obtain the grayscale change gradient of each pixel includes: The Sobel edge detection operator is used to perform convolution operations on the gray values of adjacent pixels within the preset neighborhood. The first gray-level gradient component in the horizontal direction and the second gray-level gradient component in the vertical direction of the pixel are extracted respectively; The first gray-level gradient component and the second gray-level gradient component are combined by amplitude synthesis to calculate the gray-level change gradient of each pixel.
5. The method for detecting foreign objects inside a zongzi (sticky rice dumpling) according to claim 1, characterized in that, Connecting the suspected boundary points whose spatial distance is less than the preset distance threshold to construct the closed connected region includes: Define a morphological structural element that contains multiple pixels; The morphological structuring elements are used to perform morphological closing operations on all the marked suspected boundary points. The morphological closing operation is used to bridge the gaps of broken edges whose spatial distance is less than the preset distance threshold, and the region within the closed edge contour after connection is marked as the connected region.
6. The method for detecting foreign objects inside a zongzi (sticky rice dumpling) according to claim 1, characterized in that, After calculating the average gray value of the region, the method further includes: Calculate the grayscale variance of the grayscale values of all pixels within the connected region; When the average grayscale value of the area is lower than the preset lower limit of food grayscale, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains a foreign object, including: When the average gray value of the region is lower than the preset lower limit of food gray value, and the pixel area is within the preset foreign object area range, the connected region is determined to be a suspected foreign object region. Determine whether the grayscale variance of the suspected foreign object region is less than a preset homogeneity variance threshold; If the grayscale variance is less than the preset homogeneity variance threshold, then the suspected foreign object area is confirmed to be a homogeneous high-density foreign object, and the rice dumpling to be detected is determined to contain a foreign object.
7. The method for detecting foreign objects inside a zongzi (sticky rice dumpling) according to claim 3, characterized in that, After extracting the grayscale values of all pixels within the connected region, the method further includes: Obtain the position coordinates of the connected region within the effective detection region; Based on the pixel distance from the position coordinates to the edge of the effective detection area, retrieve the corresponding thickness compensation coefficient; The average gray value of the region is weighted and corrected using the thickness compensation coefficient to obtain the corrected average gray value of the region. When the average gray value of the corrected area is lower than the preset lower limit of gray value of the food ingredient, and the pixel area is within the preset foreign object area range, it is confirmed that the rice dumpling to be detected contains a foreign object.
8. A foreign object detection system for the inside of a zongzi (sticky rice dumpling), characterized in that, include: The perspective view generation module is used to control the X-ray generator to emit X-rays towards the rice dumpling to be inspected, and to obtain the penetration energy through the receiving device to generate an X-ray perspective view of the rice dumpling. The grayscale extraction module is used to extract the grayscale values of each pixel in the X-ray perspective image of the zongzi and obtain the grayscale values of adjacent pixels in a preset neighborhood. The gradient calculation module is used to calculate the gradient difference of gray values between the pixel and its neighboring pixels, so as to obtain the gray value change gradient of each pixel. The boundary point marking module is used to mark the pixels whose grayscale change gradient is greater than a preset gradient threshold as suspected boundary points. The region construction module is used to connect the suspected boundary points whose spatial distance is less than a preset distance threshold to construct a closed connected region. The feature calculation module is used to extract the gray values of all pixels in the connected region, calculate the average gray value of the region, and calculate the pixel area of the connected region. The foreign object detection module is used to confirm that the rice dumpling to be detected contains foreign objects when the average gray value of the area is lower than the preset lower limit of food gray value and the pixel area is within the preset foreign object area range.