OpenCV to android Opencv (JAVA)



我一直在尝试将watershed.cpp转换为Java,但有一个代码我无法理解。谁能把它转换成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代码

我的代码

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

谢谢

我希望这能帮助你,因为我正在做类似的事情。

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

相关内容

  • 没有找到相关文章

最新更新