import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
//霍夫变换找图中的圆环
public class imageCriDis {
public static void main(String[] args)
{
String relativelyPath = System.getProperty("user.dir");
System.load(relativelyPath + "\\opencv_java340-x64.dll");
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src = Imgcodecs.imread("E:\\a.png");
Mat dst = src.clone();
Imgproc.cvtColor(src, dst, Imgproc.COLOR_BGR2GRAY);
Mat circles = new Mat();
Imgproc.HoughCircles(dst, circles, Imgproc.HOUGH_GRADIENT, 1, 10, 440, 50, 0, 345);
// Imgproc.HoughCircles(dst, circles, Imgproc.HOUGH_GRADIENT, 1, 100,440, 50, 0, 1000000);
for (int i = 0; i < circles.cols(); i++)
{
double[] vCircle = circles.get(0, i);
Point center = new Point(vCircle[0], vCircle[1]);
int radius = (int) Math.round(vCircle[2]);
// circle center
Imgproc.circle(src, center, 3, new Scalar(0, 255, 0), -1, 8, 0);
// circle outline
Imgproc.circle(src, center, radius, new Scalar(0, 0, 255), 3, 8, 0);
}
Imgcodecs.imwrite("E:\\b.jpg", src);
}
}
Java处理opencv 需要调用dll
下载连接://download.csdn.net/download/weixin_40652498/11997095
maven依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv-platform -->
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.1-1.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
</dependencies>