首页 > 解决方案 > C#木材缺陷检测算法的实现与测试

问题描述

我正在实施文章中的结检测算法木板:https ://www.researchgate.net/publication/220665710_Automated_visual_inspection_system_for_wood_defect_classification_using_computational_intelligence_techniques

我对此有一些疑问。

谁能告诉我“属于”I^2 的确切含义是什么?

我在 List 中有我的种子,但我不知道这是否正确。接下来,在第 2.3 点,我必须创建一个具有最小-最大点和隶属函数 bj 的超盒模糊集。

问题是当我在图片中有两个结时,我不能合理地将一个超级框分成例如两个超级框。在我看来,我应该有一些对我有帮助的课程。对于第 2.3 点,我只使用只有种子坐标的输入模式。

谁能告诉我我应该怎么做才能将一个超级盒分成两个超级盒?

谢谢。

namespace inz_test
{
    class Input
    {
        Vector xy;
        Color color;
        double eukliedes;
        public Vector XY
        {
            get { return xy; }
            set { xy = value; }
        }
        public Color Pixel
        {
            get { return color; }
            set { color = value;}
        }
        public double Eukliedes
        {
            get { return eukliedes; }
            set { eukliedes = value; }
        }
    }
    public class TestPhotos
    {
        public TestPhotos(string path, Bitmap img)
        {    
            double counter = img.Height * img.Width;
            double sumaR = 0, sumaG = 0, sumaB = 0;
            int minR = 255, minG = 255, minB = 255;

            //histogram array
            var histogramR = new double[256];
            var histogramG = new double[256];
            var histogramB = new double[256];

            var x_hArrayInputAll = new List<Vector>();

            //test CSV files with data
            StringBuilder csv = new StringBuilder();
            StringBuilder csv1 = new StringBuilder();
            /* ###################### */

            //R,G,B value for
            for (int y = 0; y < img.Height; y++)
            {
                for (int x = 0; x < img.Width; x++)
                {
                    Color pixel = img.GetPixel(x, y);

                    //add R,G,B
                    histogramR[pixel.R]++;
                    histogramG[pixel.G]++;
                    histogramB[pixel.B]++;

                    //min for R,G and B channel
                    if (pixel.R < minR) minR = pixel.R;
                    if (pixel.G < minG) minG = pixel.G;
                    if (pixel.B < minB) minB = pixel.B;

                    //[0 1] range for all image pixel
                    Vector x_a = new Vector(Math.Round(((double)x / img.Width), 2),
                                            Math.Round(((double)y / img.Height), 2));
                    x_hArrayInputAll.Add(x_a);

                    sumaR += pixel.R;
                    sumaG += pixel.G;
                    sumaB += pixel.B;
                }
            }

            //csv path on .\Desktop\csv
            string path1 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string filepath = path1 + "\\csv\\" + path + ".csv";
            string filepath1 = path1 + "\\csv\\" + path + "_XY" + ".csv";
            string filepath2 = path1 + "\\csv\\" + path + "1.jpg";

            //cumulative histogram array
            double[] cumulativeR = new double[256];
            double[] cumulativeG = new double[256];
            double[] cumulativeB = new double[256];

            //cumulative[0]
            cumulativeR[0] = histogramR[0] / counter;
            cumulativeG[0] = histogramG[0] / counter;
            cumulativeB[0] = histogramB[0] / counter;

            //
            for (var n = 1; n < histogramR.Length; n++)
            {
                cumulativeR[n] = (histogramR[n] / counter) + cumulativeR[n - 1];
                cumulativeG[n] = (histogramG[n] / counter) + cumulativeG[n - 1];
                cumulativeB[n] = (histogramB[n] / counter) + cumulativeB[n - 1];

            }

            double fiR, fiG, fiB;
            double rhoR, rhoG, rhoB;
            double lamR, lamG, lamB;
            int sredniaR = (int)(sumaR/counter) ,sredniaG = (int)(sumaG / counter), sredniaB = (int)(sumaB / counter);

            //set intensity level per channel
            fiR = ((cumulativeR[(int)(sumaR / counter)]) * 0.01);
            fiG = ((cumulativeG[(int)(sumaG / counter)]) * 0.01);
            fiB = ((cumulativeB[(int)(sumaB / counter)]) * 0.01);

            //set additional color intensity per channel
            rhoR = ((fiR + (sredniaR)) / 2);
            rhoG = ((fiG + (sredniaG)) / 2);
            rhoB = ((fiB + (sredniaB)) / 2);

            //lambda for channel
            lamR = 50;
            lamG = 50;
            lamB = 50;


            if (fiR < lamR) { fiR += rhoR; }
            if (fiG < lamG) { fiG += rhoG; }
            if (fiB < lamB) { fiB += rhoB; }

            int maxH = 0, maxW = 0, minH = img.Height, minW = img.Width;
            double euk = 0;
            //Vector[] x_hArrayInput = new Vector[(int)counter];
            var x_hArrayInput = new List<Vector>();
            var inputList = new List<Input>();

            //test
            var list = new List<Tuple<Vector, Vector>>();
            int c = 0;
            double tau = 0.7;
            double zFunction = 0;

            Vector Vj1 = new Vector();
            Vector Wj1 = new Vector();

            for (int y = 0; y < img.Height; y++)
            {
                for (int x = 0; x < img.Width; x++)
                {

                    Color pixel = img.GetPixel(x, y);

                    //if (pixel.R <= fiR)
                    //{
                    //    if (pixel.G <= fiG)
                    //    {
                    //        if (pixel.B <= fiB)
                    //        {
                    //
                    if ((pixel.R + pixel.G + pixel.B) / 3 <= (fiR + fiG + fiB) / 3)
                    {

                        Vector x_h = new Vector(Math.Round(((double)x / img.Width), 2), Math.Round(((double)y / img.Height), 2));
                        x_hArrayInput.Add(x_h);
                        Input i = new Input();

                        i.XY = x_h;
                        i.Pixel = pixel;

                        if (c>0)
                        {
                            var item = inputList[inputList.Count - 1];
                            var R = Math.Pow((item.Pixel.R - i.Pixel.R),2);
                            var G = Math.Pow((item.Pixel.G - i.Pixel.G),2);
                            var B = Math.Pow((item.Pixel.B - i.Pixel.B),2);
                            double distance = Math.Sqrt(R + G + B);
                            i.Eukliedes = distance;
                            double l = Math.Sqrt(195075);

                            if (distance == 0) { zFunction = 1; }
                            else if (0 <= distance && distance <= (l/2)) { zFunction = 1 - 2 * (Math.Pow((distance / l), 2)); }
                            else if ((l/2) <= distance && distance <= l) { zFunction = 2*(Math.Pow(((distance-l) / l), 2)); }
                            else if (distance == l) { zFunction = 0; }

                            csv.AppendLine(string.Format("{0};{1};{2};{3};{4};{5};{6}", i.XY.X, i.XY.Y, i.Pixel.R, i.Pixel.G, i.Pixel.B, distance, zFunction));
                        }
                        else
                        {
                            Vj1 = x_h;
                            Wj1 = x_h;
                            csv.AppendLine(string.Format("{0};{1};{2}", i.XY.X, i.XY.Y, i.Pixel));
                        }
                        c++;

                        inputList.Add(i);

                        //test red pixel on image
                        img.SetPixel(x, y, Color.Red);

                        //set max and min
                        if (maxH < y) { maxH = y; }
                        if (maxW < x) { maxW = x; }
                        if (minH > y) { minH = y; }
                        if (minW > x) { minW = x; }
                    }
                }
            }
            double gamma = 1;
            double bj;

            Vector Vj = new Vector(Math.Round(((double)minW / img.Width), 2), Math.Round(((double)minH / img.Height), 2));
            Vector Wj = new Vector(Math.Round(((double)maxW / img.Width), 2), Math.Round(((double)maxH / img.Height), 2));

            double fun1, fun2, fun3, fun4;

            Vector f_ramp_function_1 = new Vector();
            Vector f_ramp_function_2 = new Vector();

            csv1.AppendLine(string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};{14}", "x_hArrayInput[o].X", "x_hArrayInput[o].Y", "Vj.X", "Vj.Y", "Wj.X", "Wj.Y", "f_ramp_function_1.X", "f_ramp_function_1.Y", "f_ramp_function_2.X", "f_ramp_function_2.Y", "fun1", "fun2", "fun3", "fun4", "bj"));

            for (int o = 0; o < x_hArrayInput.Count; o++)

            {
                f_ramp_function_1.X = x_hArrayInput[o].X - Wj.X;
                f_ramp_function_1.Y = Vj.X - x_hArrayInput[o].X;

                f_ramp_function_2.X = x_hArrayInput[o].Y - Wj.Y;
                f_ramp_function_2.Y = Vj.Y - x_hArrayInput[o].Y;


                //FOR 1-ST FUNCTION
                if (f_ramp_function_1.X > 1)
                {
                    fun1 = 1;
                }
                else if(0 <= f_ramp_function_1.X && f_ramp_function_1.X <= 1)
                {
                    fun1 = f_ramp_function_1.X;
                }
                else
                {
                    fun1 = 0;
                }
                if (f_ramp_function_1.Y > 1)
                {
                    fun2 = 1;
                }
                else if (0 <= f_ramp_function_1.Y && f_ramp_function_1.Y <= 1)
                {
                    fun2 = f_ramp_function_1.Y;
                }
                else
                {
                    fun2 = 0;
                }
                //END 1-ST FUNCTION
                //FOR 2-ND FUNCTION
                if (f_ramp_function_2.X > 1)
                {
                    fun3 = 1;
                }
                else if (0 <= f_ramp_function_2.X && f_ramp_function_2.X <= 1)
                {
                    fun3 = f_ramp_function_2.X;
                }
                else
                {
                    fun3 = 0;
                }
                if (f_ramp_function_2.Y > 1)
                {
                    fun4 = 1;
                }
                else if (0 <= f_ramp_function_2.Y && f_ramp_function_2.Y <= 1)
                {
                    fun4 = f_ramp_function_2.Y;
                }
                else
                {
                    fun4 = 0;
                }
                //END 2-ND FUNCTION

                bj = Math.Min((Math.Min((1-fun1),(1-fun2))),(Math.Min((1-fun3),(1-fun4))));



                csv1.AppendLine(string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9}", x_hArrayInput[o], Vj, Wj, f_ramp_function_1, f_ramp_function_2, fun1, fun2, fun3, fun4, bj));
            }

            //test hyperbox
            for (int q = minW; q <= maxW; q++)
            {
                img.SetPixel(q, minH, Color.Black);
                img.SetPixel(q, maxH, Color.Black);
                //img.SetPixel(q, minH - 1, Color.Black);



            }
            for (int w = minH; w <= maxH; w++)
            {
                img.SetPixel(minW, w, Color.Black);
                img.SetPixel(maxW, w, Color.Black);
                //img.SetPixel(minW - 1, w, Color.Black);


            }

            File.AppendAllText(filepath1, csv1.ToString());
            File.AppendAllText(filepath, csv.ToString());
            img.Save(filepath2);

            //System.GC.Collect();
        }

    }
}

标签: c#algorithmobject-detection

解决方案


推荐阅读