android - OpenCV Android Kmeans 无法按预期工作

标签 android opencv image-processing k-means

此代码应提供 3 行的中心垫和 clusterCount 列数

    Mat reshaped_image = imageMat.reshape(1, imageMat.cols()*imageMat.rows());
    Mat reshaped_image32f = new Mat();
    reshaped_image.convertTo(reshaped_image32f, CvType.CV_32F, 1.0 / 255.0);

    Mat labels = new Mat();
    TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 100, 1);
    Mat centers = new Mat();
    int clusterCount = 5, attempts = 1;
    Core.kmeans(reshaped_image32f, clusterCount, labels, criteria, attempts, Core.KMEANS_PP_CENTERS, centers);

我在 C 中尝试了相同的代码,得到了 3 行的中心 Mat 和 clusterCount 列数。

但在 java 中,Core.kmeans 返回 4 列和簇行数。

centers.reshape(3);

所以现在 reshape 函数不适用于中心,因为行数取决于簇大小。在 C 中,行数总是恒定的,即 3。

所以在java中它给出了错误

the number of rows of the matrix cannot be divided by new number of rows

有人能找出问题所在吗?我什至试过this这与我的代码相似,但有同样的错误。

引用C代码:

    cv::Mat reshaped_image = image.reshape(1, image.cols * image.rows);
    cv::Mat reshaped_image32f;
    reshaped_image.convertTo(reshaped_image32f, CV_32FC1, 1.0 / 255.0);

    cv::Mat labels;
    int cluster_number = 5;
    cv::TermCriteria criteria(cv::TermCriteria::COUNT, 100, 1);
    cv::Mat centers;
    cv::kmeans(reshaped_image32f, cluster_number, labels, criteria, 1, cv::KMEANS_PP_CENTERS, centers);

最佳答案

终于成功了:-

我使用返回 RGBA 图像的 bitmapToMat 函数将位图转换为 Mat。所以中心有 4 列而不是 3 列。

如果您要将位图转换为 Mat,并且需要 BGR 图像,请执行此操作

    Mat imageMat = new Mat();
    Utils.bitmapToMat(bitmap, imageMat);

    Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_BGRA2BGR);

干杯

关于android - OpenCV Android Kmeans 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235599/

相关文章:

android - 在 Android 中动态单击按钮添加微调器

c++ - 如何在 Visual Studio 2010 中实现 Tesseract 与项目一起运行

python - 如何将 open3d 几何点云输出为 .pcd 文件?

opencv - 用旋转的 Gabor 核过滤的图像差异很大

android - 使用 setGraph() 以编程方式设置图形会导致应用程序无法在进程终止后恢复堆栈(Jetpack 导航)

android - 抽屉导航android中的过渡

android - 在 Android 市场上有一个单独的 Scala 库有意义吗?

java - 如何在 OpenCv 中从图像中识别人

python - networkx.exception.NodeNotFound 即使节点存在

python - 如何识别带有彩色背景图像的文本?