Optimization method of ant colony edge detection algorithm developed based on C language
Through the C language, the ant colony edge detection algorithm is developed, the reading method is adjusted and the blocked processing is adopted, which solves the problem of slow running speed of the ant colony algorithm, realizes efficient edge detection, and improves the production efficiency of mechanical parts detection.
Patent Information
- Application Number
- CN202410909924.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-08
- Publication Date
- 2024-12-24
AI Technical Summary
Classic edge detection algorithms such as Sobel and Canny cannot meet the high-precision tolerance requirements. Although the ant colony algorithm is better than them, it runs slowly, affecting the efficiency of large-scale assembly line production.
The C language is used to develop ant colony edge detection algorithm, adjust the reading method of images in memory, use non-repetitive random number generation methods, and adopt a blocking scheme for high-pixel images to improve the running speed.
The running speed of the ant colony edge detection algorithm is significantly improved, especially when processing large-scale images, the blocked solution increases the speed by about 5 times, and improves production efficiency.
Smart Images

Figure CN119180834A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of mechanical processing, and in particular to an optimization method for an ant colony edge detection algorithm developed based on C language. Background Art
[0002] With the increasingly high quality requirements for products in the field of mechanical processing, especially the increasingly stringent tolerance requirements for mechanical parts, classic edge detection algorithms such as Sobel and Canny are no longer sufficient to detect mechanical parts with high-precision tolerance requirements. The ant colony algorithm is superior to classic algorithms such as Sobel and Canny in detecting edges, but its slow running speed and long detection time limit its use in the production process of large-scale assembly lines.
[0003] Ant colony edge detection algorithm is a heuristic method in image processing. Derived from ant colony optimization, it uses pheromones left by artificial ants to mark edges. Usually, ants are randomly distributed in an image and moved through iterations. The movement of ants is based on the transition probability,
[0004]
[0005] in, represents the pheromone of the pixel point (i, j) in the nth construction step, η ij Represents the heuristic information at that position. Ω ij represents the area of pixel (i, j). α and β are parameters, usually with a value of ≈2.0. The mathematical symbol ∈ represents belonging. From the transfer rule (1), we know that pixels with high heuristic information and pheromone will be frequently visited by ants. After all ants are moved, pheromone Will update,
[0006]
[0007] Where p is the evaporation rate of the pheromone, and λ is the pheromone left behind by the ants that visited this place. The usual value of λ is,
[0008]
[0009] Where t is a user-defined value to avoid noise. Due to evaporation, pheromones will eventually disappear in places with low visit frequency, thereby highlighting the edges. In addition, each ant has a certain memory to avoid visiting places that have been visited in memory. In addition to this non-repetitive ant movement method, the initial positions of the ants can also be randomly arranged in areas with high heuristic information (η ij ≥t). The heuristic information is defined as
[0010]
[0011] Among them, I max is the maximum intensity value of the grayscale image I, I ij is the intensity value of the pixel (i, j) in image I. If the pixel is at the edge of the image, the values of u and v are adjusted to {-1, 0, 1}. For example, the heuristic information degenerates into the normalized central difference at the upper edge of the image (excluding the corners at both ends). In addition, the heuristic information at the corners of the image is set to 0. Similarly, the neighborhood Ω of the pixel (i, j) ij is defined as the first loop around (i, j),
[0012] Ω ij ={(r, s)|max{|u|, |v|}=1, u=ri, v=sj} (5)
[0013] This means that each ant has 8 degrees of freedom to move, but this drops to 5 at the image boundaries and to 3 at the corners. Figure 2 Medium ij Describes the neighborhood of pixel (i, j). Summary of the invention
[0014] In order to solve the problem that the edge detection time of the ant colony algorithm is long and affects the production efficiency, the present invention discloses an optimization method of the ant colony edge detection algorithm, which mainly has the following innovative points:
[0015] 1. The ant colony edge algorithm is written in C language, which is conducive to integrated embedded development.
[0016] 2. Adjust the way the image is read from the memory, and the matrix index (i, j) of the pixel point is reduced to the column index i*width+j, where width represents the width of the image.
[0017] 3. Use a programming method to generate non-repeating random numbers. When applied to the ant colony algorithm, it means that the ant colony quickly traverses the possible edge areas of the entire image.
[0018] 4. For high-pixel images, use a picture segmentation solution to utilize cache space. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Other features, objects and advantages of the present application will become more apparent by reading the detailed description of non-limiting embodiments made with reference to the following drawings:
[0020] Figure 1 It is a flow chart of the present invention, and the improvements are shown in brackets.
[0021] Figure 2 This is a schematic diagram of the image segmentation method and pixel area used in the present invention.
[0022] Figure 3 This is the test picture used in the present invention.
[0023] Figure 4 This is the running result of the non-blocking algorithm of the present invention.
[0024] Figure 5 This is the running result of the block algorithm of the present invention.
[0025] Figure 6 The present invention uses a comparison of the running speeds of images divided into blocks and those that are not divided into blocks. DETAILED DESCRIPTION
[0026] Based on the background technologies (1) to (5), the pseudo code of the ant colony edge detection algorithm can be seen as follows, where the Random function is a non-repeating random number generator.
[0027] Input: Grayscale image (size: height×width)
[0028] Output: Binary image of edges
[0029]
[0030] The parameter settings of the ant colony edge detection algorithm can be referred to the following table.
[0031]
[0032] The size of the image is height×width. It is the floor function, that is, an integer not greater than this value.
[0033] In particular, the non-repeating random number generator of the ant colony edge detection algorithm, namely the Random function, is written as follows:
[0034]
[0035]
[0036] To speed up the algorithm, the image is read as a one-dimensional array instead of a two-dimensional matrix, that is, the pixel point (i, j) is contracted to ij = i*width+j, which is conducive to storing the position of the ants and generating non-repeating random numbers. Specifically, the ant colony matrix ants klOnly one floating point number ij is stored instead of two floating point numbers (i, j) for the k+1th ant at the 1+1th step. When the ant needs to be moved, this floating point number ij can be decompressed into two floating point numbers (i, j), namely the integer division method i=ij / width and the integer remainder method j=ij%width. This will improve floating point operations while reducing memory reads and cache loads. In addition, random numbers are generated through a one-dimensional array ij=rand()%A. Compared with two independent random number generation methods i=rand()%height and j=rand()%width, there is a lower probability of repetition because the period A=height*width is greater than the periods height and width. The generation of edges is controlled by the threshold τ 0 We get pixels with high pheromone (τ ij >τ 0 ) are marked as edge (black) and vice versa as region (white).
[0037] To further speed up the algorithm, larger images can be divided into blocks so that smaller images can be read into the cache, such as Figure 2 In specific implementation, the ant colony edge detection algorithm can be put into a Block function, whose variables are the heuristic information matrix η, a corner point (x 0 ,y 0 ) and size (width, height). The block area is defined as [x 0 , x 0 +width]×[y 0 ,y 0 +height]. Heuristic information matrix η ij Initialize outside the Block function so that the heuristic information is complete at the boundary of the block image. And check that the block area A>0 and the block contains more than one color. Finally, the image size is Height×Width, and the block size is height×width. The block algorithm can follow the following structure,
[0038] Input: Grayscale image size Height×Width
[0039] Output: Binary image of edges
[0040]
[0041]
[0042] Running the image test on an Intel m3 processor (2.20GHz, 4M cache), we can get the results of non-blocked and blocked (b x ×b y=256×256) algorithm running time and image pixel curve. Figure 6 As shown, when the image size is small (less than 1000*1000 in this example), there is no advantage in running speed between blocking and non-blocking. However, when the image size is large (greater than 1000*1000 in this example), blocking has a significant running speed improvement (about 5 times in this example) compared with non-blocking.
Claims
1. An optimization method for an ant colony edge detection algorithm developed based on C language, characterized in that: The following steps are involved: Step 1. Use C language to write an ant colony algorithm for edge detection of images. Step 2: Propose a method to generate non-repeating pseudo-random numbers for ant colony algorithm. Step 3: For high-pixel images, the algorithm is accelerated by dividing the images into blocks.
2. The optimization method of the ant colony edge detection algorithm developed based on C language according to claim 1 is characterized in that: In step 1, the image is read as a one-dimensional array, and the matrix index (i, j) of the pixel point is reduced to the array index q = i*width + j, where width represents the width of the image. The array index can be obtained by taking the remainder to obtain the matrix index, which can be described in C language as i = q / width, j = q%width.
3. The optimization method of the ant colony edge detection algorithm developed based on C language according to claim 1, characterized in that: In step 2, the period of the pseudo-random number is the total number of pixels in the image, that is, the image area A=height*width, where height and width represent the pixels in the height and width directions of the image respectively. The generation of pseudo-random numbers is described in C language, that is, r=rand()%A. The pseudo-random numbers are stored in a one-dimensional array and duplication is avoided by comparing the existing pseudo-random numbers in the array.
4. The optimization method of the ant colony edge detection algorithm developed based on C language according to claim 1, characterized in that: In step 3, the block size (bx x by) of the image can be adjusted according to the processor used, for example, bx=256, by=256.
5. A computer program product, characterized in that: The computer program product comprises a computer program, which implements the steps of the method according to any one of claims 1 to 4 when executed by a processor.
