Java Code Examples for org.opencv.imgproc.Imgproc#HoughCircles

The following examples show how to use org.opencv.imgproc.Imgproc#HoughCircles . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From real_time_circle_detection_android with MIT License 5 votes vote down vote up
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat input = inputFrame.gray();
    Mat circles = new Mat();
    Imgproc.blur(input, input, new Size(7, 7), new Point(2, 2));
    Imgproc.HoughCircles(input, circles, Imgproc.CV_HOUGH_GRADIENT, 2, 100, 100, 90, 0, 1000);

    Log.i(TAG, String.valueOf("size: " + circles.cols()) + ", " + String.valueOf(circles.rows()));

    if (circles.cols() > 0) {
        for (int x=0; x < Math.min(circles.cols(), 5); x++ ) {
            double circleVec[] = circles.get(0, x);

            if (circleVec == null) {
                break;
            }

            Point center = new Point((int) circleVec[0], (int) circleVec[1]);
            int radius = (int) circleVec[2];

            Imgproc.circle(input, center, 3, new Scalar(255, 255, 255), 5);
            Imgproc.circle(input, center, radius, new Scalar(255, 255, 255), 2);
        }
    }

    circles.release();
    input.release();
    return inputFrame.rgba();
}
 
Example 2
Source File: MainActivity.java    From MOAAP with MIT License 5 votes vote down vote up
void HoughCircles() {
    Mat grayMat = new Mat();
    Mat cannyEdges = new Mat();
    Mat circles = new Mat();

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    Imgproc.Canny(grayMat, cannyEdges, 10, 100);

    Imgproc.HoughCircles(cannyEdges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, cannyEdges.rows() / 15);//, grayMat.rows() / 8);

    Mat houghCircles = new Mat();
    houghCircles.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1);

    //Drawing lines on the image
    for (int i = 0; i < circles.cols(); i++) {
        double[] parameters = circles.get(0, i);
        double x, y;
        int r;

        x = parameters[0];
        y = parameters[1];
        r = (int) parameters[2];

        Point center = new Point(x, y);

        //Drawing circles on an image
        Imgproc.circle(houghCircles, center, r, new Scalar(255, 0, 0), 1);
    }

    //Converting Mat back to Bitmap
    Utils.matToBitmap(houghCircles, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 3
Source File: HoughCircles.java    From opencv-fun with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void main (String[] args) {
		CVLoader.load();
		Mat orig = Highgui.imread("data/topdown-6.jpg");
		Mat gray = new Mat();
		orig.copyTo(gray);
		
		// blur
//		Imgproc.medianBlur(gray, gray, 5);
//		Imgproc.GaussianBlur(gray, gray, new Size(3, 3), 100);
		
		// convert to grayscale
		Imgproc.cvtColor(gray, gray, Imgproc.COLOR_BGR2GRAY);
		
		// do hough circles
		Mat circles = new Mat();
		int minRadius = 10;
		int maxRadius = 18;
		Imgproc.HoughCircles(gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minRadius, 120, 10, minRadius, maxRadius);
		System.out.println(circles);
		
		ImgWindow.newWindow(gray);
		ImgWindow wnd = ImgWindow.newWindow(orig);			
		
		while(!wnd.closed) {
			wnd.setImage(orig);
			Graphics2D g = wnd.begin();
			g.setColor(Color.MAGENTA);
			g.setStroke(new BasicStroke(3));
			for(int i = 0; i < circles.cols(); i++) {
				double[] circle = circles.get(0, i);
				g.drawOval((int)circle[0] - (int)circle[2], (int)circle[1] - (int)circle[2], (int)circle[2] * 2, (int)circle[2] * 2);
			}		
			wnd.end();
		}
	}
 
Example 4
Source File: BallDetector.java    From opencv-fun with GNU Affero General Public License v3.0 4 votes vote down vote up
private void extractCircles (Mat mask, List<Circle> balls, List<BallCluster> ballClusters, List<MatOfPoint> contours) {
	// clear input
	balls.clear();
	ballClusters.clear();
	contours.clear();
	
	// find the contours
	Imgproc.findContours(mask.clone(), contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
	
	// iterate through the contours, find single balls and clusters of balls touching each other
	double minArea = Math.PI * (calib.getBallRadius() * 0.9f) * (calib.getBallRadius() * 0.9f); // minimal ball area
	double maxArea = Math.PI * (calib.getBallRadius() * 1.1f) * (calib.getBallRadius() * 1.1f); // maximal ball area

	for (int i = 0; i < contours.size(); i++) {
		double area = Imgproc.contourArea(contours.get(i));
		if (area > minArea) {				
			if (area < maxArea) {
				// we found a ball
				float[] radius = new float[1];
				Point center = new Point();
				Imgproc.minEnclosingCircle(new MatOfPoint2f(contours.get(i).toArray()), center, radius);
				balls.add(new Circle(center.x, center.y, calib.getBallRadius()));
			} else {
				// we found a cluster of balls
				int numBalls = (int)(area / (Math.PI * calib.getBallRadius() * calib.getBallRadius() * 0.9));
				
				// draw the contours to a bit mask
				Mat hough = Mat.zeros(mask.size(), CvType.CV_8U);
				Imgproc.drawContours(hough, contours, i, new Scalar(255, 255, 255), -2);
				
				// detect hough circles, try different params until we hit the number of balls
				Mat houghCircles = new Mat();
				int hit = 0;
				for(int j = 8; j < 20; j++) {
					Imgproc.HoughCircles(hough, houghCircles, Imgproc.CV_HOUGH_GRADIENT, 2, calib.getBallRadius() * 0.9 * 2, 255, j, (int)(calib.getBallRadius() * 0.9), (int)(calib.getBallRadius() * 1.1));
					if(houghCircles.cols() <= numBalls) {
						hit++;
						if(hit == 4) break;
					}
				}
				
				
				List<Circle> estimatedCircles = new ArrayList<Circle>();
				for(int j = 0; j < houghCircles.cols(); j++) {
					double[] circle = houghCircles.get(0, j);
					if(circle != null) {
						estimatedCircles.add(new Circle(circle[0], circle[1], calib.getBallRadius()));
					}
				}
				
				ballClusters.add(new BallCluster(contours.get(i), numBalls, estimatedCircles));
			}
		}
	}
}