public static void main(String[] args) { // TODO Auto-generated method stub System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat src = Imgcodecs.imread("F:\\1_m1109048058.jpg"); Mat dst = src.clone(); Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGRA2GRAY); Imgproc.adaptiveThreshold(dst, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 3, 3); java.util.List<MatOfPoint> contours = new java.util.ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); System.out.println(contours.size()); for (int i = 0; i < contours.size(); i++) { Imgproc.drawContours(src, contours, i, new Scalar(0, 0, 0, 0), 1); } Imgcodecs.imwrite("F:\\test.jpg", src); } |