Image segmentation is implemented using a pyramid. This function is,
Pyrsegmentation uses a pyramid to implement image segmentation void cvpyrsegmentation (iplimage * SRC, iplimage * DST, cvmemstorage * storage, cvseq ** comp, int level, double threshold1, double threshold2); SRC input image. DST output image. storagestorage: stores the sequential results of connected parts. The output sequence pointer components of the comp splitting parts. the maximum number of layers in which level creates a pyramid threshold1 the error threshold for establishing a connection threshold2 the error threshold function cvpyrsegmentation of the segmentation cluster implements image segmentation of the pyramid method. The pyramid is created to the maximum number of layers specified by level. If P (C (A), C (B) <threshold1, the connection between the pixel point A of layer I and the parent pixel B of its adjacent layers is established, after the connected parts are defined, they are added to some clusters. If P (C (A), C (B) <threshold2, then any two parts A and B belong to the same cluster. If the input image only has one channel, P (C branch, C Branch) = | C branch-C branch |. if the input image has a single channel (red, green, and blue), then P (C branch, C Branch) = · (C branch r-c branch R) + · (C branch G-C 2g) + · (C branch B-c 2 B ). each cluster can have multiple connected parts. The image SRC and DST should be an 8-bit, single-channel, or 3-channel image with the same size
Example:
# Include "stdafx. H "# include" highgui. H "# include" CV. H "// superdont // blog.csdn.net/superdontint main (INT argc, char ** argv) {iplimage * src = cvloadimage (" lena.png "); // common errors, if the image itself cannot be divisible by 2 times, an error will be reported and terminated. Be sure to note that iplimage * DST = cvcreateimage (cvgetsize (SRC), Src-> depth, Src-> nchannels ); cvmemstorage * stoage = cvcreatememstorage (0); cvseq * comp = NULL; int level = 4; // perform n-layer sampling double threshold1 = 150; double threshold2 = 30; // cvpyrsegmentation (SRC, DST, stoage, & Comp, level, threshold1, threshold2); cvnamedwindow ("src"); cvnamedwindow ("DST"); cvshowimage ("src ", SRC); cvshowimage ("DST", DST); cvwaitkey (0); cvdestroyallwindows (); Return 0 ;}
Implementation result:
References:
1. Learning opencv, Liu Rui, Yu Shiqi, Tsinghua University Press, pp.152-155
2. http://blog.csdn.net/cartoonface/article/details/5998543
3. http://www.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86#PyrSegmentation