Using K-means clustering pixel in OpenCV using Java

吃可爱长大的小学妹 提交于 2019-12-22 19:06:10

问题


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).

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);

What should I do to display the image after using k-means?


回答1:


This Java class implements a fully functional example of the k-means color clustering algorithm in the official Java wrapper for OpenCV.

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.



来源:https://stackoverflow.com/questions/22295247/using-k-means-clustering-pixel-in-opencv-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!