List of usage examples for org.opencv.imgproc Imgproc medianBlur
public static void medianBlur(Mat src, Mat dst, int ksize)
From source file:Questao2.java
void mediana() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); String url = escolherUrl();//w w w . ja v a 2 s. c om /** * Transforma imagem em matriz para facilitar manipulacao */ Mat img = Imgcodecs.imread(url); /** * Cria matriz de destino */ Mat dst = new Mat(); /** * Aplica filtro da mediana * medianBlur(imagem original, imagem destino, ksize) */ System.out.print("K-size: "); int ksize = in.nextInt(); Imgproc.medianBlur(img, dst, ksize); /** * Salva resultado em mediana.jpg */ Imgcodecs.imwrite("mediana.jpg", dst); showResult("mediana.jpg"); }
From source file:OctoEye.java
License:Open Source License
private void detectPupil() { // min and max pupil radius int r_min = 2; int r_max = 45; // min and max pupil diameter int d_min = 2 * r_min; int d_max = 2 * r_max; // min and max pupil area double area;//w w w. ja v a 2 s .c o m double a_min = Math.PI * r_min * r_min; double a_max = Math.PI * r_max * r_max; // histogram stuff List<Mat> images; MatOfInt channels; Mat mask; Mat hist; MatOfInt mHistSize; MatOfFloat mRanges; // contour and circle stuff Rect rect = null; Rect rectMin; Rect rectMax; List<MatOfPoint> contours; MatOfPoint3 circles; // pupil center Point p; // ellipse test points Point v; Point r; Point s; // rect points Point tl; Point br; // pupil edge detection Vector<Point> pointsTest; Vector<Point> pointsEllipse; Vector<Point> pointsRemoved; // temporary variables double distance; double rad; double length; int x; int y; int tmp; byte buff[]; // ------------------------------------------------------------------------------------------------------------- // step 1 // blur the image to reduce noise Imgproc.medianBlur(src, tmp1, 25); // ------------------------------------------------------------------------------------------------------------- // step 2 // locate the pupil with feature detection and compute a histogram for each, // the best feature will be used as rough pupil location (rectMin) int score = 0; int winner = 0; // feature detection MatOfKeyPoint matOfKeyPoints = new MatOfKeyPoint(); FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.MSER); // Maximal Stable Extremal Regions blobDetector.detect(tmp1, matOfKeyPoints); List<KeyPoint> keyPoints = matOfKeyPoints.toList(); // histogram calculation for (int i = 0; i < keyPoints.size(); i++) { x = (int) keyPoints.get(i).pt.x; y = (int) keyPoints.get(i).pt.y; tl = new Point(x - 5 >= 0 ? x - 5 : 0, y - 5 >= 0 ? y - 5 : 0); br = new Point(x + 5 < WIDTH ? x + 5 : WIDTH - 1, y + 5 < HEIGHT ? y + 5 : HEIGHT - 1); images = new ArrayList<Mat>(); images.add(tmp1.submat(new Rect(tl, br))); channels = new MatOfInt(0); mask = new Mat(); hist = new Mat(); mHistSize = new MatOfInt(256); mRanges = new MatOfFloat(0f, 256f); Imgproc.calcHist(images, channels, mask, hist, mHistSize, mRanges); tmp = 0; for (int j = 0; j < 256 / 3; j++) { tmp += (256 / 3 - j) * (int) hist.get(j, 0)[0]; } if (tmp >= score) { score = tmp; winner = i; rect = new Rect(tl, br); } if (debug) { // show features (orange) Core.circle(dbg, new Point(x, y), 3, ORANGE); } } if (rect == null) { return; } rectMin = rect.clone(); if (debug) { // show rectMin (red) Core.rectangle(dbg, rectMin.tl(), rect.br(), RED, 1); } // ------------------------------------------------------------------------------------------------------------- // step 3 // compute a rectMax (blue) which is larger than the pupil int margin = 32; rect.x = rect.x - margin; rect.y = rect.y - margin; rect.width = rect.width + 2 * margin; rect.height = rect.height + 2 * margin; rectMax = rect.clone(); if (debug) { // show features (orange) Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE); } // ------------------------------------------------------------------------------------------------------------- // step 4 // blur the image again Imgproc.medianBlur(src, tmp1, 7); Imgproc.medianBlur(tmp1, tmp1, 3); Imgproc.medianBlur(tmp1, tmp1, 3); Imgproc.medianBlur(tmp1, tmp1, 3); // ------------------------------------------------------------------------------------------------------------- // step 5 // detect edges Imgproc.Canny(tmp1, tmp2, 40, 50); // ------------------------------------------------------------------------------------------------------------- // step 6 // from pupil center to maxRect borders, find all edge points, compute a first ellipse p = new Point(rectMin.x + rectMin.width / 2, rectMin.y + rectMin.height / 2); pointsTest = new Vector<Point>(); pointsEllipse = new Vector<Point>(); pointsRemoved = new Vector<Point>(); buff = new byte[tmp2.rows() * tmp2.cols()]; tmp2.get(0, 0, buff); length = Math.min(p.x - rectMax.x - 3, p.y - rectMax.y - 3); length = Math.sqrt(2 * Math.pow(length, 2)); Point z = new Point(p.x, p.y - length); for (int i = 0; i < 360; i += 15) { rad = Math.toRadians(i); x = (int) (p.x + Math.cos(rad) * (z.x - p.x) - Math.sin(rad) * (z.y - p.y)); y = (int) (p.y + Math.sin(rad) * (z.x - p.x) - Math.cos(rad) * (z.y - p.y)); pointsTest.add(new Point(x, y)); } if (debug) { for (int i = 0; i < pointsTest.size(); i++) { Core.line(dbg, p, pointsTest.get(i), GRAY, 1); Core.rectangle(dbg, rectMin.tl(), rectMin.br(), GREEN, 1); Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE, 1); } Core.rectangle(dbg, rectMin.tl(), rectMin.br(), BLACK, -1); Core.rectangle(dbg, rectMin.tl(), rectMin.br(), RED, 1); Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE); } // p: Ursprung ("Mittelpunkt" der Ellipse) // v: Zielpunkt (Testpunkt) // r: Richtungsvektor PV for (int i = 0; i < pointsTest.size(); i++) { v = new Point(pointsTest.get(i).x, pointsTest.get(i).y); r = new Point(v.x - p.x, v.y - p.y); length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2)); boolean found = false; for (int j = 0; j < Math.round(length); j++) { s = new Point(Math.rint(p.x + (double) j / length * r.x), Math.rint(p.y + (double) j / length * r.y)); s.x = Math.max(1, Math.min(s.x, WIDTH - 2)); s.y = Math.max(1, Math.min(s.y, HEIGHT - 2)); tl = new Point(s.x - 1, s.y - 1); br = new Point(s.x + 1, s.y + 1); buff = new byte[3 * 3]; rect = new Rect(tl, br); try { (tmp2.submat(rect)).get(0, 0, buff); for (int k = 0; k < 3 * 3; k++) { if (Math.abs(buff[k]) == 1) { pointsEllipse.add(s); found = true; break; } } } catch (Exception e) { break; } if (found) { break; } } } double e_min = Double.POSITIVE_INFINITY; double e_max = 0; double e_med = 0; for (int i = 0; i < pointsEllipse.size(); i++) { v = pointsEllipse.get(i); length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2)); e_min = (length < e_min) ? length : e_min; e_max = (length > e_max) ? length : e_max; e_med = e_med + length; } e_med = e_med / pointsEllipse.size(); if (pointsEllipse.size() >= 5) { Point[] points1 = new Point[pointsEllipse.size()]; for (int i = 0; i < pointsEllipse.size(); i++) { points1[i] = pointsEllipse.get(i); } MatOfPoint2f points2 = new MatOfPoint2f(); points2.fromArray(points1); pupil = Imgproc.fitEllipse(points2); } if (pupil.center.x == 0 && pupil.center.y == 0) { // something went wrong, return null reset(); return; } if (debug) { Core.ellipse(dbg, pupil, PURPLE, 2); } // ------------------------------------------------------------------------------------------------------------- // step 7 // remove some outlier points and compute the ellipse again try { for (int i = 1; i <= 4; i++) { distance = 0; int remove = 0; for (int j = pointsEllipse.size() - 1; j >= 0; j--) { v = pointsEllipse.get(j); length = Math.sqrt(Math.pow(v.x - pupil.center.x, 2) + Math.pow(v.y - pupil.center.y, 2)); if (length > distance) { distance = length; remove = j; } } v = pointsEllipse.get(remove); pointsEllipse.removeElementAt(remove); pointsRemoved.add(v); } } catch (Exception e) { // something went wrong, return null reset(); return; } if (pointsEllipse.size() >= 5) { Point[] points1 = new Point[pointsEllipse.size()]; for (int i = 0; i < pointsEllipse.size(); i++) { points1[i] = pointsEllipse.get(i); } MatOfPoint2f points2 = new MatOfPoint2f(); points2.fromArray(points1); pupil = Imgproc.fitEllipse(points2); Point[] vertices = new Point[4]; pupil.points(vertices); double d1 = Math .sqrt(Math.pow(vertices[1].x - vertices[0].x, 2) + Math.pow(vertices[1].y - vertices[0].y, 2)); double d2 = Math .sqrt(Math.pow(vertices[2].x - vertices[1].x, 2) + Math.pow(vertices[2].y - vertices[1].y, 2)); if (d1 >= d2) { pupilMajorAxis = (int) (d1 / 2); pupilMinorAxis = (int) (d2 / 2); axisA = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2, vertices[1].y + (vertices[2].y - vertices[1].y) / 2); axisB = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2, vertices[0].y + (vertices[1].y - vertices[0].y) / 2); } else { pupilMajorAxis = (int) (d2 / 2); pupilMinorAxis = (int) (d1 / 2); axisB = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2, vertices[1].y + (vertices[2].y - vertices[1].y) / 2); axisA = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2, vertices[0].y + (vertices[1].y - vertices[0].y) / 2); } } double ratio = (double) pupilMinorAxis / (double) pupilMajorAxis; if (ratio < 0.75 || 2 * pupilMinorAxis <= d_min || 2 * pupilMajorAxis >= d_max) { // something went wrong, return null reset(); return; } // pupil found if (debug) { Core.ellipse(dbg, pupil, GREEN, 2); Core.line(dbg, pupil.center, axisA, RED, 2); Core.line(dbg, pupil.center, axisB, BLUE, 2); Core.circle(dbg, pupil.center, 1, GREEN, 0); x = 5; y = 5; Core.rectangle(dbg, new Point(x, y), new Point(x + 80 + 4, y + 10), BLACK, -1); Core.rectangle(dbg, new Point(x + 2, y + 2), new Point(x + 2 + pupilMajorAxis, y + 4), RED, -1); Core.rectangle(dbg, new Point(x + 2, y + 6), new Point(x + 2 + pupilMinorAxis, y + 8), BLUE, -1); for (int i = pointsEllipse.size() - 1; i >= 0; i--) { Core.circle(dbg, pointsEllipse.get(i), 2, ORANGE, -1); } for (int i = pointsRemoved.size() - 1; i >= 0; i--) { Core.circle(dbg, pointsRemoved.get(i), 2, PURPLE, -1); } } Core.ellipse(dst, pupil, GREEN, 2); Core.circle(dst, pupil.center, 1, GREEN, 0); }
From source file:OCV_MedianBlur.java
License:Open Source License
@Override public void run(ImageProcessor ip) { // srcdst/*from w w w . ja v a2 s .com*/ int imw = ip.getWidth(); int imh = ip.getHeight(); byte[] srcdst_bytes = (byte[]) ip.getPixels(); // mat Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1); Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1); // run src_mat.put(0, 0, srcdst_bytes); Imgproc.medianBlur(src_mat, dst_mat, ksize); dst_mat.get(0, 0, srcdst_bytes); }
From source file:angryhexclient.OurVision.java
License:Open Source License
/** * Detects the ground in the image.// ww w . j a v a2 s. c o m * @return A list of blocks representing the ground. */ public List<Block> detectGround() { Mat binaryImage = new Mat(new Size(_nWidth, _nHeight), CvType.CV_8U, new Scalar(1)); // We only detect right of this margin. The slingshot has some ground // colors and would partly be detected as ground. This is not what we // want. Trajectories originate at the slingshot, and if there is ground // detected at the slingshot, the agent will think, that none of its // trajectories are valid. Therefore we start with detecting due right // of the slingshot. int startAtX = findSlingshot().x + findSlingshot().width * 2; // Now we create a binary image of the ground areas. White where there // is ground, black otherwise. for (int y = 0; y < _nHeight; y++) { for (int x = 0; x < _nWidth; x++) { if (x > startAtX && isGround(x, y)) binaryImage.put(y, x, 255); else binaryImage.put(y, x, 0); } } Mat smoothedImage = new Mat(new Size(_nWidth, _nHeight), CvType.CV_8U, new Scalar(1)); // This median filter improves the detection tremendously. There are a // whole lot of single pixels that carry ground colors spread all over // the image. We remove them here. Imgproc.medianBlur(binaryImage, smoothedImage, 7); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); // We use OpenCV to find the contours. Contours are lines, that // represent the boundaries of the objects in the binary image. Imgproc.findContours(smoothedImage, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); ArrayList<Block> result = new ArrayList<Block>(); //Now for every contour, we convert it to blocks for communicating them to DLV. for (MatOfPoint mp : contours) { org.opencv.core.Point[] pts = mp.toArray(); for (int i = 0; i < pts.length - 1; i++) { Block b = new Block((int) pts[i].x, (int) pts[i].y); b.add((int) pts[i + 1].x, (int) pts[i + 1].y); result.add(b); } //One block for the first vertex to the last vertex. Block b = new Block((int) pts[pts.length - 1].x, (int) pts[pts.length - 1].y); b.add((int) pts[0].x, (int) pts[0].y); result.add(b); } return result; }
From source file:br.com.prj.TelaPrincipal.java
public void adicionarLabel(final Image img, final Rect faceEncontrada) { JLabel lbImagem = new JLabel() { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, getWidth() - 5, getHeight() - 5, null); }/* w w w.java 2s .c om*/ }; final Integer ultimoPixelColuna = faceEncontrada.width + faceEncontrada.x; final Integer ultimoPixelLinha = faceEncontrada.height + faceEncontrada.y; final Mat mat = convertImageToMat(img); Imgproc.medianBlur(mat, mat, 17); lbImagem.addMouseListener(new MouseAdapter() { private Boolean blur = false; @Override public void mouseClicked(MouseEvent e) { int i = -1, j = 0; for (int linha = faceEncontrada.y; linha <= ultimoPixelLinha; linha++) { j = 0; i++; for (int coluna = faceEncontrada.x; coluna < ultimoPixelColuna; coluna++) { double[] cor = mat.get(i, j++); if (cor != null) { imagemCarregada.put(linha, coluna, new double[] { cor[0], cor[1], cor[2] }); } } } if (!blur) { Imgcodecs.imwrite("img/imagem_processada.jpg", imagemCarregada); } else { System.out.println("remover blur"); } blur = !blur; imgCarregada.setIcon(resizeImage("img/imagem_processada.jpg", imgCarregada.getWidth(), imgCarregada.getHeight())); } }); // Posiciona a imagem lbImagem.setBounds(boundX, boundY, 70, 70); lbImagem.setVisible(rootPaneCheckingEnabled); // Prepara a posio x do prximo label boundX += 70; // Verifica se estourou o limite if (boundX >= jPanel1.getWidth() - 70) { // Desce uma fila boundY += 70; boundX = 10; } jPanel1.add(lbImagem); jPanel1.repaint(); }
From source file:Clases.Segmentador.java
public Mat blurearImg(Mat canalAzul, int val) { Imgproc.medianBlur(canalAzul, canalAzul, val); return canalAzul; }
From source file:ctPrincipal.Filtros.java
private String mediana(int ksize) { Mat img = Imgcodecs.imread(imgS);/* w ww . j av a 2 s . c o m*/ Imgproc.medianBlur(img, output, ksize); Imgcodecs.imwrite("OutputImg/mediana.jpg", output); return "OutputImg/mediana.jpg"; }
From source file:fi.conf.tabare.ARDataProvider.java
private static void doBlur(Mat matIn, Mat matOut, Blur b, float p) { int s = ((int) (p * 7)) * 2 + 1; Size ksz = new Size(s, s); switch (b) {//w w w . j a v a2 s.c o m case normal: Imgproc.blur(matIn, matOut, ksz); break; case gaussian: Imgproc.GaussianBlur(matIn, matOut, ksz, 1 + (p * 14) / 3f); break; case median: Imgproc.medianBlur(matIn, matOut, s); break; default: break; } }
From source file:interactivespaces.activity.image.vision.opencv.outline.ImageOpenCvVisionOutlineActivity.java
License:Apache License
/** * Detect all the edges in the image and make only the edges visible. * * @param image//from www .j ava 2s. c om * the image to be processed * @param dst * the destination image */ private void edgeify(Mat image, Mat dst) { Mat gray = new Mat(); Imgproc.cvtColor(image, gray, Imgproc.COLOR_RGB2GRAY); Imgproc.medianBlur(gray, gray, BLUR_FILTER_SIZE); Mat edges = new Mat(); Imgproc.Laplacian(gray, edges, CvType.CV_8U, LAPLACIAN_KERNEL_SIZE, 1, 0); Imgproc.threshold(edges, dst, EDGES_THRESHOLD, MAXIMUM_THRESHOLD_VALUE, Imgproc.THRESH_BINARY_INV); }
From source file:LetsStart.GUI.java
private void processOperation() { if (noneString.equals(filterMode)) { output = image.clone();/* w ww. j ava 2 s . c o m*/ } else { output = new Mat(image.rows(), image.cols(), image.type()); Size size = new Size(10.0, 10.0); if (blurString.equals(filterMode)) { Imgproc.blur(image, output, size); } else if (gaussianString.equals(filterMode)) { //Imgproc.GaussianBlur(image, output, size, 0); } else if (medianString.equals(filterMode)) { Imgproc.medianBlur(image, output, 3); } else if (bilateralString.equals(filterMode)) { Imgproc.bilateralFilter(image, output, 9, 100, 100); } } }