private IplImage FeatureDetector(Bitmap bitmapIn) { double minVal, maxVal; IplImage i1 = BitmapConverter.ToIplImage(bitmapIn); IplImage gray = new IplImage(new OpenCvSharp.CvSize(i1.Size.Width, i1.Size.Height), BitDepth.U8, 1); IplImage blur = new IplImage(new OpenCvSharp.CvSize(i1.Size.Width, i1.Size.Height), BitDepth.U8, 1); IplImage gaussian = new IplImage(new OpenCvSharp.CvSize(i1.Size.Width, i1.Size.Height), BitDepth.U8, 1); IplImage laplace = new IplImage(new OpenCvSharp.CvSize(i1.Size.Width, i1.Size.Height), BitDepth.F32, gaussian.NChannels); IplImage converted = new IplImage(new OpenCvSharp.CvSize(i1.Size.Width, i1.Size.Height), BitDepth.U8, 1); IplImage normalized = new IplImage(new OpenCvSharp.CvSize(i1.Size.Width, i1.Size.Height), BitDepth.U8, 1); IplImage tresh = new IplImage(new OpenCvSharp.CvSize(i1.Size.Width, i1.Size.Height), BitDepth.U8, 1); OpenCvSharp.Cv.CvtColor(i1, gray, ColorConversion.RgbToGray); OpenCvSharp.Cv.Normalize(gray, normalized, 0.0, 255.0, NormType.MinMax); OpenCvSharp.Cv.Smooth(normalized, blur, SmoothType.Median, 5, 5); OpenCvSharp.Cv.Smooth(blur, gaussian, SmoothType.Gaussian, 5, 5); OpenCvSharp.Cv.Laplace(gaussian, laplace, ApertureSize.Size5); laplace.MinMaxLoc(out minVal, out maxVal); laplace.ConvertScale(converted, 255.0 / (2.0 * maxVal), 128); OpenCvSharp.Cv.Threshold(converted, tresh, 150.0, 255.0, ThresholdType.Binary); return tresh; }
private static int bestMatchNum(IplImage source, IplImage[] temp_num, double threshold) { CvSize size = new CvSize( source.GetROI().Width - temp_num[0].Width + 1, source.GetROI().Height - temp_num[0].Height + 1); // 0から9の一致度 double[] max_list = new double[10]; // 0-9の画像とマッチング Parallel.For(0, 10, i => { IplImage result = new IplImage(size, BitDepth.F32, 1); source.MatchTemplate(temp_num[i], result, MatchTemplateMethod.CCoeffNormed); double min; CvPoint min_point, max_point; result.MinMaxLoc(out min, out max_list[i], out min_point, out max_point); }); // 一致度が最大のものを探す double best = 0.0; int best_index = -1; for (int i = 0; i < 10; i++) { double tmp = max_list[i]; if (tmp > best) { best = tmp; best_index = i; } } // しきい値以下の精度であれば切り捨て if (best < threshold) return -1; return best_index; }
static void Main(string[] args) { CvFont font; Cv.InitFont(out font, FontFace.HersheyComplex, 0.5, 0.5);//.7 .7 file.WriteLine("Match Time Per Frame"); //*******************INPUT PATH SECTION using (CvCapture capture = CvCapture.FromFile("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\[ch03]103014_094500__103014_095700decompress-1_WMV V9.wmv"))//Works with wmv { if (capture == null) { Console.WriteLine("ERROR! FILE NOT FOUND!"); } while (true) { counter++; IplImage frame = null; int c; frame = capture.QueryFrame(); //***********Create the video the same size as the frame if (counter == 1) { double w = frame.Width, h = frame.Height;//This is the width and height for recording. video = Cv.CreateVideoWriter("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\Powerbox Video.avi", "XVID", 30, Cv.Size((int)w, (int)h)); //15 fps. The writer for the avi file. } //****************************************** if (frame == null) break; if (image == null) { image = Cv.CreateImage(frame.Size, BitDepth.U8, 3); colorImage = Cv.CreateImage(image.Size, BitDepth.U8, 3); gray = Cv.CreateImage(frame.Size, BitDepth.U8, 1); prevGray = Cv.CreateImage(frame.Size, BitDepth.U8, 1); } if (counter == 200)//Saves a picture here for power box { picture = Cv.CreateImage(frame.Size, BitDepth.U8, 3); Cv.Copy(frame, picture); picture.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\PictureforPowerboxTemplate.bmp");//Save Images for Templates here Cv.ShowImage("Picture", picture); } Cv.Copy(frame, image); Cv.Copy(image, colorImage); Cv.CvtColor(image, gray, ColorConversion.BgrToGray); Cv.SWAP(ref prevGray, ref gray); //******************************Template Matching template = new IplImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\" + currentTemplate); maxValueHolder = maxValue; if (counter >= 200 && counter != 1080)//counter >= 520 { if (xholder < 0 || yholder < 0) break; if (counter == 1116)//Save an image to use as a template 1260 image.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\NEWPowerboxTemplate2.bmp"); predictImage = image.GetSubImage(rect2); destSize = new CvSize(predictImage.Width - template.Width + 1, predictImage.Height - template.Height + 1); destImg = new IplImage(destSize, BitDepth.F32, 1); //Measuring time for template matching. watch.Start(); predictImage.MatchTemplate(template, destImg, MatchTemplateMethod.CCoeffNormed); watch.Stop(); Console.WriteLine("Frame " + counter + " took " + watch.ElapsedMilliseconds + "ms"); file.WriteLine("Frame {0}: " + watch.ElapsedMilliseconds + "ms", counter); watch.Reset(); //END SECTION destImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc); if (currentX > 220 && currentX < 370 && currentY > 170 && currentY < 320) { perimeter = new CvRect(220, 170, 150, 150); securityCounter++; Console.WriteLine(securityCounter); if (securityCounter > 0 && securityCounter <=300) { Cv.PutText(colorImage, "Target in Safe Zone...", new CvPoint(200, 150), font, CvColor.Green); colorImage.Rectangle(perimeter, CvColor.Green); } else if (securityCounter > 300 && securityCounter <= 600) { Cv.PutText(colorImage, "Possible Threat...", new CvPoint(200, 150), font, CvColor.Yellow); colorImage.Rectangle(perimeter, CvColor.Yellow); } else if (securityCounter > 600) { Cv.PutText(colorImage, "Suspicious Target Detected!!!", new CvPoint(200, 150), font, CvColor.Red); colorImage.Rectangle(perimeter, CvColor.Red); status = "DANGER!"; markTarget = CvColor.Red; } } Cv.PutText(colorImage, "(" + (xholder + maxLoc.X).ToString() + ", " +(yholder + maxLoc.Y).ToString() + ") " + status, new CvPoint(xholder + maxLoc.X, (yholder + maxLoc.Y) - 5), font, markTarget); colorImage.Rectangle(xholder + maxLoc.X, yholder + maxLoc.Y, (xholder + maxLoc.X) + template.Width, (yholder + maxLoc.Y) + template.Height, markTarget); picture.Rectangle(xholder + maxLoc.X + template.Width / 2, yholder + maxLoc.X + template.Height / 2, xholder + maxLoc.X + template.Width / 2 + 1, yholder + maxLoc.X + template.Height / 2 + 1, CvColor.Red);//Places dot at the center currentX = xholder + maxLoc.X + template.Width / 2; currentY = yholder + maxLoc.X + template.Height / 2; if (previousX > 0 && previousY > 0) picture.Line(previousX, previousY, currentX, currentY, CvColor.Red); previousX = xholder + maxLoc.X + template.Width / 2; previousY = yholder + maxLoc.X + template.Height / 2; rect2 = new CvRect((xholder + maxLoc.X) - 5, (yholder + maxLoc.Y) - 5, template.Width + 10, template.Height +10);//5 5 10 10 Console.WriteLine("{0},{1}", xholder, yholder); xholder = rect2.X; yholder = rect2.Y; } else { destSize = new CvSize(image.Width - template.Width + 1, image.Height - template.Height + 1); destImg = new IplImage(destSize, BitDepth.F32, 1); //Measuring time watch.Start(); colorImage.MatchTemplate(template, destImg, MatchTemplateMethod.CCoeffNormed); watch.Stop(); Console.WriteLine("Frame " + counter + " took " + watch.ElapsedMilliseconds + "ms"); file.WriteLine("Frame {0}: " + watch.ElapsedMilliseconds + "ms", counter); watch.Reset(); //End Section destImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc); Cv.PutText(colorImage, "(" + maxLoc.X.ToString() + "," + maxLoc.Y.ToString() + ") " + status, new CvPoint(maxLoc.X, maxLoc.Y - 5), font, CvColor.Green); colorImage.Rectangle(maxLoc.X, maxLoc.Y, maxLoc.X + template.Width, maxLoc.Y + template.Height,markTarget); rect2 = new CvRect(maxLoc.X - 10, maxLoc.Y - 10, template.Width + 20, template.Height + 20);//Original prediction size here xholder = rect2.X; yholder = rect2.Y; } if (counter == 403)//Saves a picture here { colorImage.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\KC515.png"); } if (counter == 650)//Saves a picture here { colorImage.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\KC516.png"); } if (counter == 1000)//Saves a picture here { colorImage.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\KC517.png"); } rect = new CvRect(maxLoc.X, maxLoc.Y, template.Width, template.Height); newTemplate = image.GetSubImage(rect); Cv.WaitKey(2); if (maxValue >= bestMatch) { Console.WriteLine("NEW BEST MATCH"); bestMatch = maxValue; //**********************IMPORTANT SAVES!!!! newTemplate.SaveImage("C://OpenCvSharp//FallProgram7Camera3PowerBox//NEWPowerboxTemplate.bmp"); //********************** if(counter > 1050) currentTemplate = "PowerboxTemplate2.bmp"; else currentTemplate = "PowerboxTemplate.bmp"; } else { //currentTemplate = "S2ABTemplate0.bmp"; if (counter > 1050) currentTemplate = "PowerboxTemplate2.bmp"; else currentTemplate = "PowerboxTemplate.bmp"; } //************************************************* if (counter >= 135) Cv.WriteFrame(video, colorImage);//Writes each frame to an avi file. Cv.ShowImage("color Image", colorImage); //********************IMPORTANT SAVE!!! picture.SaveImage("C:\\OpenCvSharp\\SummerPractice32AVICapture\\Summer Practice 23 Results\\Walking Path.bmp"); //******************** //Cv.ShowImage("Test", image); c = Cv.WaitKey(5); if ((char)c == 27) break; } file.Close(); colorImage.SaveImage("C:\\OpenCvSharp\\SummerPractice32AVICapture\\Summer Practice 23 Results\\Frame.bmp"); Cv.WaitKey(0); Cv.ReleaseVideoWriter(video); Cv.ReleaseCapture(capture); Cv.DestroyWindow("Test"); } }
static void Main(string[] args) { Stopwatch timer = new Stopwatch(); IplImage bestMatch = new IplImage(); CvMat mapMatrix; CvPoint2D32f center; double[] maxArray = new double[8]; int numberOfFiles = 50;//Can change the number of input files if I want double angle = 0.0, scale = 1.0, bestAngle = 0.0; mapMatrix = new CvMat(2, 3, MatrixType.F32C1); while (fileReader < numberOfFiles)//Number of files to read in folder { uavSourceImg = new IplImage("C:\\OpenCvSharp\\SummerPractice13RotateAndScale4DataInfo2\\Testing Different UAV Inputs\\RenameFolder\\Kamien " + fileReader + ".bmp", LoadMode.AnyColor); tempImg = new IplImage("C:\\OpenCvSharp\\SummerPractice1\\SummerDatabase\\BigGoogleTemplate2.jpg", LoadMode.AnyColor);//Big template test CvRect drawRectangle = new CvRect(200, 200, 300, 300); timer.Start(); while (angle < 15.0)//Angle change while loop. Can change if necessary { while (i < 10)//Scaling while loop. Can change this if necessary. { //***********************DECLARATION //tempImg = theBigTemplate.GetSubImage(drawRectangle); CvSize destSize; graySource = new IplImage(uavSourceImg.Size, BitDepth.U8, 1); grayTemp = new IplImage(tempImg.Size, BitDepth.U8, 1); tempDestImg = new IplImage(grayTemp.Size, BitDepth.U8, 1); double minValue, maxValue; CvPoint minLoc, maxLoc; //**********************END DECLARATIONS //**********************CONVERT TO GRAY uavSourceImg.CvtColor(graySource, ColorConversion.BgrToGray); tempImg.CvtColor(grayTemp, ColorConversion.BgrToGray); //**********************END CONVERT TO GRAY //**********************ROTATION center = new CvPoint2D32f(grayTemp.Width * 0.5, grayTemp.Height * 0.5); grayTemp.Copy(tempDestImg); Cv._2DRotationMatrix(center, angle, scale, out mapMatrix); Cv.WarpAffine(grayTemp, tempDestImg, mapMatrix, Interpolation.FillOutliers, Cv.ScalarAll(255)); //**********************END ROTATION theRotatedSubTemp = tempDestImg.GetSubImage(drawRectangle); //**********************RESIZE PART CvSize size = new CvSize(graySource.Width / i, graySource.Height / i);//Manipulate the source image size CvSize size2 = new CvSize(theRotatedSubTemp.Width / i, theRotatedSubTemp.Height / i);//theRotatedSubTemp test graySourceHolder = new IplImage(size, BitDepth.U8, 1);//1 for grayholder ORIGINAL grayTempHolder = new IplImage(size2, BitDepth.U8, 1); graySource.Resize(graySourceHolder);//ORIGINAL(resize the grayscale source image before template matching) theRotatedSubTemp.Resize(grayTempHolder);//TEST theRotatedSubTemp //*********************END RESIZE PART //*********************TEMPLATE MATCHING PART destSize = new CvSize(graySourceHolder.Width - grayTempHolder.Width + 1, graySourceHolder.Height - grayTempHolder.Height + 1); //TEST RESIZE BEFORE WITH RESIZED TEMPLATE resizeDestImg = new IplImage(destSize, BitDepth.F32, 1); graySourceHolder.MatchTemplate(grayTempHolder, resizeDestImg, MatchTemplateMethod.CCoeffNormed); resizeDestImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc); graySourceHolder.Rectangle(maxLoc.X, maxLoc.Y, maxLoc.X + grayTempHolder.Width, maxLoc.Y + grayTempHolder.Height, CvColor.Red, 3);//Testing resize before with resized template //********************END TEMPLATE MATCHING PART Console.WriteLine("Divided by {0}, there was a {1} percent match", i, maxValue); if (maxValue > theBestMax) { theBestMax = maxValue; bestMatch = graySourceHolder.Clone(); bestAngle = angle; iHolder = i; } Cv.NamedWindow("Rotating Template", WindowMode.AutoSize); Cv.NamedWindow("Sub Template", WindowMode.AutoSize); Cv.ShowImage("Rotating Template", tempDestImg); Cv.ShowImage("Sub Template", theRotatedSubTemp); //Cv.WaitKey(0); Cv.WaitKey(1); i++; Cv.ReleaseData(graySourceHolder); Cv.ReleaseData(grayTempHolder); Cv.ReleaseData(tempDestImg); Cv.ReleaseData(graySource); Cv.ReleaseData(grayTemp); Cv.ReleaseData(resizeDestImg); Cv.ReleaseData(theRotatedSubTemp);//Added for big template test }//End 3rd Inner while loop angle += 1.5; //This changes the angle tilt of the template. Can change if necessary. i = 1;//This changes the scale divider. Can change if necessary. Console.WriteLine("***************************************SHIFTING TEMPLATE\n"); //Cv.DestroyAllWindows(); }//End 2nd Inner while loop //***************SHOWING RESULT INFO timer.Stop(); //This section writes the results to a text file using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\OpenCvSharp\\SummerPractice12RotateAndScale3DataInfo\\Data Info Folder\\Best Match Data.txt", true)) { file.WriteLine("{0}\t{1}\t\t{2}\t1/{3}", timer.ElapsedMilliseconds, theBestMax.ToString("#.###"), bestAngle, iHolder); } Console.WriteLine("---------RESULTS"); //*************END SHOWING RESULT INFO //*************IMPORTANT SAVES bestMatch.SaveImage("C:\\OpenCvSharp\\SummerPractice12RotateAndScale3DataInfo\\Data Info Folder\\Best Match" + fileReader.ToString() + ".jpg"); Console.WriteLine("ITEM SAVED IN DATA INFO FOLDER!"); //*************END IMPORTANT SAVES Cv.WaitKey(1); fileReader++; theBestMax = double.MinValue; iHolder = int.MinValue; bestAngle = double.MinValue; angle = 0.0; i = 1; Cv.ReleaseData(bestMatch);//TEST if(fileReader < numberOfFiles) Console.WriteLine("Switching Input..."); }//End while loop }
static void Main(string[] args) { Stopwatch timer = new Stopwatch(),timer2 = new Stopwatch(); IplImage topLayer, smallerTop, uavLayer, smallerUAV, uavTemplate, destImg; CvPoint minLoc, maxLoc, brightMinLoc, brightMaxLoc; double minValue, maxValue, bestMatch = 0; CvSize makeTopSmaller, makeUAVSmaller, destSize;//scale originalSize x 12 //CvRect uavRect; CvMat mapMatrix; CvPoint2D32f center; StreamWriter writer2 = new StreamWriter (@"C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\Top Layer Measure.txt"); double angle = 0.0, scale = 1.0, topScale = 0.09, bestScale = 0, bestAngle = 0, brightMinVal = 0, brightMaxVal = 0; topLayer = new IplImage("C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\TrueSizedMosaic.bmp", LoadMode.GrayScale); uavLayer = new IplImage("C:\\Summer14\\EO_Run07_s7_301_16_00_51_278385\\Oct-28-12-00-57_523977-Frame-30.bmp", LoadMode.GrayScale); //************************Brighten input image section Cv.MinMaxLoc(topLayer, out brightMinVal, out brightMaxVal, out brightMinLoc, out brightMaxLoc); double brightScale = 255 / 70;//90 smaller denominator makes it brighter double brightShift = -brightMinVal * brightScale; Cv.ConvertScale(topLayer, topLayer, brightScale, brightShift); timer.Start(); //************************End brighten section while (topScale > 0.06) { /*****************Manipulate the size of top layer and uav*/ makeTopSmaller = new CvSize((int)(topLayer.Width * topScale), (int)(topLayer.Height * topScale)); //make top layer original size x0.07 makeUAVSmaller = new CvSize((int)(uavLayer.Width * .08), (int)(topLayer.Height * .03)); //*************************************** while (angle > -361.0) { //********************************* /*PART 1 This section makes the top layer and uav layer smaller*/ smallerTop = new IplImage(makeTopSmaller, topLayer.Depth, topLayer.NChannels);//Make the top layer smaller smallerUAV = new IplImage(makeUAVSmaller, uavLayer.Depth, uavLayer.NChannels);//Make the uav layer smaller topLayer.Resize(smallerTop);//Resize the top Layer uavLayer.Resize(smallerUAV);//Resize the uav Layer //*******************END 1 //******************Part 2 ROTATING SECTION mapMatrix = new CvMat(2, 3, MatrixType.F32C1); center = new CvPoint2D32f(smallerUAV.Width * 0.5, smallerUAV.Height * 0.5); Cv._2DRotationMatrix(center, angle, scale, out mapMatrix); Cv.WarpAffine(smallerUAV, smallerUAV, mapMatrix, Interpolation.Linear, Cv.ScalarAll(255)); angle -= 0.1; //***********************END 2 //***********************PART 3 This section handles getting the template uavRect = new CvRect((smallerUAV.Width / 2) - 75, (smallerUAV.Height / 2) - 75, 150, 150);//200x200 smallerUAV.Rectangle(uavRect, CvColor.Red, 2); uavTemplate = smallerUAV.GetSubImage(uavRect); //**********************END 3 //**********************PART 4 This is where template matching begins destSize = new CvSize(smallerTop.Width - uavTemplate.Width + 1, smallerTop.Height - uavTemplate.Height + 1); destImg = new IplImage(destSize, BitDepth.F32, 1); //Recording the time on each step timer2.Start(); smallerTop.MatchTemplate(uavTemplate, destImg, MatchTemplateMethod.CCoeffNormed); timer2.Stop(); writer2.WriteLine("{0} {1} {2}",topScale,counter, timer2.ElapsedMilliseconds);//scale multiplier,scale count, time timer2.Reset(); counter++; //End section destImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc); smallerTop.Rectangle(maxLoc.X, maxLoc.Y, maxLoc.X + uavTemplate.Width, maxLoc.Y + uavTemplate.Height, CvColor.Red, 3); //*******Records best match if (maxValue > bestMatch) { x = maxLoc.X; y = maxLoc.Y; bestAngle = angle; bestScale = topScale; bestMatch = maxValue; bestTopLayer = new IplImage(makeTopSmaller, smallerTop.Depth, smallerTop.NChannels);//decaration for the best match smallerTop.Copy(bestTopLayer); Console.WriteLine("New best match: {0}", bestMatch); //******IMPORTANT SAVE!!! uavTemplate.SaveImage("C:\\OpenCvSharp\\SummerPractice24UAVTrack3\\Used Template.bmp"); //******END IMPORTANT SAVE!!! } //*******End Record Best Match //*******Output test Cv.ShowImage("Top Layer", smallerTop); Cv.ShowImage("smaller uav", smallerUAV); Cv.ShowImage("Template", uavTemplate); Cv.WaitKey(1); //*******End Output test //*****************END 4 Cv.ReleaseData(smallerTop); Cv.ReleaseData(smallerUAV); Cv.ReleaseData(uavTemplate); Cv.ReleaseData(destImg); }//End Inner While loop angle = 0.0; topScale -= 0.01; Console.WriteLine("Scaling top layer down....."); counter = 0; }//End While loop timer.Stop(); using (System.IO.StreamWriter writer = new System.IO.StreamWriter (@"C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\Total Top Measure.txt", true)) { writer.WriteLine ("{0}\t{1}\t\t{2}\t{3}\t{4}", timer.ElapsedMilliseconds, bestMatch.ToString("#.###"), bestAngle.ToString("###.#"), bestScale.ToString(), "(X: " + x.ToString() + ", Y: " + y.ToString() + ")"); } //**********************IMPORTANT SAVE!!! bestTopLayer.SaveImage("C:\\OpenCvSharp\\FallProgram5TopLayerTimeMeasure\\TL Match Image.bmp"); //**********************END IMPORTANT SAVE!!! Cv.ShowImage("The Best Match", bestTopLayer); Cv.WaitKey(0); }