Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package simeav; import static java.lang.Math.abs; import java.util.ArrayList; import java.util.Random; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Moments; /** * * @author Nacha */ public final class Utils { private Utils() { } public static ArrayList<MatOfPoint> detectarContornos(Mat original) { Mat jerarquia = new Mat(); ArrayList<MatOfPoint> contornos = new ArrayList<>(); Imgproc.findContours(original.clone(), contornos, jerarquia, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); return contornos; } public static Scalar getColorRandom() { Random rand = new Random(); int r = rand.nextInt(256); int g = rand.nextInt(256); int b = rand.nextInt(256); return new Scalar(r, g, b); } public static Mat borrarMascara(Mat imagenOriginal, Mat mascara) { Mat imGrises = new Mat(); Imgproc.cvtColor(imagenOriginal, imGrises, Imgproc.COLOR_BGR2GRAY); Imgproc.threshold(imGrises, imGrises, 200, 250, Imgproc.THRESH_BINARY_INV); return imGrises.setTo(new Scalar(0, 0, 0), mascara); } public static boolean contiene(Rect rectangulo1, Rect rectangulo2) { return rectangulo1.contains(rectangulo2.br()) && rectangulo1.contains(rectangulo2.tl()); } public static boolean conecta(Rect modulo, Point c) { Point tl = modulo.tl(); Point br = modulo.br(); return ((abs(c.y - tl.y) < 10 || abs(c.y - br.y) < 10) && c.x >= tl.x && c.x <= br.x) || ((abs(c.x - tl.x) < 10 || abs(c.x - br.x) < 10) && c.y >= tl.y && c.y <= br.y); } public static Mat erode(Mat original) { Mat destino = new Mat(); Mat kernel = Imgproc.getStructuringElement(1, new Size(3, 3)); Imgproc.erode(original, destino, kernel); return destino; } public static Mat dilate(Mat original) { Mat destino = new Mat(); Mat kernel = Imgproc.getStructuringElement(1, new Size(3, 3)); Imgproc.dilate(original, destino, kernel); return destino; } public static ArrayList<Point> getCentros(ArrayList<MatOfPoint> contornos) { ArrayList<Moments> mu = new ArrayList<>(contornos.size()); for (int i = 0; i < contornos.size(); i++) { mu.add(i, Imgproc.moments(contornos.get(i), false)); } ArrayList<Point> mc = new ArrayList<>(contornos.size()); for (int i = 0; i < contornos.size(); i++) { mc.add(i, new Point(mu.get(i).get_m10() / mu.get(i).get_m00(), mu.get(i).get_m01() / mu.get(i).get_m00())); } return mc; } public static ArrayList<Point> detectarVertices(Mat original) { MatOfPoint corners = new MatOfPoint(); Imgproc.goodFeaturesToTrack(original, corners, 100, 0.01, 0, new Mat(), 2, false, 0.04); Mat vertices = Mat.zeros(original.size(), CvType.CV_8UC3); for (int i = 0; i < corners.height(); i++) { Core.circle(vertices, new Point(corners.get(i, 0)), 8, new Scalar(180, 170, 5), -1); } Mat imGrises = new Mat(); Mat bw = new Mat(); Imgproc.cvtColor(vertices, imGrises, Imgproc.COLOR_BGR2GRAY); Imgproc.Canny(imGrises, bw, 100, 150, 5, true); Mat jerarquia = new Mat(); ArrayList<MatOfPoint> contornos = new ArrayList<>(); Imgproc.findContours(bw.clone(), contornos, jerarquia, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); ArrayList<Point> mc = Utils.getCentros(contornos); Mat resultado = Mat.zeros(original.size(), CvType.CV_8UC3); for (int i = 0; i < contornos.size(); i++) { Scalar color = new Scalar(180, 170, 5); // Imgproc.drawContours(resultado, contornos, i, color, 2, 8, jerarquia, 0, new Point()); Core.circle(resultado, mc.get(i), 4, color, -1, 8, 0); } return mc; } }