Пример #1
0
        public static bool IsT1G1End()
        {
            if (!FinalSign)
            {
                return(false);
            }
            ColorDetectResult result = Cv.DetectColor(new Image <Rgb, byte>(TcpIpFileManager.GetInstance().FilePath));

            if (result.Result > 180)
            {
                while (true)
                {
                    FormMain.GetInstance().SampleThreadFunc();
                    Image <Rgb, Byte> rawImage   = new Image <Rgb, byte>((Bitmap)TcpIpFileManager.GetInstance().NowImage);
                    double[]          threshold1 = new double[]
                    {
                        200
                    };
                    double[] threshold2 = new double[]
                    {
                        100, 200, 300
                    };
                    CannyTextureAnalysisResult textureAnalysisResult = Cv.AutoCannyTextureAnalysis(rawImage, threshold1, threshold2, 0);
                    TcpIpFileManager.GetInstance().IsFileFresh       = false;
                    float threshold = 0.05f;
                    if (Math.Abs(textureAnalysisResult.Diff - 0.15) < threshold)
                    {
                        return(true);
                    }
                    else
                    {
                        if (textureAnalysisResult.Diff > threshold)
                        {
                            Arduino.GetInstance().Send(MotorDirection.Forward, 80, MotorDirection.Backward, 80, 300);
                        }
                        else if (textureAnalysisResult.Diff < -threshold)
                        {
                            Arduino.GetInstance().Send(MotorDirection.Backward, 80, MotorDirection.Forward, 80, 300);
                        }
                    }
                    while (StateManager.ArduinoState.IsBusy)
                    {
                        Thread.Sleep(100);
                    }
                }
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Suggest value sets are threshold1=[200] threshold2=[200,300,400]
        /// </summary>
        /// <param name="argImage"></param>
        /// <param name="threshold1"></param>
        /// <param name="threshold2"></param>
        /// <param name="factorBetweenMinAndMax"></param>
        /// <returns></returns>
        public static CannyTextureAnalysisResult AutoCannyTextureAnalysis(Image <Rgb, Byte> argImage, double[] threshold1, double[] threshold2, int factorBetweenMinAndMax = 3)
        {
            Image <Gray, Byte>         cannyImg = new Image <Gray, byte>(argImage.Size);
            CannyTextureAnalysisResult bestCannyTextureAnalysisResult = null;
            double bestCannyTextureAnalysisResultValue = 0;
            bool   FirstSign = true;

            foreach (var singleThresholdValue1 in threshold1)
            {
                foreach (var singleThresholdValue2 in threshold2)
                {
                    cannyImg = argImage.Canny(singleThresholdValue1, singleThresholdValue2);
                    CannyTextureAnalysisResult tempCannyTextureAnalysisResult = new CannyTextureAnalysisResult(cannyImg, string.Format("Threshold1/2 = {0:D} {1:D}", (int)singleThresholdValue1, (int)singleThresholdValue2));
                    float sum = 0, min = tempCannyTextureAnalysisResult.Data[0], max = min;
                    foreach (var i in tempCannyTextureAnalysisResult.Data)
                    {
                        sum += i;
                        if (i > max)
                        {
                            max = i;
                        }
                        if (i < min)
                        {
                            min = i;
                        }
                    }
                    double ave = (double)sum / tempCannyTextureAnalysisResult.Data.Length;
                    if (FirstSign)
                    {
                        bestCannyTextureAnalysisResultValue = max / ave;
                        bestCannyTextureAnalysisResult      = tempCannyTextureAnalysisResult;
                        FirstSign = false;
                    }
                    if (max / ave > factorBetweenMinAndMax && factorBetweenMinAndMax != 0)
                    {
                        return(tempCannyTextureAnalysisResult);
                    }
                    if (max / ave > bestCannyTextureAnalysisResultValue)
                    {
                        bestCannyTextureAnalysisResultValue = max / ave;
                        bestCannyTextureAnalysisResult      = tempCannyTextureAnalysisResult;
                    }
                }
            }

            if (bestCannyTextureAnalysisResult == null)
            {
                throw new LogicErrorException();
            }
            return(bestCannyTextureAnalysisResult);
        }
Пример #3
0
        public static void T1G1()
        {
            Image <Rgb, Byte> rawImage = new Image <Rgb, byte>((Bitmap)TcpIpFileManager.GetInstance().NowImage);

            double[] threshold1 = new double[]
            {
                200
            };
            double[] threshold2 = new double[]
            {
                100, 200, 300
            };

            CannyTextureAnalysisResult textureAnalysisResult = Cv.AutoCannyTextureAnalysis(rawImage, threshold1, threshold2, 0);

            //[obsolote] Detect lines and then remove them from final image.
            //DetectLineResult lineResult = Cv.DetectLine(textureAnalysisResult.Img);
            //Image<Gray, Byte> outputImage = textureAnalysisResult.Img;
            //foreach (LineSegment2D line in lineResult.Line)
            //{
            //	outputImage.Draw(line, new Gray(0), 2);
            //}
            //CannyTextureAnalysisResult reduceLinesResult = new CannyTextureAnalysisResult(rawImage, "Reduce the lines");

            float diff = textureAnalysisResult.Diff;
            LinearRegressionResult lrResult = Cv.CalculateLinearRegression(textureAnalysisResult.Img);

            FormMain.GetInstance().WriteToConsole("Diff= " + diff + "   Slope= " + lrResult.Slope + "   Count= " + lrResult.Count + " CounterFinal= " + Counter);
            FormMain.GetInstance().WriteToPicture1(textureAnalysisResult.Img);

            //[obsolote] Generate nine patch graph.
            //NinePatchResult patchResult = Cv.CalculateNinePatch(textureAnalysisResult.Img);
            //int height = FormMain.GetInstance().GetPicture2Size().Height;
            //int width = FormMain.GetInstance().GetPicture2Size().Width;
            //byte[,,] patchImageData = new byte[height, width, 1];
            //for (var index0 = 0; index0 < 3; index0++)
            //{
            //	for (var index1 = 0; index1 < 3; index1++)
            //	{
            //		if (patchResult.Patch[index0,index1])
            //		{
            //			for (var y = height/3*index1; y < height/3*(index1+1); y++)
            //			{
            //				for (var x = width/3*index0; x < width/3*(index0+1); x++)
            //				{
            //					patchImageData[y, x, 0] = 255;
            //				}
            //			}
            //		}
            //	}
            //}
            //Image<Gray, Byte> patchImage = new Image<Gray, Byte>(patchImageData);
            //FormMain.GetInstance().WriteToPicture2(patchImage);

            //threshold
            float threshold = 0.05f;

            if (lrResult.Count < 2000)
            {
                Arduino.GetInstance().Send(MotorDirection.Backward, 80, MotorDirection.Backward, 80, 300);
                return;
            }
            if (Math.Abs(diff) > threshold)
            {
                countAcc = 0;
                if (diff > threshold)
                {
                    if (_previousDiff < -threshold)
                    {
                        Arduino.GetInstance().Send(MotorDirection.Forward, 35, MotorDirection.Backward, 35, 300);
                    }
                    else
                    {
                        Arduino.GetInstance().Send(MotorDirection.Forward, 80, MotorDirection.Backward, 100, 300);
                    }
                }
                else if (diff < -threshold)
                {
                    if (_previousDiff > threshold)
                    {
                        Arduino.GetInstance().Send(MotorDirection.Backward, 35, MotorDirection.Forward, 35, 300);
                    }
                    else
                    {
                        Arduino.GetInstance().Send(MotorDirection.Backward, 80, MotorDirection.Forward, 100, 300);
                    }
                }
            }
            else
            {
                if (lrResult.Slope < 0.1)
                {
                    countAcc++;
                    if (countAcc > 2)
                    {
                        if (FinalSign)
                        {
                            Counter++;
                            Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 1000);
                        }
                        else
                        {
                            Counter = Counter + 3;
                            Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 3000);
                        }
                    }
                    else
                    {
                        Counter++;
                        Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 1000);
                    }
                }
                else
                {
                    countAcc = 0;
                    Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 200);
                }
            }
            _previousDiff = diff;
        }