예제 #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);
        }
예제 #2
0
        public static Bitmap MRCFrame2Bitmap(MRCFile file, int frameNumber)
        {
            MRCFrame frame = file.Frames[frameNumber];

            float minFloat = file.MinPixelValue;
            float maxfloat = file.MaxPixelValue;

            float delta  = maxfloat - minFloat;
            float scaler = 255 / delta;

            Bitmap bmp = new Bitmap(frame.Width, frame.Height);

            for (int y = 0, i = 0; y < frame.Height; y++)
            {
                for (int x = 0; x < frame.Width; x++, i++)
                {
                    byte b = (byte)((frame.Data[i] + Math.Abs(minFloat)) * scaler);
                    bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(b, b, b));
                }
            }

            return(bmp);
        }
예제 #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"));
        }
예제 #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");
                }
            });
        }
예제 #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);
                }
            }
        }
예제 #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);
            }
        }
예제 #7
0
        private static void FinalizeFrame(Tomogram tom, Random rand, MRCFile file,
                                          string serializedSamplerPath)
        {
            float minValue = file.MinPixelValue;
            float maxValue = file.MaxPixelValue;

            tom.MRCScaler            = 255.0f / (maxValue - minValue);
            tom.MinimumTomogramValue = minValue;

            int[] classes = tom.DataClasses.Where(n => n != -1).Distinct().ToArray();

            Dictionary <int, float> classValues = new Dictionary <int, float>();

            for (int c = 0; c < classes.Length; c++)
            {
                MRCFrame frame = file.Frames[145];// rand.Next(0, file.Frames.Count - 1)];

                int   randomX     = rand.Next(511, 611);
                int   randomY     = rand.Next(292, 392);
                int   randomIndex = randomY * frame.Width + randomX;
                float value       = frame.Data[randomIndex];
                classValues.Add(classes[c], value);
            }

            for (int y = 0, i = 0; y < tom.Height; y++)
            {
                for (int x = 0; x < tom.Width; x++, i++)
                {
                    int classNumber = tom.DataClasses[i];
                    if (classNumber >= 0)
                    {
                        tom.Data[i] = classValues[classNumber];
                    }
                }
            }


            List <float>    distribution = null;
            BinaryFormatter bf           = new BinaryFormatter();

            using (FileStream fin = File.OpenRead(serializedSamplerPath))
            {
                distribution = bf.Deserialize(fin) as List <float>;
            }

            int[] mask = new int[tom.Width * tom.Height];

            for (int y = 0, i = 0; y < tom.Height; y++)
            {
                for (int x = 0; x < tom.Width; x++, i++)
                {
                    int classNumber = tom.DataClasses[i];
                    if (classNumber == -1)
                    {
                        tom.Data[i] = distribution[rand.Next(0, distribution.Count - 1)];
                        mask[i]     = 0;
                    }
                    else
                    {
                        mask[i] = 1;
                    }
                }
            }
            //            tom.Data = blur.BlurData(tom.Data, tom.Width, tom.Height);

            GaussianBlur blur = GaussianBlur.BuildBlur(.65f, tom.BlurRadius);

            tom.Data = blur.SelectivelyBlurData(tom.Data, mask, tom.Width, tom.Height, 1f, rand);

            float[] finalData        = new float[tom.FinalHeight * tom.FinalWidth];
            int[]   finalDataClasses = new int[tom.FinalHeight * tom.FinalWidth];

            for (int y = 0, dstIndex = 0; y < tom.FinalHeight; y++)
            {
                for (int x = 0; x < tom.FinalHeight; x++, dstIndex++)
                {
                    int srcIndex = (y + tom.BlurRadius) * tom.Width + (x + tom.BlurRadius);
                    finalData[dstIndex]        = tom.Data[srcIndex];
                    finalDataClasses[dstIndex] = tom.DataClasses[srcIndex];
                }
            }

            // hack. clean this up.
            tom.Data        = finalData;
            tom.DataClasses = finalDataClasses;
            tom.Width       = tom.FinalWidth;
            tom.Height      = tom.FinalHeight;
        }
예제 #8
0
        public static Tomogram BuildTomogram(int width, int height,
                                             int backgroundDensity, int vesicleCount, MRCFile file, Random rand)
        {
            Tomogram ret = new Tomogram();

            ret.BlurRadius           = 2;
            ret.Width                = width + ret.BlurRadius * 2;
            ret.Height               = height + ret.BlurRadius * 2;
            ret.FinalWidth           = width;
            ret.FinalHeight          = height;
            ret.BackgroundDensity    = backgroundDensity;
            ret.VesicleCount         = vesicleCount;
            ret.Random               = new Random();
            ret.MinimumVesicleRadius = 8;
            ret.MaximumVesicleRadius = 15;
            ret.Data        = new float[ret.Width * ret.Height];
            ret.DataClasses = new int[ret.Width * ret.Height];

            BuildBackground(ret);
            AddVesicles(ret);

            FinalizeFrame(ret, rand, file, "distribution.dat");

            return(ret);
        }