Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

การใช้งาน Java ของ OpenCV Hough Circle Transform


คุณสามารถตรวจจับวงกลมในภาพที่กำหนดโดยใช้การแปลงวงกลม Hough คุณสามารถใช้ การแปลง Hough Circle โดยใช้ HoughCircles() วิธี วิธีนี้ยอมรับพารามิเตอร์ต่อไปนี้ -

  • วัตถุ Mat ที่แสดงภาพอินพุต

  • วัตถุ Mat เพื่อเก็บเวกเตอร์ผลลัพธ์ของวงกลมที่พบ

  • ตัวแปรจำนวนเต็มแสดงถึงวิธีการตรวจหา

  • ตัวแปรคู่สองตัวที่แสดงอัตราส่วนผกผันของความละเอียดของตัวสะสมต่อความละเอียดของภาพและระยะห่างขั้นต่ำระหว่างจุดศูนย์กลางของวงกลมที่ตรวจพบ

ตัวอย่าง

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;
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;
public class HoughCircleTransform extends Application {
   public void start(Stage stage) throws IOException {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      String file ="D:\\Images\\compass.jpg";
      Mat src = Imgcodecs.imread(file);
      //Converting the image to Gray
      Mat gray = new Mat();
      Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGBA2GRAY);
      //Blurring the image
      Mat blur = new Mat();
      Imgproc.medianBlur(gray, blur, 5);
      //Detecting the Hough Circles
      Mat circles = new Mat();
      Imgproc.HoughCircles(blur, circles, Imgproc.HOUGH_GRADIENT, Math.PI/180, 150);
      for (int i = 0; i < circles.cols(); i++ ) {
         double[] data = circles.get(0, i);
         Point center = new Point(Math.round(data[0]), Math.round(data[1]));
         // circle center
         Imgproc.circle(src, center, 1, new Scalar(0, 0, 255), 3, 8, 0 );
         // circle outline
         int radius = (int) Math.round(data[2]);
         Imgproc.circle(src, center, radius, new Scalar(0,0,255), 3, 8, 0 );
      }
      //Converting matrix to JavaFX writable image
      Image img = HighGui.toBufferedImage(src);
      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 Circle Transform");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]) {
      launch(args);
   }
}

ใส่รูปภาพ

การใช้งาน Java ของ OpenCV Hough Circle Transform

ผลลัพธ์

ในการดำเนินการ ข้างต้นสร้างผลลัพธ์ต่อไปนี้ −

การใช้งาน Java ของ OpenCV Hough Circle Transform