How to apply RBF kernel function in k means cluster? Here is the code

조회 수: 5 (최근 30일)
MINO GEORGE
MINO GEORGE 2021년 5월 13일
댓글: MINO GEORGE 2021년 5월 17일
This is the code
grayImage= imread('CYST RENAL -87.jpg');
g = rgb2gray(grayImage);
g = double(g);
sigma = 0.4;
[n,d] = size(g);
nms = sum(g'.^2);
Ks = exp(-(nms'*ones(1,n) -ones(n,1)*nms + 2*g*g')/(2*sigma^2));
[m n]=kmeans(Ks,3);
m=reshape(m,size(Ks,1),size(Ks,2));
B=labeloverlay(Ks,m);
figure;
imshow(B);
I am unable to solve this error. Plshelp me to solve this error and explain how to apply kernel functions in k means clusetering
Error using reshape Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.

답변 (1개)

Pratyush Roy
Pratyush Roy 2021년 5월 17일
Hi,
For data in matrix form, the kmeans algorithm assumes that individual rows are the data instances. So when we apply kmeans on an N*N matrix, it returns a N*1 index array instead of an N^2*1 index array.
As a workaround, we can consider
  1. Converting the Ks matrix into a column vector Ks_vec with the same number of elements as Ks.
  2. Applying kmeans on Ks_vec to obtain the labels for each element,
  3. Reshape it back to the shape of Ks.
grayImage= imread('CYST RENAL -87.jpg');
g = rgb2gray(grayImage);
g = double(g);
sigma = 0.4;
[n,d] = size(g);
nms = sum(g'.^2);
Ks = exp(-(nms'*ones(1,n) -ones(n,1)*nms + 2*g*g')/(2*sigma^2));
Ks_vec = reshape(Ks,[size(Ks,1)*size(Ks,2),1]);
[m n]=kmeans(Ks_vec,3);
m=reshape(m,size(Ks,1),size(Ks,2));
B=labeloverlay(Ks,m);
figure;
imshow(B);
Hope this helps!
  댓글 수: 1
MINO GEORGE
MINO GEORGE 2021년 5월 17일
Hi, thank you for responding. Here is the image after applying the code the code.Sometthing went wrong
This is my original image
Pls help me to segment the roi

댓글을 달려면 로그인하십시오.

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by