How to Iterate in OpenCV4Android

☆樱花仙子☆ 提交于 2019-12-30 07:43:28

问题


I had this piece of code to be converted into Java using OpenCV4Android. But their are no Java-Wrappers for matiterator in OpenCV4Android.

void show_result(const cv::Mat& labels, const cv::Mat& centers, int height, int width)
{
        std::cout << "===\n";
        std::cout << "labels: " << labels.rows << " " << labels.cols << std::endl;
        std::cout << "centers: " << centers.rows << " " << centers.cols << std::endl;
        assert(labels.type() == CV_32SC1);
        assert(centers.type() == CV_32FC1);

        cv::Mat rgb_image(height, width, CV_8UC3);
        cv::MatIterator_<cv::Vec3b> rgb_first = rgb_image.begin<cv::Vec3b>();
        cv::MatIterator_<cv::Vec3b> rgb_last = rgb_image.end<cv::Vec3b>();
        cv::MatConstIterator_<int> label_first = labels.begin<int>();

        cv::Mat centers_u8;
        centers.convertTo(centers_u8, CV_8UC1, 255.0);
        cv::Mat centers_u8c3 = centers_u8.reshape(3);

        while ( rgb_first != rgb_last ) {
                const cv::Vec3b& rgb = centers_u8c3.ptr<cv::Vec3b>(*label_first)[0];
                *rgb_first = rgb;
                ++rgb_first;
                ++label_first;
        }
        cv::imshow("tmp", rgb_image);
        cv::imwrite("Kmean/25.jpg", rgb_image);

        cv::waitKey();
}

I can't find MatIterator class in java. I saw a questiion similar to this posted in OpenCV support section, but no answer.

1) Is there any alternative way to convert this to java (if possible in efficient iterations). 2) I don't want to use native code.

Thanks in advance.


回答1:


i guess there are no default iterators in OpenCV4Android. So solved it with normal iteration.

Same result as C++ code but instead of taking height and width i took main image in to function and got height and width from it anything would be jst fine.

public static Mat show_result(Mat labels, Mat centers, Mat imageMat){

        centers.convertTo(centers, CvType.CV_8UC1, 255.0);
        centers.reshape(3);

        List<Mat> clusters = new ArrayList<Mat>();
        for(int i = 0; i < centers.rows(); i++) {
            clusters.add(Mat.zeros(imageMat.size(), imageMat.type()));
        }

        SparseIntArray counts = new SparseIntArray();
        for(int i = 0; i < centers.rows(); i++) counts.put(i, 0);

        int rows = 0;
        for(int y = 0; y < imageMat.rows(); y++) {
            for(int x = 0; x < imageMat.cols(); x++) {
                int label = (int)labels.get(rows, 0)[0];
                int r = (int)centers.get(label, 2)[0];
                int g = (int)centers.get(label, 1)[0];
                int b = (int)centers.get(label, 0)[0];
                counts.put(label, counts.get(label) + 1);
                clusters.get(label).put(y, x, b, g, r);
                rows++;
            }
        }
        System.out.println(counts);
        return combineClusters(clusters);
    }

    public  static Mat combineClusters(List<Mat> clusters){

        Mat result = new Mat(clusters.get(0).size(), CvType.CV_8UC3);
        result.setTo(new Scalar(0));

        for(int i=0; i < clusters.size(); i++){
            Core.bitwise_xor(result, clusters.get(i), result);
        }

        return result;
    }


来源:https://stackoverflow.com/questions/29204124/how-to-iterate-in-opencv4android

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