OpenCV sharpen the edges (edges with no holes)

后端 未结 2 1194
失恋的感觉
失恋的感觉 2020-12-09 23:53

I am trying to detect the biggest/larger rectangular shape and draw bounding box to the detected area. In my use case, very often (and not always) the object that represent

2条回答
  •  感动是毒
    2020-12-10 00:33

    Thanks for yours comments and suggestion. The code provided by @NEJC works perfectly and cover 80% of my use case.

    Nevertheless, it does not works with similar case like this case not solved by the current code and i don't know why.

    Perhaps someone have an idea/clue/solution ?

    I continue to improve the code and try to find a more generic solution that can cover more case. I will post it if i ever i find.

    In any case, below is the working code based on @NEJC solution and notes.

    public static Mat process(Mat original){
        Mat src = original.clone();
        Mat hsvMat = new Mat();
        Mat saturation = new Mat();
        Mat sobx = new Mat();
        Mat soby = new Mat();
        Mat grad_abs_val_approx = new Mat();
    
        Imgproc.cvtColor(src, hsvMat, Imgproc.COLOR_BGR2HSV);
        List hsv_channels = new ArrayList(3);
        Core.split(hsvMat, hsv_channels);
        Mat hue = hsv_channels.get( 0 );
        Mat sat = hsv_channels.get( 1 );
        Mat val = hsv_channels.get( 2 );
    
        Imgproc.GaussianBlur(sat, saturation, new Size(9, 9), 2, 2);
        Mat imf = new Mat();
        saturation.convertTo(imf, CV_32FC1, 0.5f, 0.5f);
    
        Imgproc.Sobel(imf, sobx, -1, 1, 0);
        Imgproc.Sobel(imf, soby, -1, 0, 1);
    
        sobx = sobx.mul(sobx);
        soby = soby.mul(soby);
    
        Mat sumxy = new Mat();
        Core.add(sobx,soby, sumxy);
        Core.pow(sumxy, 0.5, grad_abs_val_approx);
    
        sobx.release();
        soby.release();
        sumxy.release();;
    
    
        Mat filtered = new Mat();
        Imgproc.GaussianBlur(grad_abs_val_approx, filtered, new Size(9, 9), 2, 2);
    
        final MatOfDouble mean = new MatOfDouble();
        final MatOfDouble stdev = new MatOfDouble();
        Core.meanStdDev(filtered, mean, stdev);
    
        Mat thresholded = new Mat();
        Imgproc.threshold(filtered, thresholded, mean.toArray()[0] + stdev.toArray()[0], 1.0, Imgproc.THRESH_TOZERO);
    
    
        /*
        Mat thresholded_bin = new Mat();
        Imgproc.threshold(filtered, thresholded_bin, mean.toArray()[0] + stdev.toArray()[0], 1.0, Imgproc.THRESH_BINARY_INV);
        Mat converted = new Mat();
        thresholded_bin.convertTo(converted, CV_8UC1);
        */
    
        Mat converted = new Mat();
        thresholded.convertTo(converted, CV_8UC1);
        return converted;
    }
    

提交回复
热议问题