static void Main(string[] args) { SvmModelBuilder modelBuilder = new SvmModelBuilder(); string modelFileName = "model.svm"; if (!modelBuilder.LoadFromFile(modelFileName)) { // first time usage, train from feature file string problemFile = @"D:\UW\2012 Autumn\CSE 481\Kinect Capstone\WindowsFormsApplication1\WindowsFormsApplication1\rgbdfea_depth_first2.mat"; Console.WriteLine("Training new model from {0}.", problemFile); modelBuilder.TrainModel(problemFile); Console.WriteLine("Trainning finished. Saving Model as {0}", modelFileName); modelBuilder.SaveModel(modelFileName); } Console.WriteLine("Model Ready."); ImageFeature imgFeature = new ImageFeature(); short[,] imagePixels = new Png16Reader().Read("../../sampleImages/good/good_1/good_101_depthcrop.png"); double[] vector = imgFeature.GenerateFeature(imagePixels); double category = svm.svm_predict(modelBuilder.GetModel(), MatrixUtil.DoubleToSvmNode(vector)); Console.WriteLine(category); Console.ReadKey(); }
public ImageFeature GetImageFeatureByDataStream(DataStream dataStream) { ImageFeature feature = (ImageFeature)this.Features.Find(x => (x is ImageFeature) && (x.DataStream == dataStream)); // add feature is needed if (feature == null) { if (dataStream is ImageDataStream) { feature = new ImageFeature(dataStream as ImageDataStream); } else if (dataStream is VideoDataStream) { feature = new ImageFeature(dataStream as VideoDataStream); } else { throw new NotImplementedException(); } this.Features.Add(feature); } return(feature); }
private void frameBox_Paint(object sender, PaintEventArgs e) { int markerSize = 5; // put digitized points on the images if (CurrentHotFrame != null) { Feature features = CurrentHotFrame.Features.Find(x => x.DataStream == dataStream); if (features != null) { ImageFeature imageFeature = features as ImageFeature; using (Pen pen = new Pen(new SolidBrush(Color.Red), 1)) { System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 12); System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(); foreach (ImagePoint pt in imageFeature.Points) { Point pti = frameBox.GetOffsetPoint(pt.GetPoint()); //e.Graphics.DrawLine(pen, new Point(pti.X, pti.Y), new Point(pti.X+10, pti.Y)); e.Graphics.DrawLine(pen, pti.X - markerSize, pti.Y, pti.X + markerSize, pti.Y); e.Graphics.DrawLine(pen, pti.X, pti.Y - markerSize, pti.X, pti.Y + markerSize); e.Graphics.DrawString(pt.ID.ToString(), drawFont, drawBrush, pti.X + markerSize / 2, pti.Y + markerSize / 2, drawFormat); } } } } }
private void frameBox_MouseClick(object sender, MouseEventArgs e) { if (this.windowState == StreamWindowState.DigitizePoints) { if (e.Button == MouseButtons.Left) // add point { if (CurrentHotFrame == null) { return; } ImageFeature imageFeature = (ImageFeature)CurrentHotFrame.GetImageFeatureByDataStream(dataStream); Point pt = frameBox.PointToImage(e.X, e.Y); ImagePoint imagePoint = new ImagePoint(++lastId, pt.X, pt.Y); PropertiesWnd wnd = new PropertiesWnd(imagePoint); if (wnd.ShowDialog() == DialogResult.OK) { ImagePoint pti = wnd.Object as ImagePoint; lastId = pti.ID; imageFeature.Points.Add(pti); } frameBox.Refresh(); } if (e.Button == MouseButtons.Right) // remove point { ImageFeature imageFeature = (ImageFeature)CurrentHotFrame.GetImageFeatureByDataStream(dataStream); Point pt = frameBox.PointToImage(e.X, e.Y); /*double minVal = imageFeature.Points.Min(x => Math.Pow(x.X - pt.X, 2) + Math.Pow(x.Y - pt.Y, 2)); * int minIndex = Array.IndexOf(imageFeature.Points, minVal);*/ ImagePoint minPt = null; double minR = Double.MaxValue; foreach (ImagePoint pti in imageFeature.Points) { double r = Math.Pow(pti.X - pt.X, 2) + Math.Pow(pti.Y - pt.Y, 2); if (r < minR) { minPt = pti; minR = r; } } if (minPt != null) { PropertiesWnd wnd = new PropertiesWnd(minPt); if ((wnd.ShowDialog() == DialogResult.OK) && (wnd.IsDeleteObject)) { imageFeature.Points.Remove(minPt); } } frameBox.Refresh(); } } }
public void SetHotFrame(HotFrame frame) { SetFrameByTime(frame.Timestamp); CurrentHotFrame = frame; frameBox.Refresh(); ImageFeature imageFeature = frame.GetImageFeatureByDataStream(dataStream); imageFeature.Image = frameBox.Image; imageFeature.TimeStamp = currentDataLine.TimeStamp; }
private void steTest(int d, ImageFeature<float>[] item) { for (int i = 0; i < item.Count(); i++) { Testing_data.Add(new KeyValuePair<int, float[]>(d, item[i].Descriptor)); } }
public static void ExportHotFrame(String rootFolder, String annotationName, HotFrame frame, MatlabExporterOptions options = MatlabExporterOptions.Overwrite, ILogger logger = null, CancellationToken cancelletionToken = default(CancellationToken)) { // delete features /*List<Feature> newFeatureSet = new List<Feature>(); * foreach (Feature feature in frame.Features) * { * if (feature.DataStream != null) newFeatureSet.Add(feature); * } * frame.Features = newFeatureSet;*/ String folderName = rootFolder + "\\frame_" + annotationName; String matFilePath = folderName + "\\frame_" + annotationName + ".mat"; Directory.CreateDirectory(folderName); logger?.WriteLineInfo(""); logger?.WriteLineInfo("Convert frame to Matlab MAT format and export images!"); logger?.WriteLineInfo("Export folder: " + folderName); logger?.WriteLineInfo("MAT file location: " + matFilePath); List <MLArray> mlList = new List <MLArray>(); Dictionary <DataStream, List <VeloFeature> > velodyneData = new Dictionary <DataStream, List <VeloFeature> >(); if ((options == MatlabExporterOptions.Append) && (File.Exists(matFilePath))) { MatFileReader matReader = new MatFileReader(matFilePath); mlList = matReader.Data; } int i = 0; foreach (Feature feature in frame.Features) { logger?.WriteLineInfo("Exporting: " + feature.DataStream.ShortName); MLStructure structFeatures = new MLStructure(feature.DataStream.ShortName, new int[] { 1, 1 }); structFeatures["Name", 0] = new MLChar(null, feature.DataStream.Name); structFeatures["ShortName", 0] = new MLChar(null, feature.DataStream.ShortName); if (cancelletionToken.IsCancellationRequested == true) { logger?.WriteLineWarning("Export cancelled!"); return; } if (feature is GPSFeature) { var gpsFeature = feature as GPSFeature; structFeatures["Lat", 0] = new MLDouble("", new double[] { gpsFeature.Position.Lat }, 1); structFeatures["Lon", 0] = new MLDouble("", new double[] { gpsFeature.Position.Lon }, 1); structFeatures["Height", 0] = new MLDouble("", new double[] { gpsFeature.Position.Height }, 1); structFeatures["Quality", 0] = new MLDouble("", new double[] { gpsFeature.Position.Quality }, 1); mlList.Add(structFeatures); } else if (feature is ImageFeature) { ImageFeature imageFeature = feature as ImageFeature; structFeatures["Timestamp", 0] = new MLDouble("", new double[] { Utils.ConvertToUnixTimestamp(imageFeature.TimeStamp) }, 1); structFeatures["TimestampC", 0] = ConvertDateTimeToMLDouble(imageFeature.TimeStamp); double[][] points = new double[imageFeature.Points.Count][]; int k = 0; foreach (ImagePoint pt in ((ImageFeature)feature).Points) { points[k] = new double[3]; points[k][0] = pt.ID; points[k][1] = pt.X; points[k][2] = pt.Y; k++; } if (k != 0) { MLDouble arrayPoints = new MLDouble("Points", points); structFeatures["Points", 0] = arrayPoints; } // save images if (imageFeature.Image != null) { using (Bitmap img = new Bitmap(imageFeature.Image)) { img.Save(folderName + "\\frame_" + annotationName + "_" + feature.DataStream.ShortName + ".bmp"); } // you can export the images here, but it's very slow and the code also needs to be tested! /*using (Bitmap img = new Bitmap(imageFeature.Image)) * { * * int[] dims = new int[] { img.Width, img.Height, 3 }; * MLDouble arrImg = new MLDouble("Image", dims); * for (int i = 0; i < img.Width; i++) * { * for (int j = 0; j < img.Height; j++) * { * Color col = img.GetPixel(i, j); * arrImg.Set(col.R, i, j + img.Height * 0); * arrImg.Set(col.G, i, j + img.Height * 1); * arrImg.Set(col.B, i, j + img.Height * 2); * } * } * * structFeatures["Image", 0] = arrImg; * }*/ } mlList.Add(structFeatures); } else if (feature is VeloFeature) { VeloFeature veloFeature = feature as VeloFeature; // if key does not exist create one if (!velodyneData.ContainsKey(veloFeature.DataStream)) { velodyneData.Add(veloFeature.DataStream, new List <VeloFeature>()); } velodyneData[veloFeature.DataStream].Add(veloFeature); } i++; logger?.WriteProgress((double)i / (double)frame.Features.Count() * 100.0); } // ok, now finalize velodyne data foreach (KeyValuePair <DataStream, List <VeloFeature> > entry in velodyneData) { if (cancelletionToken.IsCancellationRequested == true) { logger?.WriteLineWarning("Export cancelled!"); return; } MLStructure structFeatures = new MLStructure(entry.Key.ShortName, new int[] { 1, 1 }); structFeatures["Name", 0] = new MLChar(null, entry.Key.Name); structFeatures["ShortName", 0] = new MLChar(null, entry.Key.ShortName); structFeatures["TimeStamp", 0] = new MLDouble("", new double[] { Utils.ConvertToUnixTimestamp(frame.Timestamp) }, 1); // get number of points long n_points = 0; foreach (VeloFeature feature in entry.Value) { n_points += feature.Points.Count(); } if (n_points == 0) { logger?.WriteLineWarning(entry.Key.ShortName + ": No features!"); continue; } // populate points double[][] points = new double[n_points][]; int k = 0; foreach (VeloFeature feature in entry.Value) { foreach (VelodynePoint pt in feature.Points) { points[k] = new double[9]; points[k][0] = feature.ID; points[k][1] = pt.X; points[k][2] = pt.Y; points[k][3] = pt.Z; points[k][4] = Convert.ToDouble(pt.Intensity); points[k][5] = pt.Distance; points[k][6] = pt.Hz; points[k][7] = pt.Vz; points[k][8] = pt.InternalTime; k++; } } MLDouble arrayPoints = new MLDouble("Points", points); structFeatures["Points", 0] = arrayPoints; mlList.Add(structFeatures); } try { logger?.WriteLineInfo("Saving mat file..."); MatFileWriter mfw = new MatFileWriter(matFilePath, mlList, true); } catch (Exception ex) { logger?.WriteLineError("Error occured: " + ex.ToString()); return; } logger?.WriteProgress(100.0); logger?.WriteLineInfo("Done."); }
public ImageFeature Post(ImageFeature imageFeature) { return(imageFeature); }
public ImageFeature Get([FromUri] ImageFeature imageFeature) { return(imageFeature); }