ออปเปอเรเตอร์ Sobel สำหรับ edge detection ช่วยหาขอบในภาพทั้งแนวนอนและแนวตั้ง
เมธอด Sobel() ของคลาส Imgproc ใช้อัลกอริทึม Sobel Edge Detection บนภาพที่กำหนด เมธอดนี้รับ:
- สองอ็อบเจ็กต์ Mat แทน source และ destination images
- ตัวแปร integer แทน depth ของภาพ
- สองตัวแปร double เก็บค่า x และ y derivatives
ตัวอย่างโค้ด
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class SobelEdgeDetection {
public static void main(String[] args) {
// โหลด OpenCV core library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String file = "D:\\Images\\win2.jpg";
Mat src = Imgcodecs.imread(file);
// สร้าง matrix ว่างสำหรับ destination image
Mat dst = new Mat();
// ใช้ sobel derivative x:0 y:1
Imgproc.Sobel(src, dst, -1, 0, 1);
HighGui.imshow("Sobel - x:0 & y:1", dst);
HighGui.waitKey();
// ใช้ sobel derivative x:1 y:0
Imgproc.Sobel(src, dst, -1, 1, 0);
HighGui.imshow("Sobel - x:1 & y:0", dst);
HighGui.waitKey();
// ใช้ sobel derivative x:1 y:1
Imgproc.Sobel(src, dst, -1, 1, 1);
HighGui.imshow("Sobel - x:1 & y:1", dst);
HighGui.waitKey();
}
}
ผลลัพธ์
เมื่อรันโปรแกรมจะได้หน้าต่างดังนี้:
Sobel - x:0 & y:1 − 
Sobel - x:1 & y:0 − 
Sobel - x:1 & y:1 − 