Beispiel #1
0
        public static LabeledTomogram ReadMRCFile(string mrcFilePath, int frameNumber)
        {
            MRCFile  file  = MRCParser.Parse(mrcFilePath);
            MRCFrame frame = file.Frames[frameNumber];

            LabeledTomogram ret = new LabeledTomogram();

            ret.Labels = null;
            ret.Data   = frame.Data;
            ret.Width  = frame.Width;
            ret.Height = frame.Height;

            return(ret);
        }
Beispiel #2
0
        public static List <float> Sample(string annotatedBitmap, string correspondingMRCFileName, int correspondingMRCFrameNumber)
        {
            MRCFrame file = MRCParser.Parse(correspondingMRCFileName).Frames[correspondingMRCFrameNumber];

            List <float> ret = new List <float>();

            using (Bitmap bmp = (Bitmap)Image.FromFile(annotatedBitmap))
            {
                for (int y = 0, i = 0; y < bmp.Height; y++)
                {
                    for (int x = 0; x < bmp.Width; x++, i++)
                    {
                        Color c = bmp.GetPixel(x, y);

                        if (c.R != c.B)
                        {
                            ret.Add(file.Data[i]);
                        }
                    }
                }
            }

            return(ret);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            //Convolution.DoIt();
            //EdgeTracer.DoIt();
            //LabeledEdgeFinder.DoIt();

            const string TomogramDirectory = @"c:/users/ben/downloads/";

            Console.WriteLine("Loading main file..");
            MRCFile file = MRCParser.Parse(@"D:\tomograms\tomography2_fullsirtcliptrim.mrc");

            //MRCFrame frame = file.Frames[145];

            //float[] data =
            //for (int y = 264; y < 363; y++)
            //{
            //    for (int x = 501; x < 600; x++)
            //    {

            //    }
            //}

            float scaler = 255 / (file.MaxPixelValue - file.MinPixelValue);

            //Console.WriteLine("Loading label files...");
            //List<MRCFile> labelFiles = new List<MRCFile>();
            //labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels.mrc")));
            //labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels2.mrc")));
            //labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels3.mrc")));
            //labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels4.mrc")));
            //labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels5.mrc")));
            //labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels7.mrc")));

            //Color[] colors = new Color[]
            //{
            //    Color.Red, Color.Green, Color.Pink, Color.Orange, Color.Yellow, Color.Violet
            //};

            //

            float skipLength = 1.0f;

            //int vertexCount = 0;
            //using (StreamWriter sw = new StreamWriter(Path.Combine(TomogramDirectory, "pointCloudEnding.ply")))
            //{
            for (int z = 0; z < file.Frames.Count; z++)
            {
                Console.WriteLine($"File {z}/{file.Frames.Count}");
                MRCFrame frame = file.Frames[z];
                using (Bitmap bmp = new Bitmap(frame.Width / (int)skipLength, frame.Height / (int)skipLength))
                {
                    for (int y = 0; y < frame.Height; y += (int)skipLength)
                    {
                        for (int x = 0; x < frame.Width; x += (int)skipLength)
                        {
                            try
                            {
                                int i = y * frame.Width + x;

                                bool labeled    = false;
                                int  colorIndex = 0;
                                //foreach (MRCFile label in labelFiles)
                                //{
                                //    if (label.Frames[z].Data[i] > 0)
                                //    {
                                //        byte b = (byte)(frame.Data[i] * scaler);
                                //        bmp.SetPixel((int)(x / skipLength), (int)(y / skipLength), colors[colorIndex]);
                                //        sw.WriteLine("{0} {1} {2} {3} {4} {5}",
                                //            x / skipLength,
                                //            y / skipLength,
                                //            z / skipLength,
                                //            colors[colorIndex].R, colors[colorIndex].G, colors[colorIndex].B);
                                //        //vertices.Add(new PLYVertex { X = x, Y = y, Z = z, Color = colors[colorIndex] });
                                //        labeled = true;
                                //        vertexCount++;
                                //    }

                                //    colorIndex++;
                                //}

                                //if (!labeled)
                                {
                                    float v = frame.Data[i] + Math.Abs(file.MinPixelValue);
                                    byte  b = (byte)(v * scaler);
                                    bmp.SetPixel((int)(x / skipLength), (int)(y / skipLength), Color.FromArgb(b, b, b));
                                    //vertices.Add(new PLYVertex { X = x, Y = y, Z = z, Color = Color.FromArgb(b, b, b) });
                                }
                            }
                            catch
                            {
                                //sw.WriteLine("{0} {1} {2} {3} {4} {5}", x, y, z, 0, 0, 0);
                                //vertexCount++;
                                //vertices.Add(new PLYVertex { X = x, Y = y, Z = z, Color = Color.FromArgb(0, 0, 0) });
                            }
                        }
                    }
                    bmp.Save(Path.Combine("C:/users/brush/desktop/samples/", $"{z}.png"), ImageFormat.Png);
                }
            }
            //}

            //using (StreamWriter sw = new StreamWriter(Path.Combine(TomogramDirectory, "pointCloud.ply")))
            //{
            //    sw.WriteLine("ply");
            //    sw.WriteLine("format ascii 1.0");
            //    sw.WriteLine("element vertex " + vertexCount.ToString());
            //    sw.WriteLine("property float x");
            //    sw.WriteLine("property float y");
            //    sw.WriteLine("property float z");
            //    sw.WriteLine("property uchar red");
            //    sw.WriteLine("property uchar green");
            //    sw.WriteLine("property uchar blue");
            //    sw.WriteLine("element face " + 0.ToString());
            //    sw.WriteLine("property list uchar uint vertex_indices");
            //    sw.WriteLine("end_header");

            //    using (StreamReader sr = new StreamReader(Path.Combine(TomogramDirectory, "pointCloudEnding.ply")))
            //    {
            //        string l = null;
            //        while ((l = sr.ReadLine()) != null)
            //        {
            //            sw.WriteLine(l);
            //        }
            //    }

            //}

            //using (StreamWriter sw = new StreamWriter(Path.Combine(TomogramDirectory, "pointCloud.json")))
            //{
            //    using (StreamReader sr = new StreamReader(Path.Combine(TomogramDirectory, "pointCloudEnding.ply")))
            //    {
            //        sw.WriteLine("{ \"data\": [");

            //        string l = null;
            //        while ((l = sr.ReadLine()) != null)
            //        {
            //            string[] bits = l.Split(' ');

            //            sw.WriteLine($"\"{bits[0]} {bits[1]} {bits[2]}\",");
            //            sw.WriteLine($"\"{bits[3]} {bits[4]} {bits[5]}\",");
            //        }

            //        sw.WriteLine($"\"0 0 0\",");
            //        sw.WriteLine($"\"0 0 0\"");

            //        sw.WriteLine("]}");
            //    }
            //}

            //File.Delete(Path.Combine(TomogramDirectory, "pointCloudEnding.ply"));
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            //List<float> distribution = AnnotatedTomogramSampler.Sample("C:/users/ben/desktop/145_painted.png",
            //    @"C:\Users\Ben\Desktop\tomography2_fullsirtcliptrim.mrc",
            //    145);
            //BinaryFormatter bf = new BinaryFormatter();
            //using (FileStream fout = File.Create("c:/users/ben/desktop/distribution.dat"))
            //{
            //    bf.Serialize(fout, distribution);
            //}


            int     counter = 0;
            MRCFile file    = null;

            if (Directory.Exists("c:/users/ben/desktop"))
            {
                file = MRCParser.Parse(@"c:/users/ben/desktop/tomography2_fullsirtcliptrim.mrc");
            }
            else if (Directory.Exists("/home/brush/"))
            {
                file = MRCParser.Parse(@"/home/brush/tomography2_fullsirtcliptrim.mrc");
            }
            else if (Directory.Exists(@"D:\tomograms\"))
            {
                file = MRCParser.Parse(@"D:\tomograms\tomography2_fullsirtcliptrim.mrc");
            }

            Parallel.For(0, 40, c =>
                         //int c = 0;
            {
                Random rand = new Random(c);

                lock (typeof(string))
                {
                    Console.Clear();
                    Interlocked.Increment(ref counter);
                    Console.WriteLine(counter.ToString());
                }

                Tomogram tom = SamplingVoronoiDiagramBuilder.BuildTomogram(100, 100, 3000,
                                                                           rand.Next(15, 40), file, rand);

                if (Directory.Exists("c:/users/ben/desktop"))
                {
                    TomogramDrawing.Tomogram2Bitmap(tom, false).Save($"c:/users/ben/desktop/tom4/{c}.bmp");
                    TomogramDrawing.Tomogram2Bitmap(tom, true).Save($"c:/users/ben/desktop/tom4/{c}_labeled.bmp");
                    SamplingVoronoiDiagramBuilder.SaveAsDatFile(tom, $"c:/users/ben/desktop/tom4/{c}.dat");
                    DistributionWriter.WriteDistribution(tom, $"C:/users/ben/desktop/tom4/{c}.csv");
                }
                else if (Directory.Exists("/home/brush/"))
                {
                    TomogramDrawing.Tomogram2Bitmap(tom, false).Save($"/home/brush/tom4/{c}.bmp");
                    TomogramDrawing.Tomogram2Bitmap(tom, true).Save($"/home/brush/tom4/{c}_labeled.bmp");
                    SamplingVoronoiDiagramBuilder.SaveAsDatFile(tom, $"/home/brush/tom4/{c}.dat");
                }
                else if (Directory.Exists(@"D:\tomograms\"))
                {
                    TomogramDrawing.Tomogram2Bitmap(tom, false).Save($"D:/tomograms/simulated/{c}.bmp");
                    TomogramDrawing.Tomogram2Bitmap(tom, true).Save($"D:/tomograms/simulated/{c}_labeled.bmp");
                    SamplingVoronoiDiagramBuilder.SaveAsDatFile(tom, $"D:/tomograms/simulated/{c}.dat");
                }
            });
        }
Beispiel #5
0
        public static void DoIt()
        {
            const string TomogramDirectory = @"C:\Users\Ben\Desktop\tomograms";

            Console.WriteLine("Loading main file..");
            MRCFile file = MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.mrc"));

            Console.WriteLine("Loading label files...");
            List <MRCFile> labelFiles = new List <MRCFile>();

            labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels.mrc")));
            labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels2.mrc")));
            labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels3.mrc")));
            labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels4.mrc")));
            labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels5.mrc")));
            labelFiles.Add(MRCParser.Parse(Path.Combine(TomogramDirectory, "tomography2_fullsirtcliptrim.labels7.mrc")));

            float scaler = 255 / (file.MaxPixelValue - file.MinPixelValue);

            Color[] colors = new Color[]
            {
                Color.Red, Color.Green, Color.Pink, Color.Orange, Color.Yellow, Color.Violet
            };

            for (int z = 0; z < file.Frames.Count; z++)
            {
                Console.WriteLine($"File {z}/{file.Frames.Count}");
                MRCFrame frame = file.Frames[z];
                using (Bitmap bmp = new Bitmap(frame.Width, frame.Height))
                {
                    for (int y = 1; y < frame.Height - 1; y++)
                    {
                        for (int x = 1; x < frame.Width - 1; x++)
                        {
                            //if(x == 196 && y == 123 && z == 58)
                            //{
                            //    Console.WriteLine(".");
                            //}

                            try
                            {
                                int i = y * frame.Width + x;

                                bool labeled    = false;
                                int  colorIndex = 0;
                                //foreach (MRCFile label in labelFiles)
                                //{
                                //    if (label.Frames[z].Data[i] > 0)
                                //    {
                                //        bool isEdge = false;
                                //        for (int y1 = y - 1; y1 <= y + 1; y1++)
                                //        {
                                //            for (int x1 = x - 1; x1 <= x + 1; x1++)
                                //            {
                                //                int offsetIndex = y1 * frame.Width + x1;

                                //                if (label.Frames[z].Data[offsetIndex] == 0)
                                //                {
                                //                    isEdge = true;
                                //                    break;
                                //                }
                                //            }
                                //        }

                                //        if (isEdge)
                                //        {
                                //            byte b = (byte)(frame.Data[i] * scaler);
                                //            bmp.SetPixel((int)(x), (int)(y), colors[colorIndex]);
                                //            labeled = true;
                                //        }
                                //    }

                                //    colorIndex++;
                                //}

                                if (!labeled)
                                {
                                    float v = frame.Data[i] + Math.Abs(file.MinPixelValue);
                                    byte  b = (byte)(v * scaler);
                                    bmp.SetPixel((int)(x), (int)(y), Color.FromArgb(b, b, b));
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    bmp.Save(Path.Combine(TomogramDirectory, $"{z}.png"), ImageFormat.Png);
                }
            }
        }
Beispiel #6
0
        static void Test()
        {
            BinaryFormatter bf = new BinaryFormatter();

            using (FileStream fs = File.OpenRead("serialized.dat"))
            {
                DecisionTreeNode node = bf.Deserialize(fs) as DecisionTreeNode;


                DecisionTreeOptions options = new DecisionTreeOptions
                {
                    // TODO: Fill in
                    MaximumNumberOfRecursionLevels = 25,
                    NumberOfFeatures        = 300,
                    NumberOfThresholds      = 35,
                    OffsetXMax              = 40,
                    OffsetXMin              = -40,
                    OffsetYMax              = 40,
                    OffsetYMin              = -40,
                    OutOfRangeValue         = 1000000,
                    SplittingThresholdMax   = .2f,
                    SufficientGainLevel     = 0,
                    PercentageOfPixelsToUse = .9f,
                    //DistanceThreshold = .1f,
                };


                MRCFile file = MRCParser.Parse(Path.Combine("/home/brush/tomography2_fullsirtcliptrim.mrc"));

                MRCFrame frame = file.Frames[145];

                LabeledTomogram tom = new LabeledTomogram();
                tom.Width  = frame.Width;
                tom.Height = frame.Height;
                tom.Data   = new float[frame.Width * frame.Height];

                for (int i = 0; i < frame.Data.Length; i++)
                {
                    tom.Data[i] = frame.Data[i];
                }

                //for (int y = 264, i = 0; y < 364; y++)
                //{
                //    for (int x = 501; x < 601; x++, i++)
                //    {
                //        tom.Data[i] = frame.Data[y * frame.Width + x];
                //    }
                //}


                float[] labels = DecisionTreeBuilder.Predict(tom, node, options);
                //Bitmap bmp = DataManipulator.Tomogram2Bitmap(tom);
                Bitmap bmp = Drawing.TomogramDrawing.PaintClassifiedPixelsOnTomogram(tom, labels);
                bmp.Save("/var/www/html/static/labeled_real.png", System.Drawing.Imaging.ImageFormat.Png);

                LabeledTomogram tom2 = DataReader.ReadDatFile("/home/brush/tom4/0.dat");

                labels = DecisionTreeBuilder.Predict(tom2, node, options);
                //Bitmap bmp = DataManipulator.Tomogram2Bitmap(tom);
                bmp = Drawing.TomogramDrawing.PaintClassifiedPixelsOnTomogram(tom2, labels);
                bmp.Save("/var/www/html/static/labeled_simulated.png", System.Drawing.Imaging.ImageFormat.Png);
            }
        }