Image mean shift blur: pyrMeanShiftFiltering()

https://blog.csdn.net/kingkee/article/details/94437333

1. Programming environment:

OpenCV  4.1.0
HERE Visual Studio 2017 Enterprise (15.9.13)
operating system Windows 10 x64 Chinese Professional Edition (1903)

2. The image mean shift is blurred:

Mean shift blur is a kind of image edge preservation filtering algorithm, which is often used to remove noise before watershed segmentation of the image, which can greatly improve the effect of watershed segmentation. Its basic principle is:

For a given number of samples, choose one of the samples, delineate a circular area with the sample as the center point, find the centroid of the sample in the circular area, that is, the point with the highest density, and then use this point as The center continues to perform the above iterative process until it finally converges.

This feature of the mean shift algorithm can be used to achieve color image segmentation. The corresponding function in Opencv is pyrMeanShiftFiltering.

Strictly speaking, this function is not an image segmentation, but a smooth filtering of the image at the color level. It can neutralize colors with similar color distribution, smooth color details, and erode smaller color areas.
 

3. Procedure description:

  • The pyrMeanShiftFiltering() function in OpenCV:
 
  1. void pyrMeanShiftFiltering( InputArray src, OutputArray dst,

  2. double sp, double sr, int maxLevel = 1,

  3. TermCriteria termcrit=TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS,5,1) );

The first parameter src: input image, 8-bit, three-channel color image. It is not required to be in RGB format. Color image formats in Opencv such as HSV and YUV can be used.

The second parameter dst: the output image, which has the same size and data format as the input src.

The third parameter sp, defines the radius of the drifting physical space.

The fourth parameter sr: the defined drift color space radius.

The fifth parameter maxLevel: defines the maximum number of levels of the pyramid.

The sixth parameter termcrit: the defined drift iteration termination condition, which can be set as the number of iterations meets the termination, the deviation between the iteration target and the center point meets the termination, or a combination of the two.

  • The execution process of the pyrMeanShiftFiltering function is like this:

1. Iterative space construction:

Take any point P0 on src on the input image as the center of the circle, create a spherical space with radius sp in physical space and radius sr in color space, with 2 coordinates in physical space—x, y, and 3 coordinates in color space—R, G , B (or HSV), forming a 5-dimensional space sphere.

The range x and y of the physical space are the length and width of the image, and the range R, G, and B of the color space are 0~255 respectively.

2. Find the vector of the iteration space and move the sphere of the iteration space and recalculate the vector until convergence:

In the spherical space constructed in 1, after obtaining the sum of the color vectors of all points relative to the center point, move the center point of the iterative space to the end of the vector, and calculate the vector sum of all points in the spherical space again, Iterate in this way until the end of the vector sum obtained in the last space sphere is the center point Pn of the space sphere, and the iteration ends.

3. Update the color value of the initial origin P0 corresponding to the output image dst to the color value of the end point Pn of the current iteration, thus completing the color average shift of one point.

4. For other points on the input image src, perform steps 1, 2, 2, and 3 in sequence. After all the points are traversed, the entire mean shift color filter is completed.

In this process, the key parameters are the settings of sp and sr. The larger the value of the two settings, the more obvious the smoothing effect on the image color, and the more time the function consumes.

Four, sample program:

 
  1. #include <opencv2/opencv.hpp>

  2. #include <iostream>

  3.  
  4. using namespace cv;

  5. using namespace std;

  6.  
  7. int main(int argc, char* argv[]) {

  8. Mat src = imread("../images/test.jpg");

  9. if (src.empty()) {

  10. printf("不能打开图像!\n");

  11. return -1;

  12. }

  13.  
  14. namedWindow("1-原图", WINDOW_AUTOSIZE);

  15. imshow("1-原图", src);

  16.  
  17. Mat dst;

  18.  
  19. pyrMeanShiftFiltering(src, dst, 15, 50, 1, TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 5, 1));

  20. imshow("2-pyrMeanShiftFiltering", dst);

  21.  
  22. waitKey(0);

  23. return 0;

  24. }

5. Operation effect:

Guess you like

Origin blog.csdn.net/c2a2o2/article/details/111170166