How does the function imsegkmeans compute distance?

I know imsegkmeans can do color-based clustering but I wonder how it is implemented. Specifically, how does it compte the distance between two pixels in the image? What distance does it use? I tried stepping into the code to see details but found it used an internal function:
images.internal.ocvkmeans(X,k,NumAttempts,MaxIterations,Threshold)
And I couldn't step into the "ocvkmeans" function. Can anyone tell me what distance it uses for distance?
Thanks!

답변 (1개)

Sumukh
Sumukh 2024년 11월 8일

0 개 추천

Kindly refer to the following MATLAB Answer where a staff member has answered that the “imgsegkmeans” and “kmeans” functions use similar algorithms under the hood:
The functionality of “imgsegkemeans” is to cluster the images based on the colour of each pixel of that image. As it uses k-means clustering to achieve this, you can refer to the following documentation where the algorithm for k-means clustering has been explained along with the way the distances are computed and used:
I hope this answers your query.

댓글 수: 6

Tao Hu
Tao Hu 2024년 11월 10일
편집: Tao Hu 2024년 11월 10일
Thanks but I'm sorry it does not answer my question. The above link only says:
Compute distances from each observation to c1. Denote the distance between cj and the observation m as d(xm,cj).
So what is the distance? That's my question. How does it measure the color distance between two pixel is what I want to know.
DGM
DGM 2024년 11월 10일
I would suspect that it's using euclidean distance (or squared euclidean distance), but my guess probably isn't very helpful. I don't find anything in the documentation, and like you found out, algkmeans() seems to just call a builtin.
I agree with @DGM. The Euclidean distance is the general default in various functions, even where options for the distance metric are provided. If it uses the same algorithm as the Statistics and Machine Learning Toolbox kmeans function (as I suspect it does), the various distance metrics, including the default, are described in the kmeans documentation section on Distance.
DGM
DGM 2024년 11월 10일
That, and Euclidean distance in LAB is already a canonical color distance metric (1976 Delta E), so it kind of makes a little extra sense to choose it as a default. That's not to say that we're always using LAB, but I don't know what other simple distance metric would have similar contextual relevance.
The code is very likely doing
Temp = reshape(TheImage, size(TheImage,1)*size(TheImage,2), []);
[IDX, Centers] = kmeans(Temp);
DGM
DGM 2024년 11월 10일
편집: DGM 2024년 11월 10일
It reshapes the data, but it calls ocvkmeans()
[Label,NormCen] = images.internal.ocvkmeans(X,k,NumAttempts,MaxIterations,Threshold);
... which appears to be undocumented. IPT internals grabcut() and algkmeans() appear to be the only things that use it.

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

제품

릴리스

R2022b

질문:

2024년 11월 8일

편집:

2024년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by