List of usage examples for org.opencv.imgproc Imgproc HoughCircles
public static void HoughCircles(Mat image, Mat circles, int method, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius)
From source file:KoImgProc.java
License:Open Source License
/** * Detects stones in the given image and returns an array list of KoCircle objects. * @param grey greyscale Mat of the board. * @param threshold threshold value for HoughCircles * @return an ArrayList of KoCircle objects. *///from w w w . j a v a 2s . c om private static ArrayList<KoCircle> detectStones(Mat grey, int minRadius, int maxRadius, int minDist, int threshold) { Mat stones = new Mat(); ArrayList<KoCircle> highThresholdStones = new ArrayList<KoCircle>(); Imgproc.HoughCircles(grey, stones, Imgproc.CV_HOUGH_GRADIENT, DP, minDist, threshold, threshold / 2, minRadius, maxRadius); for (int i = 0; i < stones.cols(); i++) { highThresholdStones.add(new KoCircle(stones.get(0, i))); } System.out.println(highThresholdStones.size() + " stones detected. minRadius: " + minRadius + "\tmaxRadius: " + maxRadius + "\tminDist: " + minDist + "\tthreshold: " + threshold); return highThresholdStones; }
From source file:balldetection.BallDetection.java
/** * @param args the command line arguments * @throws java.io.IOException//from w w w .j a v a2 s .c om */ public static void main(String[] args) throws IOException { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> CameraWindow cWindow = new CameraWindow(); cWindow.setVisible(true); int radius = 0; System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //intialization of matrices Mat circles = new Mat(); gray = new Mat(); hsv = new Mat(); filter = new Mat(); dst = new Mat(); camera = new VideoCapture(0); Mat frame = new Mat(); Webcam.ImagePanel panel = Webcam.createPanel(camera, "src"); Webcam.ImagePanel panel2 = Webcam.createPanel(camera, "filter"); Webcam.ImagePanel panel3 = Webcam.createPanel(camera, "dst"); while (true) { camera.read(frame); src = frame; GaussianBlur(src, src, new Size(3, 3), 2, 2); Imgproc.cvtColor(src, hsv, Imgproc.COLOR_BGR2HSV); Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); Core.inRange(gray, new Scalar(20, 100, 100), new Scalar(30, 255, 255), gray); Core.inRange(hsv, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()), new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), filter); Core.inRange(src, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()), new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), dst); double[] temp = hsv.get(hsv.rows() / 2, hsv.cols() / 2); System.out.println(temp[0] + ", " + temp[1] + ", " + temp[2] + ", " + radius); //System.out.println("Current Distance from ball: " + ((2.5366*radius) - 123.02)); Imgproc.HoughCircles(filter, circles, CV_HOUGH_GRADIENT, cWindow.get_dp(), filter.rows() / 2, cWindow.get_param1(), cWindow.get_param2(), cWindow.get_minCircleSize(), cWindow.get_maxCircleSize()); for (int i = 0; i < circles.cols(); i++) { Point center = new Point(Math.round(circles.get(0, i)[0]), Math.round(circles.get(0, i)[1])); radius = (int) Math.round(circles.get(0, i)[2]); // draw the circle center Core.circle(src, center, 3, new Scalar(0, 255, 0), -1, 8, 0); // draw the circle outline Core.circle(src, center, radius, new Scalar(0, 0, 255), 3, 8, 0); //System.out.println("" + circles.get(0,0)[0] + ", " + circles.get(0,0)[1] + ", " + circles.get(0,0)[2]); } panel.updateImage(toBufferedImage(src)); panel2.updateImage(toBufferedImage(filter)); panel3.updateImage(toBufferedImage(dst)); } }
From source file:Clases.Segmentador.java
public double[] TransfHough(Mat edges, int inv, int distMinCir, int umbMin, int umbMax, int radMin, int radMax) { Mat circles = new Mat(); double[] data = null; //Vector<Mat> circlesList = new Vector<Mat>(); Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, inv, distMinCir, umbMin, umbMax, radMin, radMax);/*from w w w.j av a 2s .c om*/ //Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 200, 1, 10, 20, 40 ); //Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 50) ; System.out.println("#rows " + circles.rows() + " #cols " + circles.cols()); double x = 0.0; double y = 0.0; int r = 0; for (int i = 0; i < circles.rows(); i++) { data = circles.get(i, 0); for (int j = 0; j < data.length; j++) { x = data[0]; y = data[1]; r = (int) data[2]; } Point center = new Point(x, y); // circle center // Core.circle(color, center, 1, new Scalar(0, 0, 0), -1); // circle outline // Core.circle(color, center, r, new Scalar(0, 255, 0), 1); //Ventana ventana8 = new Ventana(convertir((color)),0,2); //ventana8.setTitle("Houg"); /* Rect bbox = new Rect((int)Math.abs(x-r), (int)Math.abs(y-r), (int)2*r, (int)2*r); Mat croped_image = new Mat(color, bbox); Imgproc.resize(croped_image, croped_image, new Size(200,200)); circlesList.add(croped_image); Ventana ventana9 = new Ventana(convertir(croped_image),1,2); */ } return data; }
From source file:com.untref.bordes.HoughCirculos.java
public static BufferedImage implementarCiculos(BufferedImage screen, int acumulador, int radioMin, int radioMax) { Mat source = new Mat(screen.getHeight(), screen.getWidth(), CvType.CV_8UC3); byte[] data = ((DataBufferByte) screen.getRaster().getDataBuffer()).getData(); source.put(0, 0, data);/*from w w w . j av a 2s . co m*/ //ImageIO.write(screen, "jpg", "imagen"); //Mat source = Highgui.imread("test.jpg", Highgui.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(), source.cols(), source.type()); Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY); Imgproc.GaussianBlur(destination, destination, new Size(3, 3), 0, 0); Mat circles = new Mat(); Imgproc.HoughCircles(destination, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 30, 10, acumulador, radioMin, radioMax); int radius; org.opencv.core.Point pt; for (int x = 0; x < circles.cols(); x++) { double vCircle[] = circles.get(0, x); if (vCircle == null) { break; } pt = new org.opencv.core.Point(Math.round(vCircle[0]), Math.round(vCircle[1])); radius = (int) Math.round(vCircle[2]); // draw the found circle Core.circle(source, pt, radius, new Scalar(150, 0, 0), 2); Core.circle(source, pt, 1, new Scalar(0, 0, 0), 2); } BufferedImage res = matToBufferedImage(source); return res; }
From source file:contador_de_moedas.Circulo.java
private Mat doCirciloHough(Mat imgIn) { int valor = 0; baseImageList();//from w ww . j a v a 2s .c o m Reconhecimento _reco; Imgproc.cvtColor(imgIn, imgIn, Imgproc.COLOR_BGR2GRAY); /*Imgproc.erode(imgIn, imgIn, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2))); Imgproc.dilate(imgIn, imgIn, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));*/ double min_dist_Centro = imgIn.rows() / 8.0; /* Imgproc.HoughCircles(imageB, circles, method, dp, minDist, param1, param2, minRadius, maxRadius) imageB: Imagem de entrada (tons de cinza) circles: um vector que armazena conjuntos de 3 valores: Xc, Yc, r para cada crculo detectado. method: CV_HOUGH_GRADIENT definir o mtodo de deteco. Atualmente, esse o nico disponvel em OpenCV dp: (1) A razo inversa da resoluo min_dist: (imgIn.rows / 8 ) A distncia mnima entre centros detectados param1: (200 limite superior para o detector de bordas Canny interna) limite superior para o detector de bordas Canny interna param2: (100* Limiar para deteco de centro)Limiar para deteco de centro. minRadius: (0) raio minimo a ser detectado. Se for desconhecida, colocar zero como padrao. maxRadius: (maxRadius) raio maximo a ser detectado. Se desconhecida, colocar zero como padrao */ Imgproc.HoughCircles(imgIn, circles, Imgproc.CV_HOUGH_GRADIENT, 1, min_dist_Centro, param1, param2, minRadius, maxRadius); rowsCols[0] = circles.rows(); rowsCols[1] = circles.cols(); for (int i = 0; i < circles.cols(); i++) { _reco = new Reconhecimento(); double data[] = circles.get(0, i); Point pt = new Point(Math.round(data[0]), Math.round(data[1])); int raio = (int) Math.round(data[2]); if (data[2] > 20.0 && data[2] < 28.0) { valor = 10; setSomatorio(getSomatorio() + valor); } else if (data[2] > 27.0 && data[2] < 32.0) { valor = 5; setSomatorio(getSomatorio() + valor); } else if (data[2] > 32.0 && data[2] < 33.0) { valor = 50; setSomatorio(getSomatorio() + valor); } else if (data[2] > 33.5 && data[2] < 36.0) { valor = 25; setSomatorio(getSomatorio() + valor); } else if (data[2] > 35.0 && data[2] < 40.0) { valor = 100; setSomatorio(getSomatorio() + valor); } resultados.add(" r:" + (int) raio + " (X:" + (int) pt.x + "-Y:" + (int) pt.y + ") Val: " + valor); vetMat[i] = _reco.CriaMascara(pt, data[2], imgIn); output = _reco.CalculaHistograma(vetMat[i], imageA); /* System.out.println("histogram\n"+output.dump()); for (int j = 0; j < vetMatFile.length - 1; j++) { Mat ab = vetMatFile[j]; System.out.println("histogram\n"+vetMatFile[j].dump()); double distance = Imgproc.compareHist(output, vetMatFile[j], Imgproc.CV_COMP_CORREL); System.out.print("ok"); }*/ if (criaBase) { FileWriter arq = null; try { String nome = "X" + (int) pt.x + "Y" + (int) pt.y + "R" + (int) raio; // Imgcodecs.imwrite("baseConhecimento/" + nome + "M.png", vetMat[i]); arq = new FileWriter("baseConhecimento/" + nome + ".txt"); PrintWriter gravarArq = new PrintWriter(arq); gravarArq.print(output.dump()); arq.close(); // Imgcodecs.imwrite("baseConhecimento/" + nome + ".yml", output); } catch (IOException ex) { Logger.getLogger(Circulo.class.getName()).log(Level.SEVERE, null, ex); } finally { try { arq.close(); } catch (IOException ex) { Logger.getLogger(Circulo.class.getName()).log(Level.SEVERE, null, ex); } } } Imgproc.circle(imageA, pt, raio, new Scalar(0, 0, 255), 2); Imgproc.circle(imageA, pt, 2, new Scalar(0, 255, 0), 1); } return imageA; }
From source file:fi.conf.tabare.ARDataProvider.java
private void detect() { //Mat composite_image; Mat input_image = new Mat(); Mat undistorted_image = new Mat(); Mat circles = new Mat(); MatOfKeyPoint mokp = new MatOfKeyPoint(); Mat cameraMatrix = null;/*from ww w.java 2 s. c o m*/ //List<Mat> channels = new LinkedList<>(); //Loop while (running) { try { if (inputVideo.read(input_image)) { Mat preview_image = null; if (selectedView == View.calib) preview_image = input_image.clone(); //Imgproc.cvtColor(input_image, input_image, Imgproc.COLOR_RGB2HSV); //Core.split(input_image, channels); Imgproc.cvtColor(input_image, input_image, Imgproc.COLOR_BGR2GRAY); //Imgproc.equalizeHist(input_image, input_image); input_image.convertTo(input_image, -1, params.contrast, params.brightness); //image*contrast[1.0-3.0] + brightness[0-255] doBlur(input_image, input_image, params.blur, params.blurAmount); if (selectedView == View.raw) preview_image = input_image.clone(); if (params.enableDistortion) { if (cameraMatrix == null) cameraMatrix = Imgproc.getDefaultNewCameraMatrix(Mat.eye(3, 3, CvType.CV_64F), new Size(input_image.width(), input_image.height()), true); Imgproc.warpAffine(input_image, input_image, shiftMat, frameSize); if (undistorted_image == null) undistorted_image = new Mat((int) frameSize.width * 2, (int) frameSize.height * 2, CvType.CV_64F); Imgproc.undistort(input_image, undistorted_image, cameraMatrix, distCoeffs); input_image = undistorted_image.clone(); if (selectedView == View.dist) preview_image = input_image.clone(); } // if(background == null) background = input_image.clone(); // if(recaptureBg){ // backgSubstractor.apply(background, background); // System.out.println(background.channels() + " " + background.size() ); // System.out.println(input_image.channels() + " " + input_image.size() ); // recaptureBg = false; // } // if(dynamicBGRemoval){ // //Imgproc.accumulateWeighted(input_image, background, dynamicBGAmount); // //Imgproc.accumulateWeighted(input_image, background, 1.0f); // //Core.subtract(input_image, background, input_image); // //Core.bitwise_xor(input_image, background, input_image); // // doBlur(input_image, background, Blur.normal_7x7, 0); //Blur a little, to get nicer result when substracting // backgSubstractor.apply(background, background, dynamicBGAmount); // } // if(background != null) Core.add(input_image, background, input_image); if (params.blobTracking) { Mat blobs_image = input_image.clone(); Imgproc.threshold(blobs_image, blobs_image, params.blobThreshold, 254, (params.blobThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY)); Size kernelSize = null; switch (params.blobMorpthKernelSize) { case size_3x3: kernelSize = new Size(3, 3); break; case size_5x5: kernelSize = new Size(5, 5); break; case size_7x7: kernelSize = new Size(7, 7); break; case size_9x9: kernelSize = new Size(9, 9); break; } int kernelType = -1; switch (params.blobMorphKernelShape) { case ellipse: kernelType = Imgproc.MORPH_ELLIPSE; break; case rect: kernelType = Imgproc.MORPH_RECT; break; default: break; } switch (params.blobMorphOps) { case dilate: Imgproc.dilate(blobs_image, blobs_image, Imgproc.getStructuringElement(kernelType, kernelSize)); break; case erode: Imgproc.erode(blobs_image, blobs_image, Imgproc.getStructuringElement(kernelType, kernelSize)); break; default: break; } if (blobFeatureDetector == null) blobFeatureDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB); blobFeatureDetector.detect(blobs_image, mokp); blobData.add(mokp); if (selectedView == View.blob) preview_image = blobs_image.clone(); blobs_image.release(); } if (params.tripTracking) { Mat trips_image = undistorted_image.clone(); if (params.tripEnableThresholding) if (params.tripAdaptThreshold) { Imgproc.adaptiveThreshold(trips_image, trips_image, 255, (params.tripThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY), Imgproc.ADAPTIVE_THRESH_MEAN_C, 5, params.tripThreshold * 0.256f); } else { Imgproc.threshold(trips_image, trips_image, params.tripThreshold, 255, (params.tripThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY)); } switch (params.tripMorphOps) { case dilate: Imgproc.dilate(trips_image, trips_image, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3, 3))); break; case erode: Imgproc.erode(trips_image, trips_image, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3, 3))); break; default: break; } //Imgproc.HoughCircles(tres, circ, Imgproc.CV_HOUGH_GRADIENT, 1, tres.height()/8, 80, 1+p.par4, p.par5, p.par6); Imgproc.HoughCircles(trips_image, circles, Imgproc.CV_HOUGH_GRADIENT, params.tripDP, params.tripCenterDist, params.tripCannyThresh, params.tripAccumThresh, params.tripRadMin, params.tripRadMax); for (int i = 0; i < circles.cols(); i++) { double[] coords = circles.get(0, i); if (coords == null || coords[0] <= 1 || coords[1] <= 1) continue; //If the circle is off the limits, or too small, don't process it. TripcodeCandidateSample tc = new TripcodeCandidateSample(undistorted_image, coords); if (tc.isValid()) tripcodeData.add(tc); } if (selectedView == View.trip) preview_image = trips_image.clone(); trips_image.release(); } if (preview_image != null) { camPreviewPanel.updatePreviewImage(preview_image); preview_image.release(); } } else { System.out.println("frame/cam failiure!"); } } catch (Exception e) { e.printStackTrace(); running = false; } //FPS calculations if (camPreviewPanel != null) { long t = System.currentTimeMillis(); detectTime = (t - lastFrameDetectTime); lastFrameDetectTime = t; camPreviewPanel.updateDetectTime(detectTime); } } //De-init circles.release(); undistorted_image.release(); input_image.release(); inputVideo.release(); shiftMat.release(); }
From source file:logic.featurepointextractor.EyeIrisesFPE.java
@Override public Point[] detect(MatContainer mc) { Mat eyePairMat = mc.grayEyePairMat;/* w w w . ja v a 2s . c o m*/ Rect eyePairRect = mc.eyePairRect; Rect faceRect = mc.faceRect; //search for eye centers Mat circlesMat = new Mat(); double minDist = 2 * eyePairRect.width / 5; int minRad = eyePairRect.height / 5; int maxRad = 2 * eyePairRect.height / 3; Imgproc.HoughCircles(eyePairMat, circlesMat, Imgproc.CV_HOUGH_GRADIENT, 3.0, minDist, 200.0, 20.0, minRad, maxRad); float arr1[] = new float[3]; float arr2[] = new float[3]; if (circlesMat.size().width == 2) { circlesMat.get(0, 0, arr1); circlesMat.get(0, 1, arr2); float f11 = arr1[0], f12 = arr1[1], f21 = arr2[0], f22 = arr2[1]; if (Math.abs(f11 - f21) < Parameters.irisXDifferencesThreshold * eyePairRect.width && Math.abs(f12 - f22) > Parameters.irisYDifferencesThreshold) { //find where left and right eye if (f11 < f21) //left-right return new Point[] { new Point(f11 + faceRect.x + eyePairRect.x, f12 + faceRect.y + eyePairRect.y), new Point(f21 + faceRect.x + eyePairRect.x, f22 + faceRect.y + eyePairRect.y) }; else //right-left return new Point[] { new Point(f21 + faceRect.x + eyePairRect.x, f22 + faceRect.y + eyePairRect.y), new Point(f11 + faceRect.x + eyePairRect.x, f12 + faceRect.y + eyePairRect.y) }; } } LOG.warn("Extract eye iris: FAIL"); return null; }
From source file:opencvtesting.OpenCVTesting.java
/** * @param args the command line arguments *///from w ww . j a v a 2 s . co m public static void main(String[] args) throws IOException { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ CameraWindow cWindow = new CameraWindow(); cWindow.setVisible(true); Random gen = new Random(); JFrame frameF = new JFrame(); System.loadLibrary("opencv_java248"); VideoCapture camera = new VideoCapture(0); camera.open(0); //Useless if (!camera.isOpened()) { System.out.println("Camera Error"); } else { System.out.println("Camera OK?"); } frame = new Mat(); frame_gray = new Mat(); camera.read(frame); showResult(frame, frameF); dst = new Mat(frame.size(), frame.type()); mapX = new Mat(frame.size(), CvType.CV_32FC1); mapY = new Mat(frame.size(), CvType.CV_32FC1); boolean bEnd = true; while (bEnd) { camera.read(frame); Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(frame_gray, frame_gray, new Size(1, 1), 2); Imgproc.equalizeHist(frame_gray, frame_gray); Mat circles = new Mat(); Imgproc.HoughCircles(frame_gray, circles, Imgproc.CV_HOUGH_GRADIENT, cWindow.get_dp(), frame_gray.rows() / 8, cWindow.get_param1(), cWindow.get_param2(), 0, 0); //System.out.println(circles.rows()); for (int i = 0; i < circles.cols(); i++) { double[] circle = circles.get(0, i); Point center = new Point(Math.round(circle[0]), Math.round(circle[1])); int radius = (int) Math.round(circle[2]); Core.circle(frame, center, 3, new Scalar(gen.nextInt(), gen.nextInt(), gen.nextInt()), -1, 8, 0); Core.circle(frame, center, radius, new Scalar(gen.nextInt(), gen.nextInt(), gen.nextInt()), 3, 8, 0); } showResult(frame, frameF); } //Highgui.imwrite("camera1.jpg", frame); //System.out.println("OK"); }
From source file:org.akvo.caddisfly.helper.ImageHelper.java
License:Open Source License
/** * Gets the center of the backdrop in the test chamber * * @param bitmap the photo to analyse// w w w . j a v a2 s.com * @return the center point of the found circle */ public static Point getCenter(@NonNull Bitmap bitmap) { // convert bitmap to mat Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC1); Mat grayMat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC1); Utils.bitmapToMat(bitmap, mat); // convert to grayScale int colorChannels = (mat.channels() == 3) ? Imgproc.COLOR_BGR2GRAY : ((mat.channels() == 4) ? Imgproc.COLOR_BGRA2GRAY : 1); Imgproc.cvtColor(mat, grayMat, colorChannels); // reduce the noise so we avoid false circle detection //Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2); // param1 = gradient value used to handle edge detection // param2 = Accumulator threshold value for the // cv2.CV_HOUGH_GRADIENT method. // The smaller the threshold is, the more circles will be // detected (including false circles). // The larger the threshold is, the more circles will // potentially be returned. double param1 = 10, param2 = 100; // create a Mat object to store the circles detected Mat circles = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC1); // find the circle in the image Imgproc.HoughCircles(grayMat, circles, Imgproc.CV_HOUGH_GRADIENT, RESOLUTION_INVERSE_RATIO, (double) MIN_CIRCLE_CENTER_DISTANCE, param1, param2, MIN_RADIUS, MAX_RADIUS); int numberOfCircles = (circles.rows() == 0) ? 0 : circles.cols(); // draw the circles found on the image if (numberOfCircles > 0) { double[] circleCoordinates = circles.get(0, 0); int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1]; org.opencv.core.Point center = new org.opencv.core.Point(x, y); int foundRadius = (int) circleCoordinates[2]; // circle outline Imgproc.circle(mat, center, foundRadius, COLOR_GREEN, 4); Utils.matToBitmap(mat, bitmap); return new Point((int) center.x, (int) center.y); } return null; }
From source file:org.openpnp.vision.FluentCv.java
License:Open Source License
public FluentCv findCirclesHough(int minDiameter, int maxDiameter, int minDistance, String... tag) { Mat circles = new Mat(); Imgproc.HoughCircles(mat, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minDistance, 80, 10, minDiameter / 2, maxDiameter / 2);// w ww .j a va2 s . c om store(circles, tag); return this; }