gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:39:10 27 4
gpt4 key购买 nike

我正在使用 OpenCV 中的以下算法。

1) 检测图像中的圆圈。

2) 在图像上绘制检测到的圆圈。 (这已经完成了,代码如下)。

3) 裁剪检测到的区域。

但是,问题是我如何裁剪掉图像上检测到的区域?

最佳答案

<强>1。创建蒙版:

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

<强>2。在该蒙版上绘制圆圈(将厚度设置为-1以填充圆圈):

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

<强>3。使用蒙版复制图像:

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

<强>4。应用阈值

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

<强>5。查找轮廓

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

<强>6。裁剪

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

完整代码示例(OpenCV 3.4):

enter image description here

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);
}
}

enter image description here

关于java - OpenCV Java - 如何裁剪图像中检测到的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48701160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com