Slic Hyper Pixel Segmentation (ii): Key Code Analysis _ hyper-pixel

Source: Internet
Author: User

Slic Hyper Pixel Segmentation (II.)

The website http://ivrl.epfl.ch/research/superpixels gives the SLIC code. For the C + + code of several key functions to realize the details of the implementation, to facilitate beginners to understand. Specifically as follows:

1, set the desired number of pixels to split, open the picture. Converts a color RGB picture to lab space and X, y pixel coordinates in a total of 5-D spaces.

2, Detectlabedges. Ask for a bit of gradient =dx+dy in the picture.

Dx= (L (x-1)-L (x+1)) * (L (x-1)-L (x+1)) + (A (x-1)-A (x+1)) * (A (x-1)-A (x+1)) + (b (x-1)-B (x+1)) * (b (x-1)-II (x+1));

Dy= (L (y-1)-L (y+1)) * (L (y-1)-L (y+1)) + (A (y-1)-A (y+1)) * (A (y-1)-A (y+1)) + (b (y-1)-B (y+1)) * (b (y-1)-II (y+1));

3, Getlabxyseeds_forgivenk. Given the total number of pixels to be segmented k, the seed points are obtained based on the LABXY information.

1 The pixel-length step=sqrt (n/k) between the seed points. Initializes the seed point. Seed points are evenly distributed by step size and the seed points are evenly spaced after initialization (the red dots in Figure 1).

2) perturbseeds. Disturb the seed point. In the 3*3 neighborhood of each seed point, the lab color gradient (with step 2) of the pixel in the 8 neighborhood of the seed point is calculated, and the point of the lowest gradient value (the most "flat") is compared with the initial seed point gradient. and record its Labxy information as the new seed point (the green point in Figure 1 is the new seed point after the disturbance).

Figure 1: Disturbing the seed point diagram

4, hyper-pixel step step=sqrt (n/k) +2. Adding a small offset of 2 is to avoid the step is too small, resulting in hyper-pixel too dense situation.

5, Performsuperpixelsegmentation_variablesandm. For each hyper-pixel, the maximum color distance m is range [1,40], generally taking 10. The maximum space distance takes step.

1 Search Range 2step* 2step, that is, set offset=step. The offset=step*1.5 can be extended as a search scope when the step is shorter (step<10).

2 initialization of Distlab, Distxy, Distvec for Infinity. Maxlab initialized to 10*10,maxxy initialized to Step*step. The Distlab represents the lab color space distance between a point and the seed point, as follows: Distlab (i) = (L-KSEEDSL (n)) * (L-KSEEDSL (n)) + (A-KSEEDSA (n)) * (A-KSEEDSA (n)) + ( B-KSEEDSB (n)) * (B-KSEEDSB (n));d Istxy represents the space coordinate distance between a point and the seed point, calculated as follows: Distxy (i) = (X-KSEEDSX (n)) * (X-KSEEDSX (n)) + (Y-kseedsy (n)) * (Y-kseedsy (n));d ist represents the integrated distance between a point and the seed point (normalized color distance + spatial distance), calculated as follows: dist=distlab/(Maxlab) + distxy/(MAXXY); Here's a reminder: if C + + When the program becomes MATLAB code, special attention should be paid to the data type. The result of the UInt16 type variable minus the double type variable is the uint16 type, so if the latter value is greater than the former, the result is 0. Error prone here, you need to force type conversions.

3 Calculate the distance from the seed point of each point in the search area and save the distance from the seed point of each point in the search area to the Distvec Dist. Because a point may be in the search area of multiple seed points, the last save is the closest distance from the adjacent seed point, and the pixel is labeled as the same as the nearest seed point. The label of all pixels in the same hyper Pixel is the same

4 Calculate the labxy mean and coordinate center of gravity of all pixels in each new hyper pixel. Position the coordinate center of gravity as the new seed point for the hyper-pixel.

5 The above steps 2) to 4 repeat iterations 10 times.

6, Enforcelabelconnectivity. The function has several functions: to ensure that the same hyper-pixel is a single connected region, to remove the super pixel with too small size, and to avoid the single hyper pixel being cut.

1 First compute the pixel ideal area size: Supsz = sz/k = n/k;

2) There are two kinds of label vectors: the old label Vector obtained in the previous step is labels (i.e. the klabels obtained in step 5), but it has many problems, such as multiple connectivity, too little hyper pixel, which need to be optimized. The new label Vector Nlabels, the initialization value is all-1.

3 First select the starting point of each hyper-pixel (the first point in the upper-left corner), the starting point to determine the condition: A, from left to right, from top to bottom of the "Z" type order to find. b The point is not marked in the new label vector Nlabels (value is-1). Save its coordinates in XVEC[0],YVEC[0].

4) record the label value of the previous Adjlabel pixel. Judge condition: a the neighborhood field at the starting point in step 3. b) is marked in the new label Vector Nlabels (the label is greater than 0). The purpose of the recording Adjlabel is to replace the current hyper-pixel label with Adjlabel if the current hyper-pixel size is too small. Merge to the previous adjacent hyper-pixel, refer to step 6 below.

5) Extend the current hyper-pixel. First select the starting point as the center point of the current operation, and then determine whether the neighborhood domain belongs to the hyper-pixel member. Judgment condition: a The point is not marked in the new label vector Nlabels (the value is-1); b the point N and the current operation center point C are labeled in the old label vector, i.e. labels (n) = labels (c), which is understood to be a member of the same hyper-pixel. If the judge is a new member of the hyper-pixel, the new member is used as the new Action Center point, looping until the new member is found.

6 If the new hyper pixel size is less than half the size of the ideal pixel (you can define it as you want), the pixel label is replaced with the label value of the previous Adjlabel, and the label value is not incremented.

(7) Iterate over steps 3) to 6 until the entire picture is traversed.

7, draw the split result, exit the window.

Figure 2:slic The pixel segmentation result, and the blue point indicates the final hyper pixel seed point.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.