OpenCV的到Android opencv的(JAVA) [英] OpenCV to android Opencv (JAVA)

查看:616
本文介绍了OpenCV的到Android opencv的(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图watershed.cpp转换成Java,但有一个code,我无法理解。任何人都可以请其转换为Java

I have been trying to convert watershed.cpp into Java but there is a code I am unable to understand. Can anyone please convert it to Java

int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0], compCount++ )
    drawContours(markers, contours, idx, Scalar::all(compCount+1), -1, 8, hierarchy, INT_MAX);

if( compCount == 0 )
    continue;

vector<Vec3b> colorTab;
for( i = 0; i < compCount; i++ )
{
    int b = theRNG().uniform(0, 255);
    int g = theRNG().uniform(0, 255);
    int r = theRNG().uniform(0, 255);

    colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}

您可以找到这个链接全CPP code

下面是我的code

public Mat watershedCPP(Mat img0) {
    // img0 is the image got by imread
    img = getImageViewImage();

    img0.copyTo(img);
    Imgproc.cvtColor(img, markersMask, Imgproc.COLOR_RGB2GRAY);
    Imgproc.cvtColor(markersMask, imgGray, Imgproc.COLOR_GRAY2BGR);
    markersMask.setTo(Scalar.all(0));
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    MatOfInt4 hierarchy = new MatOfInt4();
    Imgproc.findContours(markersMask, contours, hierarchy,
            Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    Mat markers = new Mat(markersMask.size(), CvType.CV_32S);
    markers.setTo(Scalar.all(0));
    int idx = 0, compCount = 0;
    // for( ; idx >= 0; idx = (int) hierarchy.get(idx, 0)[0], compCount++ )
    for (int i = 0; i < hierarchy.rows(); i++)
        for (int j = 0; j < hierarchy.cols(); j++) {
            compCount++;
            Imgproc.drawContours(markers, contours, compCount,
                    new Scalar((Math.random()*8+1)), 2, 8, hierarchy,
                    (hierarchy.rows() * hierarchy.rows()),
                    new Point(-1, -1));
        }
    Imgproc.watershed(img0, markers);
    /*int size = (int) (markers.total() * markers.channels());
    List<colorContainer> colorTab = new ArrayList<WatershedActivity.colorContainer>();
    double[] temp = new double[size];
    for (int i = 0; i <= compCount; i++) {

        for (int j = 0; j < size; j++)
            temp[j] = (Math.random() * 255 + 0);
        colorContainer container = new colorContainer();
        container.setColor(temp);
        colorTab.add(container);
    }*/
    Mat wshed = new Mat(markers.size(), CvType.CV_8UC3);
    int gen=0;
  /*        for (int i = 0; i < markers.rows(); i++)
        for (int j = 0; j < markers.cols(); j++) {


            double[]index = markers.get(i, j);
            if (index[0] == -1) {
                for (int k = 0; k < size; k++)
                    temp[k] = (double)255;
                wshed.put(i, j, temp);
            } else if (index[0] <= 0) {
                for (int k = 0; k < temp.length; k++)
                    temp[k] = (double)0;
                wshed.put(i, j, temp);
            } else

            Log.d("Running", i+" : "+j);
            try {
                if(gen%colorTab.size()==0)
                    gen=0;
                //wshed.put(i, j, temp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
    Scalar alpha = new Scalar(0.6); // the factor
    Core.multiply(wshed, alpha, wshed);
    Scalar beta = new Scalar(0.6); // the factor
    Core.multiply(imgGray, beta, imgGray);
    Core.add(wshed, imgGray, wshed);
    return wshed;
}

感谢您
投下来之前,你请评论。

Thank you Please comment before you down vote.

推荐答案

我希望这可以帮助你为我做类似的事情。

I hope this might help you as I am doing something similar.

Mat gray8 = new Mat(marked.size(), CvType.CV_8UC1);
    Imgproc.cvtColor(marked, gray8, Imgproc.COLOR_RGB2GRAY);    
    Scalar mean = Core.mean(gray8);
    Imgproc.threshold(gray8, gray8, mean.val[0], 255,
            Imgproc.THRESH_BINARY);
    /*Imgproc.erode(gray8, gray8, new Mat(), new Point(-1, -1), 2);*/
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    MatOfInt4 hierarchy = new MatOfInt4();
    Imgproc.findContours(gray8, contours, hierarchy,
            Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    Toast.makeText(getApplicationContext(), contours.size()+" yo", Toast.LENGTH_SHORT).show();
    for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
        Imgproc.drawContours(orginal, contours, contourIdx, new Scalar(0, 0, 255), -1, 1, hierarchy, 50, new Point(1,1));
    }
    gray8.convertTo(gray8, CvType.CV_32S);
    Imgproc.watershed(orginal, gray8);
    gray8.convertTo(gray8, CvType.CV_8UC1);

这篇关于OpenCV的到Android opencv的(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆