using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace TrainSVM
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("dg.train",FileMode.OpenOrCreate,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            String[] filePathArr = Directory.GetFiles("E:\\images\\");

            foreach (string filePath in filePathArr)
            {
                if (filePath.Contains("HBP"))
                {
                    sw.Write("1 ");
                    Console.Write("1 ");
                }
                else
                {
                    sw.Write("1 ");
                    Console.Write("1 ");
                }

                using (Bitmap originalBMP = new Bitmap(filePath))
                {


                    /***********************/
                    Bitmap imageBody;
                    ImageBody.ImageBody im = new ImageBody.ImageBody(originalBMP);
                    using (imageBody = im.GetImageBody(-1))
                    {

                        /* white coat */
                        Bitmap whiteCoatBitmap = Rgb2Hsi.Rgb2Hsi.GetHuePlane(imageBody);
                        float WhiteCoatPixelPercentage = Rgb2Hsi.Rgb2Hsi.GetWhiteCoatPixelPercentage(whiteCoatBitmap);
                        //Console.Write("whiteDone\t");
                        sw.Write("1:" + WhiteCoatPixelPercentage + " ");
                        Console.Write("1:" + WhiteCoatPixelPercentage + " ");

                        /******************/
                        Quaternion.Quaternion qtr = new Quaternion.Quaternion(-15);
                        Bitmap yellowCoatBMP = qtr.processImage(imageBody);
                        //yellowCoatBMP.Save("yellowCoat.bmp");
                        float yellowCoatPixelPercentage = qtr.GetYellowCoatPixelPercentage(yellowCoatBMP);
                        //Console.Write("yellowCoatDone\t");
                        sw.Write("2:" + yellowCoatPixelPercentage + " ");
                        Console.Write("2:" + yellowCoatPixelPercentage + " ");

                        /**********************/
                        Bitmap balckPatchBitmap = BlackPatchDetection.BlackPatchDetector.MarkBlackPatches(imageBody);
                        float BlackPatchPixelPercentage = BlackPatchDetection.BlackPatchDetector.BlackPatchPercentage;
                        //Console.Write("balckPatchDone\n");
                        sw.Write("3:" + BlackPatchPixelPercentage + "\n");
                        Console.Write("3:" + BlackPatchPixelPercentage + "\n");
                    }
                }


                sw.Flush();

            }


            sw.Dispose();
            fs.Dispose();

        }

    }
}

最佳答案

有一些您不需要处理的Bitmap实例。您应该真正养成使用using块的习惯,而不是手动进行处理,以阻止这些东西从网上溜走。

关于c# - 垃圾收集器未完成其工作。内存消耗= 1.5GB&OutOFMemory异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2559826/

10-10 09:47