BlockTester.cs 2.7 KB
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wafer_die_counter
{
    internal class BlockTester
    {

        internal static List<KeyPoint> FindBlock(Mat src)
        {

            // 创建一个Mat对象
            Mat mat = new Mat(src.Size(), MatType.CV_8UC1, new Scalar(255));
            List<List<Point>> k = new List<List<Point>>();
            // 遍历Mat对象的元素
            for (int row = 0; row < mat.Rows; row++)
            {
                for (int col = 0; col < mat.Cols; col++)
                {
                    // 使用Get方法获取元素值
                    byte value = mat.Get<byte>(row, col);
                    byte value2 = src.Get<byte>(row, col);
                    //Console.WriteLine($"{row},{col} = {value2}");
                    if (value == 255 && value2==0)
                    {
                        Cv2.FloodFill(src, new Point(col,row), new Scalar(125), out Rect rect, Scalar.All(0), Scalar.All(0), FloodFillFlags.Link8);

                        if (rect.Width == 746)
                            continue;

                        List<Point> selectedPixels = new List<Point>();
                        
                        for (int y = 0; y < src[rect].Rows; y++)
                        {
                            for (int x = 0; x < src[rect].Cols; x++)
                            {
                                if (src[rect].Get<byte>(y, x) == 125)
                                {
                                    mat.Set(y+rect.Y,x + rect.X, 125);

                                    selectedPixels.Add(new Point(x + rect.X, y + rect.Y));
                                }
                            }
                        }
                        k.Add(selectedPixels);
                    }
                }

            }
            int count = 0;
            for (int i = 0; i < k.Count; i++)
            {
                if (k[i].Count > 10)
                {
                    count = count + 3;
                }
                if (k[i].Count > 8)
                {
                    count = count + 2;
                }
                //else if (k[i].Count > 7)
                //    count = count + 2;
                //else if (k[i].Count > 6)
                //    count = count + 2;
                else
                    count++;
            }

                // 显示结果图像
                Cv2.ImShow("Magic Wand Result", mat);
            Cv2.ImWrite("d:\\111.png",mat);
            Cv2.ImWrite("d:\\222.png",src);
                Cv2.WaitKey(1000000);
            return new List<KeyPoint>();
        }

    }
}