コード例 #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
        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);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: kwende/TomogramClassifier
        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"));
        }
コード例 #5
0
ファイル: LabeledEdgeFinder.cs プロジェクト: kwende/MRCSharp
        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;
        }