MATLAB exercise program (meanshift image clustering)

Source: Internet
Author: User

Meanshift can be used as the target tracking, and image clustering. I only implement image clustering here. Of course, it is a program compiled based on my own understanding. Target Tracking must also be implemented in the future, because the reason why I first looked at this algorithm is to use it to track the target.

I will not introduce the basic principles of meanshift. Compared with my introduction, many ox people on the Internet are better than I have explained. Finally, I will list my references. Here I will explain how I understand meanshift image clustering. Clustering here also requires a template matrix like filtering in the past. However, this template does not have a preset matrix, but extracts an R * r matrix around the currently processed pixels, then, convert the matrix into a one-dimensional vector, perform meanshift on the vector, and assign the value of the final iteration to the processed pixel. So it can be understood that the image is iterated to the pixel clustering with the same value through meanshift as a class.

I am using gray images here. As for color images, I have seen a blog post that converts the RGB domain to the LUV domain for further processing. This is not clear to me, however, I think some of his code is similar to mean filtering. Although I didn't use the same method as him, his code can also be referenced. The portal is here.

Below is the code (this is my own understanding and cannot be guaranteed to be correct, but at least some ideas can be provided for your encoding ):

Main. m

Clear all; close all; clc; r = 2; %imimimimg=imread('lena.jpg '); imshow (IMG); IMG = double (IMG); [m n] = size (IMG ); imgn = zeros (m + 2 * r + 1, n + 2 * r + 1); imgn (R + 1: m + R, R + 1: N + r) = IMG; imgn (1: R, R + 1: N + R) = IMG (1: R, 1: N); imgn (1: m + R, N + R + 1: n + 2 * r + 1) = imgn (1: m + R, N: N + r); imgn (m + R + 1: m + 2 * r + 1, R + 1: n + 2 * r + 1) = imgn (M: m + R, R + 1: n + 2 * r + 1); imgn (1: m + 2 * r + 1, 1: R) = imgn (1: m + 2 * r + 1, R + * r); imshow (mat2gray (imgn) for I = 1 + R: m + R for j = 1 + R: N + r ser = imgn (I-r: I + R, J-R: J + r); SER = reshape (SER, [1 (2 * r + 1) ^ 2]); % change the two-dimensional template to one-dimensional imgn (I, j) = mean_shift (SER, 2 * r ^ 2 + 2 * r + 1 ); % Take the value at the center of the template as the iteration initial value end endfigure; imgn = imgn (R + 1: m + R, R + 1: N + r ); imshow (mat2gray (imgn ));

Meanshift. m

Function Re = mean_shift (SER, p) [m n] = size (SER); TMP = double (SER); pre_w = TMP (p); point = P; while 1 SER = tmp-pre_w; for I = 1: M * n if I ~ = Point Ser (I) = Ser (I)/(I-point); % I-point is the distance, that is, H end SER = Ser in various formulas. ^ 2; k = (1/SQRT (2 * PI) * exp (-0.5 * Ser); % the legendary kernel function W = sum (TMP. * (k)/sum (k); If ABS (w-pre_w) <0.01 break; end pre_w = W; end % tmp1 = ABS (TMP-W ); % [I point] = min (tmp1); Re = W; % if Max (TMP)-W <0.01% point = 0; % end % point = W; End

Processing result:

Source image

Effect of processing with a radius of 2

---------- The following is 2013.5.30 add ------------

The meanshift image clustering in the previous part still needs to be modified. The simplest meanshift algorithm is implemented below, which is based entirely on the principle.

The final references are good summaries, but this time I am referring to image processing, analysis and Machine Vision (3rd edition.

Below are the typical iterations:

The procedure is as follows:

Clear all; close all; clc; % test data mu = [0 0]; % mean S = [30 0; 0 35]; % covariance DATA = mvnrnd (MU, S, 300); % generate 300 Gaussian distribution data (:, 1), data (:, 2), 'O'); H = 3; % kernel size x = [data ()]; % Take the first data as the initial iteration value pre_x = [0 0]; hold onwhile norm (pre_x-x)> 0.01; pre_x = x; plot (X (1), X (2), 'r + '); u = 0; % molecular accumulate item D = 0; % denominator accumulate for I = % the two most critical steps. The mean Displacement Formula is k = norm (X-data (I, :))/h ). ^ 2; G = (1/SQRT (2 * PI) * exp (-0.5 * k); U = data (I, :) * g + U; D = G + D; end M = u/d; % Coordinate Position after iteration x = m; End

 

Refer:

1. http://en.wikipedia.org/wiki/Mean-shift wiki encyclopedia, the introduction is clear.

2. http://www.cnblogs.com/liqizhou/archive/2012/05/12/2497220.html very detailed understanding.

3. http://emuch.net/bbs/viewthread.php? Tid = 4626864 the understanding of a student on the bug.

4. http://en.wikipedia.org/wiki/Kernel_ (Statistics) introduces the kernel function.

5. http://wenku.baidu.com/view/11b6a7de6f1aff00bed51eac.html proposed the meanshift algorithm paper, although I did not look at it, but want to thoroughly understand the algorithm or read this good.

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.