您可以使用imgproc类的houghlinesp()方法应用概率hough线变换,该方法接受以下参数:
表示源图像和存储线条参数(r, φ)的向量的两个mat对象。
表示参数r(像素)和φ(弧度)的分辨率的两个double变量。
表示“检测”一条线所需的最小交点数的整数。
示例以下java示例使用opencv的概率hough线变换检测图像中的线条:
import java.awt.image;import java.awt.image.bufferedimage;import java.io.ioexception;import javafx.application.application;import javafx.embed.swing.swingfxutils;import javafx.scene.group;import javafx.scene.scene;import javafx.scene.image.imageview;import javafx.scene.image.writableimage;import javafx.stage.stage;import org.opencv.core.core;import org.opencv.core.mat;import org.opencv.core.point;import org.opencv.core.scalar;import org.opencv.highgui.highgui;import org.opencv.imgcodecs.imgcodecs;import org.opencv.imgproc.imgproc;public class houghlineprobabilistictransform extends application { public void start(stage stage) throws ioexception { //loading the opencv core library system.loadlibrary( core.native_library_name ); string file ="d:\images\road4.jpg"; mat src = imgcodecs.imread(file); //converting the image to gray mat gray = new mat(); imgproc.cvtcolor(src, gray, imgproc.color_rgba2gray); //detecting the edges mat edges = new mat(); imgproc.canny(gray, edges, 60, 60*3, 3, false); // changing the color of the canny mat cannycolor = new mat(); imgproc.cvtcolor(edges, cannycolor, imgproc.color_gray2bgr); //detecting the hough lines from (canny) mat lines = new mat(); imgproc.houghlinesp(edges, lines, 1, math.pi/180, 50, 50, 10); for (int i = 0; i < lines.rows(); i++) { double[] data = lines.get(i, 0); //drawing lines on the image point pt1 = new point(data[0], data[1]); point pt2 = new point(data[2], data[3]); imgproc.line(cannycolor, pt1, pt2, new scalar(0, 0, 255), 3); } //converting matrix to javafx writable image image img = highgui.tobufferedimage(cannycolor); writableimage writableimage= swingfxutils.tofximage((bufferedimage) img, null); //setting the image view imageview imageview = new imageview(writableimage); imageview.setx(10); imageview.sety(10); imageview.setfitwidth(575); imageview.setpreserveratio(true); //setting the scene object group root = new group(imageview); scene scene = new scene(root, 595, 400); stage.settitle("hough line transform"); stage.setscene(scene); stage.show(); } public static void main(string args[]) { launch(args); }}
输入图像
输出执行后,上述代码将产生以下输出 −
以上就是在java中实现opencv的概率霍夫线变换的详细内容。
