Ask Your Question

Revision history [back]

OpenCV to android (JAVA)

I have been trying to convert watershed.cpp from opencv samples. Which you can find on this Link There are few lines of code which I am unable to understand to code them in 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));
        }

And here is my code I guess it fine above and below these lines but if not kindly guide me through.

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

Thanks in advance