Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV(3.4.3) Error: Assertion failed (src.type() == CV_8UC3 && dst.type() == CV_32SC1) in void cv::watershed(cv::InputArray, cv::InputOutputArray)

i try watershed alghoritm following this link then when i run theres a problem with watershed src. Can anyone explain the error and how to solve it

Utils.bitmapToMat(imageBmp, srcMat);
    byte[] srcData = new byte[(int) (srcMat.total() * srcMat.channels())];
    srcMat.get(0, 0, srcData);
    for (int i = 0; i < srcMat.rows(); i++) {
        for (int j = 0; j < srcMat.cols(); j++) {
            if (srcData[(i * srcMat.cols() + j) * 3] == (byte) 255 && srcData[(i * srcMat.cols() + j) * 3 + 1] == (byte) 255
                    && srcData[(i * srcMat.cols() + j) * 3 + 2] == (byte) 255) {
                srcData[(i * srcMat.cols() + j) * 3] = 0;
                srcData[(i * srcMat.cols() + j) * 3 + 1] = 0;
                srcData[(i * srcMat.cols() + j) * 3 + 2] = 0;
            }
        }
    }
    srcMat.put(0, 0, srcData);

    Mat kernel = new Mat(3, 3, CvType.CV_32F);

    float[] kernelData = new float[(int) (kernel.total() * kernel.channels())];
    kernelData[0] = 1; kernelData[1] = 1; kernelData[2] = 1;
    kernelData[3] = 1; kernelData[4] = -8; kernelData[5] = 1;
    kernelData[6] = 1; kernelData[7] = 1; kernelData[8] = 1;
    kernel.put(0, 0, kernelData);

    Mat imgLaplacian = new Mat();
    Imgproc.filter2D(srcMat, imgLaplacian, CvType.CV_32F, kernel);
    Mat sharp = new Mat();
    srcMat.convertTo(sharp, CvType.CV_32F);
    Mat imgResult = new Mat();
    Core.subtract(sharp, imgLaplacian, imgResult);

    imgResult.convertTo(imgResult, CvType.CV_8UC3);
    imgLaplacian.convertTo(imgLaplacian, CvType.CV_8UC3);

    Mat bw = new Mat();
    Imgproc.cvtColor(imgResult, bw, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(bw, bw, 40, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

    Mat dist = new Mat();
    Imgproc.distanceTransform(bw, dist, Imgproc.DIST_L2, 3);

    Core.normalize(dist, dist, 0.0, 1.0, Core.NORM_MINMAX);
    Mat distDisplayScaled = new Mat();
    Core.multiply(dist, new Scalar(255), distDisplayScaled);
    Mat distDisplay = new Mat();
    distDisplayScaled.convertTo(distDisplay, CvType.CV_8U);

    Imgproc.threshold(dist, dist, 0.4, 1.0, Imgproc.THRESH_BINARY);

    Mat kernel1 = Mat.ones(3, 3, CvType.CV_8U);
    Imgproc.dilate(dist, dist, kernel1);
    Mat distDisplay2 = new Mat();
    dist.convertTo(distDisplay2, CvType.CV_8U);
    Core.multiply(distDisplay2, new Scalar(255), distDisplay2);

    Mat dist_8u = new Mat();
    dist.convertTo(dist_8u, CvType.CV_8U);

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(dist_8u, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    Mat markers = Mat.zeros(dist.size(), CvType.CV_32S);

    for (int i = 0; i < contours.size(); i++) {
        Imgproc.drawContours(markers, contours, i, new Scalar(i + 1), -1);
    }

    Mat markersScaled = new Mat();
    markers.convertTo(markersScaled, CvType.CV_32F);
    Core.normalize(markersScaled, markersScaled, 0.0, 255.0, Core.NORM_MINMAX);
    Imgproc.circle(markersScaled, new Point(5, 5), 3, new Scalar(255, 255, 255), -1);
    Mat markersDisplay = new Mat();
    markersScaled.convertTo(markersDisplay, CvType.CV_8U);
    Imgproc.circle(markers, new Point(5, 5), 3, new Scalar(255, 255, 255), -1);

     Imgproc.watershed(imgResult, markers);

    Mat mark = Mat.zeros(markers.size(), CvType.CV_8U);
    markers.convertTo(mark, CvType.CV_8UC1);
    Core.bitwise_not(mark, mark);

    Bitmap resultBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mark, resultBmp);


    Glide.with(ResultActivity.this).asBitmap().load(resultBmp).into(ivAfter);