使用Java在OpenCV中使用K-均值聚类像素 [英] Using K-means clustering pixel in OpenCV using Java
问题描述
我目前正在尝试开发一个Android应用.我试图将叶子的图像从RBG转换为HSV,以生成饱和度值空间中的图像(无色相).接下来,我尝试使用K均值生成图像,该图像应以蓝色作为背景,以绿色显示叶子(前景对象).
I am currently trying to develop an Android app. I have tried to convert an image of a leaf from RBG to HSV to produce an image which is in saturation-value space (without hue). Next, I tried to use K-means to produce a image where it should display blue as background and green for the leaf (foreground object).
但是,在OpenCV中使用K-means函数后,我不知道如何显示图像.
However, I do not know how to display the image after using the K-means function in OpenCV.
Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGBA2RGB,3);
Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGB2HSV,3);
List<Mat> hsv_planes = new ArrayList<Mat>(3);
Core.split(mHSV, hsv_planes);
Mat channel = hsv_planes.get(2);
channel = Mat.zeros(mHSV.rows(),mHSV.cols(),CvType.CV_8UC1);
hsv_planes.set(2,channel);
Core.merge(hsv_planes,mHSV);
Mat clusteredHSV = new Mat();
mHSV.convertTo(mHSV, CvType.CV_32FC3);
TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
Core.kmeans(mHSV, 2, clusteredHSV, criteria, 10, Core.KMEANS_PP_CENTERS);
使用k均值后应该怎么显示图像?
What should I do to display the image after using k-means?
推荐答案
此类Java 在用于OpenCV的官方Java包装器中实现了k均值颜色聚类算法的完整功能示例.
This Java class implements a fully functional example of the k-means color clustering algorithm in the official Java wrapper for OpenCV.
尽管上述实现是在RGB颜色空间中的图像上执行的,但它是一个很好的示例,可以大致了解Java OpenCV中的k均值,您可以轻松地对其进行扩展以使其在HSV空间中工作
Although the mentioned implementation is performed over an image in the RGB color space, it is a very good example for a general understanding of k-means in OpenCV on Java and you could easily extend it to make it work in the HSV space.
这篇关于使用Java在OpenCV中使用K-均值聚类像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!