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 trainBoW(object sender, RoutedEventArgs e) { //train bow imageInfo.Text = "Training BoW"; WPFFolderBrowserDialog dd = new WPFFolderBrowserDialog(); dd.Title = "Select a folder"; if (dd.ShowDialog() == true) { String[] names = UtilFn.GetFilesFrom(dd.FileName, filtersDir, false); if (names.Length == 0) { MessageBox.Show("NO IMAGES IN SELECTED FOLDER."); } ushort c = 0; List <Bitmap> ims = new List <Bitmap>(); foreach (String fn in names) { if (c++ == 2000) { break; } ims.Add(new Bitmap(fn)); } bow = new BagOfVisualWords(20); // br features bow.Learn(ims.ToArray()); } else { MessageBox.Show("Something went wrong."); } imageInfo.Text = "Done"; }
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); }
private void loadButton(object sender, RoutedEventArgs e) { images = new List <BitmapImage>(); imagesEdited = new List <BitmapImage>(); i = 0; j = 0; if (checkBox.IsChecked == true) { allcurr.IsChecked = false; allcurr.IsEnabled = true; WPFFolderBrowserDialog dd = new WPFFolderBrowserDialog(); dd.Title = "Select a folder"; if (dd.ShowDialog() == true) { String[] names = UtilFn.GetFilesFrom(dd.FileName, filtersDir, false); if (names.Length == 0) { return; } foreach (String fn in names) { images.Add(new BitmapImage(new Uri(fn))); imagesEdited.Add(new BitmapImage(new Uri(fn))); } } else { imageInfo.Text = "Something went wrong."; } } else { allcurr.IsChecked = false; allcurr.IsEnabled = false; Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog(); op.Title = "Select a picture"; op.Filter = filters; if (op.ShowDialog() == true) { images.Add(new BitmapImage(new Uri(op.FileName))); imagesEdited.Add(new BitmapImage(new Uri(op.FileName))); } else { imageInfo.Text = "Something went wrong."; } } if (0 == images.Count) { allcurr.IsChecked = false; allcurr.IsEnabled = false; MessageBox.Show("No images"); return; } cvsim.Source = imagesEdited[i]; cvsim2.Source = images[i]; if (1 == images.Count) { rightOriginal.IsEnabled = false; rightEdited.IsEnabled = false; } else { rightOriginal.IsEnabled = true; rightEdited.IsEnabled = true; } cvs.Width = cvsim.Source.Width; cvs.Height = cvsim.Source.Height; cvs2.Width = cvsim2.Source.Width; cvs2.Height = cvsim2.Source.Height; rectSel = new System.Windows.Shapes.Rectangle { Stroke = new SolidColorBrush(Colors.Orange), StrokeThickness = 4, Width = cvs.Width, Height = cvs.Height }; Canvas.SetLeft(rectSel, 0); Canvas.SetTop(rectSel, 0); rectSel2 = new System.Windows.Shapes.Rectangle { Stroke = new SolidColorBrush(Colors.LimeGreen), StrokeThickness = 4, Width = cvs.Width - 20, Height = cvs.Height - 20 }; Canvas.SetLeft(rectSel2, 10); Canvas.SetTop(rectSel2, 10); if (cvs.Children.Count > 1) { cvs.Children.RemoveRange(1, cvs.Children.Count); } selected = new bool[imagesEdited.Count]; imageInfo.Text = ""; }