OpenCV4Android Kmean不';I don’我没有按预期工作
此代码应提供包含3行和clusterCount列数的中心matOpenCV4Android Kmean不';I don’我没有按预期工作,opencv,image-processing,k-means,opencv4android,Opencv,Image Processing,K Means,Opencv4android,此代码应提供包含3行和clusterCount列数的中心mat 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 M
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行和clusterCount列数的centers Mat
但是在java中,Core.kmeans返回4列和集群行数
centers.reshape(3);
因此,现在重塑函数不适用于中心,因为行数取决于簇大小。在C中,行数始终不变,即3
所以在java中,它给出了错误
矩阵的行数不能除以新的行数
有人能找出问题所在吗。我甚至尝试了与我的代码相似的代码,但得到了相同的错误
参考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);
干杯