คุณสามารถตรวจจับเส้นตรงในภาพได้โดยใช้ Hough line transform มี 2 แบบใน OpenCV ได้แก่ Standard Hough line transform และ Probabilistic Hough Line Transform
คุณสามารถใช้ Standard Hough line transform ได้ด้วยเมธอด HoughLines() ของคลาส Imgproc เมธอดนี้รับ:
- สองอ็อบเจ็กต์ Mat แทน source image และ vector ที่เก็บพารามิเตอร์ (r, Φ) ของเส้น
- สองตัวแปร double แทน resolution ของพารามิเตอร์ r (pixels) และ Φ (radians)
- ตัวแปร integer แทน minimum number of intersections เพื่อ "detect" เส้น
คุณสามารถใช้ Probabilistic Hough line transform ได้ด้วยเมธอด HoughLinesP() (พารามิเตอร์เหมือนกัน)
คุณสามารถตรวจจับขอบในภาพได้โดยใช้เมธอด Canny() ของคลาส Imgproc เมธอดนี้รับ:
- สองอ็อบเจ็กต์ Mat แทน source และ destination images
- สองตัวแปร double เก็บค่า threshold values
ขั้นตอนการตรวจจับขอบด้วย Canny edge detector:
- อ่านเนื้อหาภาพต้นทางด้วยเมธอด imread() ของคลาส Imgcodecs
- แปลงเป็น grayscale ด้วย cvtColor() ของ Imgproc
- Blur ภาพ grayscale ด้วย blur() ของ Imgproc ด้วย kernel value 3
- ใช้ canny edge detection บนภาพที่ blur ด้วย canny() ของ Imgproc
- สร้าง matrix ว่างที่มีค่าทั้งหมดเป็น 0
- เพิ่มขอบที่ตรวจพบเข้าไปด้วย copyTo() ของคลาส Mat
ตัวอย่างโค้ด
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.CvType;
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 HoughLineTransform extends Application {
@Override
public void start(Stage stage) throws IOException {
// โหลด OpenCV core library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String file = "D:\\Images\\road4.jpg";
Mat src = Imgcodecs.imread(file);
// แปลงภาพเป็น Grayscale
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGBA2GRAY);
// ตรวจจับขอบ
Mat edges = new Mat();
Imgproc.Canny(gray, edges, 60, 60*3, 3, false);
// เปลี่ยนสีของ canny
Mat cannyColor = new Mat();
Imgproc.cvtColor(edges, cannyColor, Imgproc.COLOR_GRAY2BGR);
// ตรวจจับ hough lines จาก canny
Mat lines = new Mat();
Imgproc.HoughLines(edges, lines, 1, Math.PI/180, 150);
for (int i = 0; i < lines.rows(); i++) {
double[] data = lines.get(i, 0);
double rho = data[0];
double theta = data[1];
double a = Math.cos(theta);
double b = Math.sin(theta);
double x0 = a * rho;
double y0 = b * rho;
// วาดเส้นบนภาพ
Point pt1 = new Point();
Point pt2 = new Point();
pt1.x = Math.round(x0 + 1000 * (-b));
pt1.y = Math.round(y0 + 1000 * (a));
pt2.x = Math.round(x0 - 1000 * (-b));
pt2.y = Math.round(y0 - 1000 * (a));
Imgproc.line(cannyColor, pt1, pt2, new Scalar(0, 0, 255), 3);
}
// แปลง matrix เป็น JavaFX writable image
Image img = HighGui.toBufferedImage(cannyColor);
WritableImage writableImage = SwingFXUtils.toFXImage((BufferedImage) img, null);
// ตั้งค่า ImageView
ImageView imageView = new ImageView(writableImage);
imageView.setX(10);
imageView.setY(10);
imageView.setFitWidth(575);
imageView.setPreserveRatio(true);
// ตั้งค่า Scene
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);
}
}
รูปภาพ Input

ผลลัพธ์
เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้:
