| 
 | 
 
 
 楼主 |
发表于 2023-10-11 15:59:25
|
显示全部楼层
 
 
 
using System; 
using System.Drawing; 
using Emgu.CV; 
using Emgu.CV.CvEnum; 
using Emgu.CV.Structure; 
 
class Program 
{ 
    static void Main(string[] args) 
    { 
        // 加载图像 
        Mat image = CvInvoke.Imread("wafer_cutting_edge.jpg", ImreadModes.Color); 
 
        // 转换为灰度图像 
        Mat gray = new Mat(); 
        CvInvoke.CvtColor(image, gray, ColorConversion.Bgr2Gray); 
 
        // 高斯模糊 
        Mat blur = new Mat(); 
        CvInvoke.GaussianBlur(gray, blur, new Size(5, 5), 0); 
 
        // Canny边缘检测 
        Mat edges = new Mat(); 
        CvInvoke.Canny(blur, edges, 50, 150); 
 
        // Hough直线检测 
        LineSegment2D[] lines = CvInvoke.HoughLinesP(edges, 1, Math.PI / 180, 50, 50, 10); 
 
        // 计算缺陷到边的距离 
        int defectDistanceThreshold = 10; // 设置缺陷到边的距离阈值 
        foreach (LineSegment2D line in lines) 
        { 
            // 计算直线的长度 
            double lineLength = Math.Sqrt(Math.Pow(line.P1.X - line.P2.X, 2) + Math.Pow(line.P1.Y - line.P2.Y, 2)); 
 
            // 计算直线的斜率 
            double slope = (double)(line.P2.Y - line.P1.Y) / (line.P2.X - line.P1.X); 
 
            // 计算直线的截距 
            double intercept = line.P1.Y - slope * line.P1.X; 
 
            // 判断左侧缺陷到边的距离是否小于阈值 
            if (Math.Abs(intercept) < defectDistanceThreshold) 
            { 
                Console.WriteLine("Left defect detected!"); 
            } 
 
            // 判断右侧缺陷到边的距离是否小于阈值 
            if (Math.Abs(image.Width - slope * image.Width - intercept) < defectDistanceThreshold) 
            { 
                Console.WriteLine("Right defect detected!"); 
            } 
        } 
 
        // 显示结果图像 
        CvInvoke.Imshow("Result", image); 
        CvInvoke.WaitKey(0); 
        CvInvoke.DestroyAllWindows(); 
    } 
} 
 
 |   
 
 
 
 |