Image segmentation learning note _ 1 (the meanshift splitting example provided by opencv)

Source: Internet
Author: User

Meanshift can be used for image filtering, video tracking, and image segmentation.

Generally, the feature points of an image can be extracted at least five dimensions (X, Y, R, G, and B). As we all know, meanshift is often used to find modal points, that is, the point with the highest density. So we can also use it to find the modal points of the Five-dimensional space. Because different points will eventually converge to different peaks, these points form a category, this completes the purpose of image segmentation, which means clustering.

One thing to note is that the change range of the image pixel is different from that of the coordinate. Therefore, we need to use different window radius for modal detection of these data points using Windows. Therefore, in the meanshift split function pyrmeanshiftfiltering () that comes with opencv, there are two parameters for selecting the radius of the space search window and the radius of the color window search.

According to the function name pyrmeanshiftfiltering, The meanshift algorithm and the image pyramid are combined for segmentation. Therefore, the parameter list contains a variable that specifically defines the number of required pyramid layers.

This experiment comes from a sample in opencv2.3.1. The main process is to set the parameters first, and then use the pyrmeanshiftfiltering () function to split the input image. The split result is stored in the second parameter of the function, that is, the output image. Finally, based on the features of the split image, floodfill () the function fills the split result with different colors. Of course, the use of this function has not been thoroughly clarified.

The experiment results are as follows:

Image before split:

    

Split image:

  

Different colors represent different split categories.

 

  Summary of this experiment:

The use of the createtrackbar () function, that is, the slide function. A parameter of this function is the parameter of the slide response function (INT, void *), which is fixed and cannot be changed. However, you can leave it empty when calling the function. You can leave it empty when implementing the function. For more information, see the code comments.

The reason why createtrackbar () can respond is that the main function uses the waitkey () function, which can not only wait infinitely, but also interact with users. For details, see the code comment.

Memory Errors may occur when the program runs frequently. For example, if you drag a slide bar, the memory will report errors. I don't know why. The sample provided by opencv is generally excellent in code. However, opencv has many bugs and is still being improved. It may also be caused by slightly modifying the code, but I think this is not the main reason.

The experiment code is as follows:

1 // meanshift_segmentation.cpp: defines the entry point of the console application. 2 // 3 4 # include "stdafx. H "5 # include <opencv2/CORE/core. HPP> 6 # include <opencv2/highgui. HPP> 7 # include <opencv2/imgproc. HPP> 8 # include <iostream> 9 10 using namespace CV; 11 using namespace STD; 12 13 14 mat SRC, DST; 15 int spatialrad = 10, colorrad = 10, maxprylevel = 1; 16 // const scalar & colordiff = scalar: All (1); 17 18 void meanshift_seg (INT, void *) 19 {20 // call the meanshift image pyramid to split 21 pyrme Anshiftfiltering (SRC, DST, spatialrad, colorrad, maxprylevel); 22 RNG = therng (); 23 mat mask (DST. rows + 2, DST. cols + 2, cv_8uc1, scalar: All (0); 24 for (INT I = 0; I <DST. rows; I ++) // opencv image and other matrices are also based on the 0 index of 25 for (Int J = 0; j <DST. cols; j ++) 26 if (mask. at <uchar> (I + 1, J + 1) = 0) 27 {28 scalar newcolor (RNG (256), RNG (256), RNG (256 )); 29 floodfill (DST, mask, point (I, j), newcolor, 0, scalar: All (1), scalar: All (1 )); 30 // floodfill (DST, MAS K, point (I, j), newcolor, 0, colordiff, colordiff); 31} 32 imshow ("DST", DST ); 33} 34 35 36 int main (INT argc, uchar * argv []) 37 {38 39 namedwindow ("src", window_autosize); 40 namedwindow ("DST", window_autosize ); 41 42 src = imread ("stuff.jpg"); 43 cv_assert (! SRC. empty (); 44 45 spatialrad = 10; 46 colorrad = 10; 47 maxprylevel = 1; 48 49 // although the onchange function of the createtrackbar function requires the two parameters to be in the form of onchange (INT, void *) 50 // but here is the system response function, when using the createtrackbar function, the called function does not need to write parameters, or even 51 // parentheses. However, the implementation process of the called function still needs to be met (INT, void *) two parameter types: 52 createtrackbar ("spatialrad", "DST", & spatialrad, 80, meanshift_seg); 53 createtrackbar ("colorrad", "DST", & colorrad, 60, meanshift_seg); 54 createtrackbar ("maxprylevel", "DST ", & Maxprylevel, 5, meanshift_seg); 55 56 // meanshift_seg (0, 0); 57 58 imshow (" src ", Src); 59/* char c = (char) waitkey (); 60 if (27 = C) 61 Return 0; */62 imshow ("DST", Src); 63 waitkey (); // infinite waiting for user interaction response 64 // while (1); // The reason why while (1) cannot be used here is that you need to wait for user interaction, while (1) this function is not available. Both of them have an infinite waiting effect. 65 return 0; 66}

 

 

 

 

 

 

 

 

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.