예제 #1
0
        static void Main(string[] args)
        {
            IPixelPicker iPixelPicker = new LeastLuminousPixelPicker();

            string[] labeledShapes = Directory.GetFiles("LabeledShapes");
            Console.WriteLine("Loaded " + labeledShapes.Length.ToString() + " labeled shapes.");

            List<HSL> redHSL = new List<HSL>();
            List<HSL> greenHSL = new List<HSL>();
            List<HSL> purpleHSL = new List<HSL>();

            Console.WriteLine("Selecting pixels from shapes...");
            foreach (string labeledShape in labeledShapes)
            {
                string fileName = Path.GetFileNameWithoutExtension(labeledShape);
                string[] information = fileName.Split('-').Take(3).ToArray();
                if (information[0] != "error")
                {
                    string color = information[0].ToLower();
                    string shape = information[1].ToLower();
                    string fill = information[2].ToLower();

                    using (Bitmap bmp = (Bitmap)Bitmap.FromFile(labeledShape))
                    {
                        Random rand = new Random();
                        //for (int c = 0; c < 6; c++)
                        {
                            //BrightnessCorrection brightness = new BrightnessCorrection(rand.Next(-5, 5));
                            //float adjust = 0f; 
                            //if (rand.Next() % 2 == 0)
                            //    adjust = (float)rand.NextDouble() / 8.0f; 
                            //else
                            //    adjust = -(float)rand.NextDouble() / 8.0f;
                            //SaturationCorrection saturation = new SaturationCorrection(adjust); 

                            //brightness.ApplyInPlace(bmp);
                            //saturation.ApplyInPlace(bmp); 

                            FoundColorSpaces colorSpaces = ColorSpaceFinder.Find(bmp);
                            Bitmap corrected = ColorSpaceFinder.FindColorCorrectedForBlob(colorSpaces.OriginalColorSpace);
                            //corrected.Save(@"C:\users\brush\desktop\corrected\" + Guid.NewGuid().ToString() + ".bmp"); 

                            HSL[] pixels = iPixelPicker.Get(corrected, 100);

                            switch (color)
                            {
                                case "red":
                                    redHSL.AddRange(pixels);
                                    break;
                                case "green":
                                    greenHSL.AddRange(pixels);
                                    break;
                                case "purple":
                                    purpleHSL.AddRange(pixels);
                                    break;
                            }
                        }
                    }
                }
            }

            List<PLYFileVertex> verticies = new List<PLYFileVertex>();
            foreach (HSL pixel in purpleHSL)
            {
                verticies.Add(new PLYFileVertex
                    {
                        X = pixel.Hue,
                        Y = pixel.Luminance * 360,
                        Z = pixel.Saturation * 360,
                        PixelColor = System.Drawing.Color.Purple
                    });
            }
            foreach (HSL pixel in redHSL)
            {
                verticies.Add(new PLYFileVertex
                {
                    X = pixel.Hue,
                    Y = pixel.Luminance * 360,
                    Z = pixel.Saturation * 360,
                    PixelColor = System.Drawing.Color.Red
                });
            }
            foreach (HSL pixel in greenHSL)
            {
                verticies.Add(new PLYFileVertex
                {
                    X = pixel.Hue,
                    Y = pixel.Luminance * 360,
                    Z = pixel.Saturation * 360,
                    PixelColor = System.Drawing.Color.Green
                });
            }
            PLYFile ply = new PLYFile
            {
                Vertices = verticies,
                Faces = new List<PLYFileFace>(), //necessary to save 
                Header = new PLYHeader(), //not necessary to save
            };
            ply.Save(@"C:\Users\brush\Desktop\cloud.ply");

            List<HSL> positives = new List<HSL>();
            List<HSL> negatives = new List<HSL>();

            Console.WriteLine("Learning red...");
            positives.AddRange(purpleHSL);
            negatives.AddRange(greenHSL);
            negatives.AddRange(redHSL);

            WriteToLibSVMFile(positives, negatives, @"c:\users\brush\desktop\purple.txt"); 

            //KernelSupportVectorMachine redSVM = LearnSVM(positives.ToArray(), negatives.ToArray(), .1);
            //PrintAccuracy("red", redSVM, positives.ToArray(), negatives.ToArray()); 
            //redSVM.Save(@"C:\users\brush\desktop\red.svm"); 

            //negatives.Clear();
            //positives.Clear();

            //Console.WriteLine("Learning purple..."); 
            //positives.AddRange(purpleHSL);
            //negatives.AddRange(redHSL);
            //negatives.AddRange(greenHSL);
            //KernelSupportVectorMachine purpleSVM = LearnSVM(positives.ToArray(), negatives.ToArray(), .1);
            //PrintAccuracy("purple", purpleSVM, positives.ToArray(), negatives.ToArray()); 
            //purpleSVM.Save(@"C:\users\brush\desktop\purple.svm"); 

            //negatives.Clear();
            //positives.Clear();

            //Console.WriteLine("Learning green..."); 
            //positives.AddRange(greenHSL);
            //negatives.AddRange(redHSL);
            //negatives.AddRange(purpleHSL);
            //KernelSupportVectorMachine greenSVM = LearnSVM(positives.ToArray(), negatives.ToArray(), .1);
            //PrintAccuracy("green", greenSVM, positives.ToArray(), negatives.ToArray()); 
            //greenSVM.Save(@"C:\users\brush\desktop\green.svm");


            Console.WriteLine("Done!");
            Console.ReadLine();
            return;
        }
예제 #2
0
        public static PLYFile ReadPLYFile(string filePath)
        {
            PLYFile file = new PLYFile();
            List<PLYFileVertex> vertices = new List<PLYFileVertex>();

            // read all the lines from the file. 
            IEnumerable<string> lines = File.ReadAllLines(filePath);

            // parse the header out of those lines. 
            file.Header = ParseHeader(lines);

            // parse the body
            ParseBody(file, lines);

            return file;
        }
예제 #3
0
        private static void ParseFacePortionOfBody(PLYFile file, string[] lines)
        {
            string[] faceLines = new string[file.Header.ElementFaceCount];
            Array.Copy(lines, file.Header.ElementFaceStartLine, faceLines, 0, faceLines.Length);

            foreach (string line in faceLines)
            {
                PLYFileFace face = new PLYFileFace();
                string[] bits = line.Split(' ');

                int numberOfVerticies = int.Parse(bits[0]);

                for (int c = 0; c < numberOfVerticies; c++)
                {
                    int vertexIndex = int.Parse(bits[c + 1]);

                    face.FaceVertices.Add(file.Vertices[vertexIndex]);
                }

                file.Faces.Add(face);
            }
        }
예제 #4
0
        private static void ParseBody(PLYFile file, IEnumerable<string> lines)
        {
            string[] linesAsArray = lines.ToArray();

            ParseVertexPortionOfBody(file, linesAsArray);
            ParseFacePortionOfBody(file, linesAsArray);

            return;
        }
예제 #5
0
        private static void ParseVertexPortionOfBody(PLYFile file, string[] lines)
        {
            string[] vertexLines = new string[file.Header.ElementVertexCount];
            Array.Copy(lines, file.Header.ElementVertexStartLine, vertexLines, 0, vertexLines.Length);

            //foreach (string line in vertexLines)
            for (int i = 0; i < vertexLines.Length; i++)
            {
                string line = vertexLines[i];

                string[] bits = line.Split(' ');
                PLYFileVertex vertex = new PLYFileVertex();

                int red = 0, blue = 0, green = 0;
                for (int c = 0; c < file.Header.ElementVertexProperties.Count; c++)
                {
                    PLYHeaderProperty prop = file.Header.ElementVertexProperties[c];

                    switch (prop.Name)
                    {
                        case "x":
                            vertex.X = double.Parse(bits[c]);
                            break;
                        case "y":
                            vertex.Y = double.Parse(bits[c]);
                            break;
                        case "z":
                            vertex.Z = double.Parse(bits[c]);
                            break;
                        case "red":
                            red = int.Parse(bits[c]);
                            break;
                        case "green":
                            green = int.Parse(bits[c]);
                            break;
                        case "blue":
                            blue = int.Parse(bits[c]);
                            break;
                    }
                }

                vertex.VertexIndex = i;
                vertex.PixelColor = Color.FromArgb(red, green, blue);
                file.Vertices.Add(vertex);
            }
        }