# How to use kmeans of opencvjs, you can give a sampl？Tks

**URL:** <https://forum.opencv.org/t/how-to-use-kmeans-of-opencvjs-you-can-give-a-sampl-tks/7165>\
**Category:** Python\
**Created:** [January 17, 2022, 9:35am UTC](https://forum.opencv.org/t/how-to-use-kmeans-of-opencvjs-you-can-give-a-sampl-tks/7165 "2022-01-17T09:35:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![N18](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/n18/32/4254_2.png) [@N18](https://forum.opencv.org/u/N18)\
**Post date:** [January 17, 2022, 9:35am UTC](https://forum.opencv.org/t/how-to-use-kmeans-of-opencvjs-you-can-give-a-sampl-tks/7165/1 "2022-01-17T09:35:33Z")

</div>

How to use kmeans of opencvjs, you can give a sampl？Tks  
My code can’t display the quantized results correctly

```auto
let canvas = document.getElementById('kmeans')
let mat=cv.imread(canvas);
cv.cvtColor(mat,mat,cv.COLOR_BGR2Lab, 0);
let sample= new cv.Mat(mat.rows * mat.cols, 4, cv.CV_32F)
let labels = new cv.Mat();
let centers = new cv.Mat();
let criteria= new cv.TermCriteria(cv.TermCriteria_EPS + cv.TermCriteria_MAX_ITER, 10,1.0)
cv.kmeans(sample,14, labels, criteria, 10, cv.KMEANS_RANDOM_CENTERS, centers );
console.log('sample',sample)
console.log('labels',labels)
console.log('centers',centers)
cv.imshow('kmeans',labels);

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 17, 2022, 9:45am UTC](https://forum.opencv.org/t/how-to-use-kmeans-of-opencvjs-you-can-give-a-sampl-tks/7165/2 "2022-01-17T09:45:25Z")

</div>

what are you trying to achieve, using kmeans ?  
also, if you have any code so far, please show !

---

<div class="post-metadata">

**Author:** ![N18](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/n18/32/4254_2.png) [@N18](https://forum.opencv.org/u/N18)\
**Post date:** [January 17, 2022, 9:48am UTC](https://forum.opencv.org/t/how-to-use-kmeans-of-opencvjs-you-can-give-a-sampl-tks/7165/3 "2022-01-17T09:48:43Z")

</div>

> [@berak](#):
>
> 你想达到什么目的，使用 kmeans ？  
> 另外，如果您到目前为止有任何代码，请显示！

I wanted to quantify the color of the picture, but so far it hasn’t worked correctly
