private void borderFollow(ushort j) { cvsim.Source = images[j]; cvsbmp = UtilFn.BitmapImage2Bitmap(images[j]); Bitmap resbmp = UtilFn.ResizeBitmap(cvsbmp, (int)cvsim.Width, (int)(cvsim.Width * images[j].Height / images[j].Width)); Grayscale gfilter = new Grayscale(0.2125, 0.7154, 0.0721); Bitmap grayImage = gfilter.Apply(cvsbmp); BorderFollowing bf = new BorderFollowing(); List <Accord.IntPoint> contour = bf.FindContour(resbmp); imageInfo.Text = contour.Count + ": "; foreach (var c in contour) { Ellipse dot = new Ellipse(); dot.Width = 4; dot.Height = 4; dot.StrokeThickness = 2; dot.Stroke = System.Windows.Media.Brushes.Violet; Canvas.SetTop(dot, c.Y - 1 + (int)((cvs.Height - resbmp.Height) / 2)); Canvas.SetLeft(dot, c.X - 1); imageInfo.Text += "(" + c.X + ", " + c.Y + ");"; cvs.Children.Add(dot); } imagesEdited.Add(UtilFn.rtbToBitmapImage(UtilFn.ExportToPng(null, cvs)).Clone()); cvs.Children.RemoveRange(1, cvs.Children.Count); }
private void faceDetect(ushort j) { cvsbmp = UtilFn.BitmapImage2Bitmap(images[j]); var cascade = new Accord.Vision.Detection.Cascades.FaceHaarCascade(); var detector = new Accord.Vision.Detection.HaarObjectDetector(cascade, minSize: 50, searchMode: Accord.Vision.Detection.ObjectDetectorSearchMode.Average); System.Drawing.Rectangle[] rectangles = detector.ProcessFrame(cvsbmp); List <Bitmap> listBitmap = new List <Bitmap>(); foreach (System.Drawing.Rectangle r in rectangles) { listBitmap.Add(UtilFn.CropImage(cvsbmp, r.X, r.Y, r.Width, r.Height)); } if (rectangles.Count() == 0) { imageInfo.Text = "No faces detected!"; } else { //imagesEdited[j] = (converter.Convert(UtilFn.MergeImages(listBitmap), Type.GetType("BitmapImage"), null, null) as BitmapImage).Clone(); foreach (var x in listBitmap) { imagesEdited.Add((converter.Convert(x, Type.GetType("BitmapImage"), null, null) as BitmapImage).Clone()); } } }
private void edgeDetect(ushort j) { cvsbmp = UtilFn.BitmapImage2Bitmap(images[j]); Grayscale gfilter = new Grayscale(0.2125, 0.7154, 0.0721); Bitmap grayImage = gfilter.Apply(cvsbmp); GaborFilter filter = new GaborFilter(); Bitmap output = filter.Apply(grayImage); imagesEdited.Add(converter.Convert(output, Type.GetType("BitmapImage"), null, null) as BitmapImage); }
private void trainSVMim(object sender, RoutedEventArgs e) { imageInfo.Text = "Training SVMim!"; if (bow == null || imagesEdited.Count == 0) { MessageBox.Show("NO TRAINED BoW MODEL or NO IMAGES LOADED"); return; } Bitmap[] trainIms = new Bitmap[imagesEdited.Count]; ushort z = 0; foreach (BitmapImage b in imagesEdited) { trainIms[z++] = UtilFn.BitmapImage2Bitmap(b); } int[] labels = new int[imagesEdited.Count]; for (z = 0; z < imagesEdited.Count; ++z) { labels[z] = selected[z] ? +1 : -1; } if (bow == null) { MessageBox.Show("NO TRAINED BoW MODEL"); return; } double[][] features = bow.Transform(trainIms); var teacher = new SequentialMinimalOptimization <Linear>() { Complexity = 70 // margin hardness }; svmIm = teacher.Learn(features, labels); imageInfo.Text = "Done"; }
private void PredictImages(object sender, RoutedEventArgs e) { imageInfo.Text = "Predicting"; if (bow == null) { MessageBox.Show("No BoW model!"); return; } if (svmIm == null) { MessageBox.Show("No SVM model!"); return; } Bitmap[] trainIms = new Bitmap[imagesEdited.Count]; ushort z = 0; foreach (BitmapImage b in imagesEdited) { trainIms[z++] = UtilFn.BitmapImage2Bitmap(b); } double[][] features = bow.Transform(trainIms); output = svmIm.Decide(features); if (output != null) { if (output[j] && !cvs.Children.Contains(rectSel2)) { cvs.Children.Add(rectSel2); } else if (!output[j] && cvs.Children.Contains(rectSel2)) { cvs.Children.Remove(rectSel2); } } imageInfo.Text = "Done"; btnCorrect.IsEnabled = true; }
private void cluster(ushort j) { cvsbmp = UtilFn.BitmapImage2Bitmap(images[j]); var imageToArray = new ImageToArray(min: -1, max: +1); var arrayToImage = new ArrayToImage(cvsbmp.Width, cvsbmp.Height, min: -1, max: +1); int kk; double[][] pixels; imageToArray.Convert(cvsbmp, out pixels); try { kk = Int16.Parse(kCluster.Text); } catch (Exception) { return; } if (kk < 1) { return; } KMeans kmeans = new KMeans(k: kk) { Distance = new SquareEuclidean(), Tolerance = 0.05 }; var clusters = kmeans.Learn(pixels); int[] labels = clusters.Decide(pixels); double[][] replaced = pixels.Apply((x, i) => clusters.Centroids[labels[i]]); Bitmap result; arrayToImage.Convert(replaced, out result); imagesEdited.Add(converter.Convert(result, Type.GetType("BitmapImage"), null, null) as BitmapImage); }