简体   繁体   English

OpenCV Java-如何裁剪图像中检测到的圆

[英]OpenCV Java - How to crop a circle detected in a image

I am using the following algorithm from OpenCV. 我正在使用来自OpenCV的以下算法。

1) Detect circles in the image. 1)检测图像中的圆圈。

2) Draw the detected circle on the image. 2)在图像上绘制检测到的圆。 (This is already done, the code is as follow). (这已经完成,代码如下)。

3) Crop the detected region. 3)裁剪检测到的区域。

But, the question is how can i crop out this detected region on the image? 但是,问题是如何在图像上裁剪出该检测到的区域?

1. create a mask: 1.创建一个面具:

Mat mask = new Mat(src.rows(), src.cols(), CvType.CV_8U, Scalar.all(0));

2. Draw the circle on that mask (set thickness to -1 to fill the circle): 2.在该蒙版上绘制圆圈(将厚度设置为-1以填充圆圈):

Imgproc.circle(mask, center, radius, new Scalar(255,255,255), -1, 8, 0 );

3. Copy the image using the mask: 3.使用遮罩复制图像:

Mat masked = new Mat();
src.copyTo( masked, mask );

4. Apply Threshold 4.应用阈值

Mat thresh = new Mat();
Imgproc.threshold( mask, thresh, 1, 255, Imgproc.THRESH_BINARY );

5. Find contour 5.找到轮廓

List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(thresh, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

6. Crop 6.作物

Rect rect = Imgproc.boundingRect(contours.get(0));
Mat cropped = masked.submat(rect);

Full code example (OpenCV 3.4): 完整代码示例(OpenCV 3.4):

在此处输入图片说明

import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

class HoughCirclesRun {
    public void run(String[] args) {
        String filename = "smartie.png";
        // Load an image
        Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
        // Check if image is loaded fine
        if( src.empty() ) {
            System.out.println("Error opening image!");
        }

        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.medianBlur(gray, gray, 5);
        Mat circles = new Mat();
        Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
                (double)gray.rows()/16, // change this value to detect circles with different distances to each other
                100.0, 30.0, 1, 30); // change the last two parameters (min_radius & max_radius) to detect larger circles

        Mat mask = new Mat(src.rows(), src.cols(), CvType.CV_8U, Scalar.all(0));

        for (int x = 0; x < circles.cols(); x++) {
            double[] c = circles.get(0, x);
            Point center = new Point(Math.round(c[0]), Math.round(c[1]));
            // circle outline
            int radius = (int) Math.round(c[2]);
            Imgproc.circle(mask, center, radius, new Scalar(255,255,255), -1, 8, 0 );
        }

        Mat masked = new Mat();
        src.copyTo( masked, mask );

        // Apply Threshold
        Mat thresh = new Mat();
        Imgproc.threshold( mask, thresh, 1, 255, Imgproc.THRESH_BINARY );

        // Find Contour
        List<MatOfPoint> contours = new ArrayList<>();
        Imgproc.findContours(thresh, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

        // Crop
        Rect rect = Imgproc.boundingRect(contours.get(0));
        Mat cropped = masked.submat(rect);

        HighGui.imshow("Cropped circle", cropped);
        HighGui.waitKey();
        System.exit(0);
    }
}
public class CropCircle {
    public static void main(String[] args) {
        // Load the native library.
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        new HoughCirclesRun().run(args);
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM