bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
ny = new Mat();
Utils.bitmapToMat(bitmap, ny);
Mat bgrImage = new Mat();
Imgproc.cvtColor(ny, bgrImage, Imgproc.COLOR_RGB2BGR);
bgrImage.convertTo(bgrImage, CvType.CV_32F, 1.0 / 255.0);
Mat reshapedImage = bgrImage.reshape(3,bgrImage.rows()*bgrImage.cols() );
Mat labels = new Mat();
Mat centers = new Mat();
ArrayList<Integer> labelsList = new ArrayList<>();
int K = 32; // Number of clusters (colors)
TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 400, 2.0);
Core.kmeans(reshapedImage, K, labels, criteria, 1, Core.KMEANS_PP_CENTERS,centers);
for (int i = 0; i < labels.rows(); i++) {
for (int j = 0; j < labels.cols(); j++) {
labelsList.add((int) labels.get(i, j)[0]);
}
}
int n = labels.cols() * labels.rows();
for (int i = 0; i < n; ++i) {
double[] color = centers.get(labelsList.get(i), 0);
labels.put(i, 0, color);
}
labels.convertTo(labels, CvType.CV_8U);
Core.normalize(labels, labels, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3);
Imgproc.cvtColor(labels,labels,Imgproc.COLOR_GRAY2RGB);
labels = labels.reshape(3,bgrImage.rows());
Mat blured = new Mat();
int kernelsz = 5;
Imgproc.medianBlur(labels, blured, kernelsz);
Bitmap resultBitmap = Bitmap.createBitmap(blured.cols(), blured.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(blured, resultBitmap);
mainview.setImageBitmap(resultBitmap);
![original![3bbe8c15-63ac-47c4-abf9-fd3185c2e7bb|494x500]