c# 图像识别 - 运行 在整个文件夹上

c# image recognition - RUN ON ENTIRE FOLDER

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

   namespace ConsoleApp1
   {
  class Program
{


    static String filename1;

    static String filename2;
    static int equalElements;

    //parse image file names into txt file for working cameras
    static StreamWriter writetext =  new StreamWriter(@"c: \Users\ChawlaRa\Desktop\FilezilleIMGS\MTO_Image\working.txt",true);

    static StreamWriter writetext2 = new StreamWriter(@"c: \Users\ChawlaRa\Desktop\FilezilleIMGS\MTO_Image\notworking.txt", true);

    //method which converts image to 16*16 gets hash code
    public static List<bool> GetHash(Bitmap bmpSource)
    {
        List<bool> lResult = new List<bool>();
        //create new image with 16x16 pixel
        Bitmap bmpMin = new Bitmap(bmpSource, new Size(16, 16));
        for (int j = 0; j < bmpMin.Height; j++)
        {
            for (int i = 0; i < bmpMin.Width; i++)
            {
                //reduce colors to true / false                
                lResult.Add(bmpMin.GetPixel(i, j).GetBrightness() < 0.5f);
            }
        }
        return lResult;
    }


    public static int CompareImgSimilarityvalue(String filename1,String filename2)
    {
       List<bool> iHash1 = GetHash(new Bitmap(@"C:\Users\ChawlaRa\Desktop\FilezilleIMGS\MTO_Image\"+filename1+".jpg"));
        List <bool> iHash2 = GetHash(new Bitmap(@"C:\Users\ChawlaRa\Desktop\FilezilleIMGS\MTO_Image\" + filename2+".jpg"));

        //determine the number of equal pixel (x of 256) 256 is a perfect match
        equalElements = iHash1.Zip(iHash2, (i, j) => i == j).Count(eq => eq);

        return equalElements;
    }



    public static void Analyis()
        {

            filename1= Console.ReadLine();
            filename2= Console.ReadLine();

            CompareImgSimilarityvalue(filename1, filename2);


        //if LESS THAN 98% similar
        if (equalElements < 253.44)
        {
            //images different
            Console.WriteLine("NOT Same Images");

            Console.WriteLine("These images below are DIFFERENT - conclusion CAMERA WORKING");
            Console.WriteLine(filename1 + ".jpg");
            Console.WriteLine(filename2 + ".jpg");
            using ( writetext)
            {
                writetext.Write(filename1 + ".jpg");
                writetext.Write(",");
                writetext.Write(filename2 + ".jpg");
                writetext.Write(",");
            }
            Console.WriteLine("The image file names of the non working cameras above have been appened to list of nonworking cameras.");
        }

        else
        //images same
        {
            Console.WriteLine("These images below are the same- conclusion CAMERA NOT WORKING ");
            Console.WriteLine(filename1 + ".jpg");
            Console.WriteLine(filename2 + ".jpg");

            using (writetext2)
            {
                writetext2.Write(filename1 + ".jpg");
                writetext2.Write(",");
                writetext2.Write(filename2 + ".jpg");
                writetext2.Write(",");
            }
            Console.WriteLine("The image file names of the non working cameras above have been appened to list of nonworking cameras.");

        }
           // Console.WriteLine(equalElements);
            Console.ReadLine();


        }



    //main test harness

    static void Main(string[] args)

    {

        Analyis();





    }
}
   }

所以这是我的一个应用程序的代码,它要求用户提供 2 个图像文件,比较它们,如果相同,它会将这 2 个文件的名称附加到一个无效的文本文件中,如果不同,则不工作'会将 2 个 img 文件的名称附加到名为 WORKING.txt 的文本文件中。

基本上我的问题是用户输入。包含图像的目录包含大约 1100 张图像,第一张图像比较第二张,第三张到第四张,依此类推,

所以我不想手动输入每个图像文件名来将它与第二个文件进行比较,我想遍历整个 MTOImage 目录并比较图像,同时将 similar/different 相机的名称附加到它们的各自的 txt 文件。

我很难自动执行此操作,有人可以帮忙吗?谢谢

更新:编辑后的分析方法 Arman 建议

 public static void Analyis()
        {
        /*
            filename1= Console.ReadLine();
            filename2= Console.ReadLine();

            CompareImgSimilarityvalue(filename1, filename2);
        */




        string[] files = Directory.GetFiles(@"C:\Users\ChawlaRa\Desktop\FilezilleIMGS\MTO_Image");
        for (var i = 0; i < (files.Length - 1); i++)
        {
            CompareImgSimilarityvalue(files[i], files[i + 1]);





            //if LESS THAN 98% similar
            if (equalElements < 253.44)
            {
                //images different camera working
                Console.WriteLine("NOT Same Images");

                Console.WriteLine("These images below are DIFFERENT - conclusion CAMERA WORKING");
                Console.WriteLine(files[i] + ".jpg");
                Console.WriteLine(files[i+1] + ".jpg");
                using (writetext)
                {
                    writetext.Write(files[i] + ".jpg");
                    writetext.Write(",");
                    writetext.Write(files[i+1] + ".jpg");
                    writetext.Write(",");
                }
                Console.WriteLine("The image file names of the  working cameras above have been appened to list of working cameras.");
            }

            else
            //images same camera frozen
            {
                Console.WriteLine("These images below are the same- conclusion CAMERA NOT WORKING ");
                Console.WriteLine(files[i] + ".jpg");
                Console.WriteLine(files[i+1] + ".jpg");

                using (writetext2)
                {
                    writetext2.Write(files[i] + ".jpg");
                    writetext2.Write(",");
                    writetext2.Write(files[i+1] + ".jpg");
                    writetext2.Write(",");
                }
                Console.WriteLine("The image file names of the non working cameras above have been appened to list of nonworking cameras.");

            }
        }
           // Console.WriteLine(equalElements);
            Console.ReadLine();


        }
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Analyis();
        }

        //method which converts image to 16*16 gets hash code
        public static List<bool> GetHash(Bitmap bmpSource)
        {
            List<bool> lResult = new List<bool>();
            //create new image with 16x16 pixel
            Bitmap bmpMin = new Bitmap(bmpSource, new Size(16, 16));
            for (int j = 0; j < bmpMin.Height; j++)
            {
                for (int i = 0; i < bmpMin.Width; i++)
                {
                    //reduce colors to true / false                
                    lResult.Add(bmpMin.GetPixel(i, j).GetBrightness() < 0.5f);
                }
            }
            return lResult;
        }


        public static int CompareImgSimilarityvalue(String filename1, String filename2)
        {
            List<bool> iHash1 = GetHash(new Bitmap(filename1));
            List<bool> iHash2 = GetHash(new Bitmap(filename2));

            //determine the number of equal pixel (x of 256) 256 is a perfect match
            int equalElements = iHash1.Zip(iHash2, (i, j) => i == j).Count(eq => eq);

            return equalElements;
        }



        public static void Analyis()
        {
            var writetext = new StreamWriter(@"C:\Whosebugtests76837\working.txt", true);
            var writetext2 = new StreamWriter(@"C:\Whosebugtests76837\notworking.txt", true);

            try
            {
                string[] files = Directory.GetFiles(@"C:\Whosebugtests76837\MTO_Image");
                for (var i = 0; i < (files.Length - 1); i++)
                {
                    string filename1 = files[i];
                    string filename2 = files[i + 1];
                    int equalElements = CompareImgSimilarityvalue(filename1, filename2);

                    //if LESS THAN 98% similar
                    if (equalElements < 253.44)
                    {
                        //images different
                        Console.WriteLine("NOT Same Images");

                        Console.WriteLine("These images below are DIFFERENT - conclusion CAMERA WORKING");
                        Console.WriteLine(filename1);
                        Console.WriteLine(filename2);

                        // Write to files
                        writetext.Write(filename1);
                        writetext.Write(",");
                        writetext.Write(filename2);
                        writetext.Write(",");

                        Console.WriteLine("The image file names of the non working cameras above have been appened to list of nonworking cameras.");
                    }

                    else
                    //images same
                    {
                        Console.WriteLine("These images below are the same- conclusion CAMERA NOT WORKING ");
                        Console.WriteLine(filename1);
                        Console.WriteLine(filename2);

                        // Write to files
                        writetext2.Write(filename1);
                        writetext2.Write(",");
                        writetext2.Write(filename2);
                        writetext2.Write(",");

                        Console.WriteLine("The image file names of the non working cameras above have been appened to list of nonworking cameras.");

                    }
                    // Console.WriteLine(equalElements);                    
                }
                Console.ReadLine();
            }
            catch(Exception e)
            {
                // Catch exceptions here
            }
            finally
            {
                writetext.Dispose();
                writetext2.Dispose();
            }
        }
    }
}

我认为您过度使用了静态方法和变量。例如。我会将 equalElements 设为局部变量,这样您就可以使用它更轻松地遍历所有文件:

public int CompareImgSimilarityvalue(String filename1, String filename2)
    {
        List<bool> iHash1 = GetHash(new Bitmap(filename1));
        List<bool> iHash2 = GetHash(new Bitmap(filename2));

        //determine the number of equal pixel (x of 256) 256 is a perfect match
        return iHash1.Zip(iHash2, (i, j) => i == j).Count(eq => eq);

    }

    public static void Analyis()
    {

        string[] files = Directory.GetFiles(@"C:\Users\ChawlaRa\Desktop\FilezilleIMGS\MTO_Image\", "*.jpg");

        Array.Sort(files);

        int equalElements = 0;
        StringBuilder workingText = new StringBuilder();
        StringBuilder notWorkingText = new StringBuilder();


        for (var i = 0; i < (files.Length - 1); i++)
        {
            equalElements = CompareImgSimilarityvalue(files[i], files[i + 1]);



            //if LESS THAN 98% similar
            if (equalElements < 253.44)
            {
                //images different
                Console.WriteLine("NOT Same Images");

                Console.WriteLine("These images below are DIFFERENT - conclusion CAMERA WORKING");
                Console.WriteLine(files[i] + ".jpg");
                Console.WriteLine(files[i+1] + ".jpg");

                workingText.AppendLine(files[i] + ".jpg");
                workingText.AppendLine(",");
                workingText.AppendLine(files[i+1] + ".jpg");
                workingText.AppendLine(",");

                Console.WriteLine("The image file names of the non working cameras above have been appended to list of nonworking cameras.");
            }

            else
            //images same
            {
                Console.WriteLine("These images below are the same- conclusion CAMERA NOT WORKING ");
                Console.WriteLine(files[i] + ".jpg");
                Console.WriteLine(files[i+1] + ".jpg");


                notWorkingText.AppendLine(files[i] + ".jpg");
                notWorkingText.AppendLine(",");
                notWorkingText.AppendLine(files[i+1] + ".jpg");
                notWorkingText.AppendLine(",");

                Console.WriteLine("The image file names of the non working cameras above have been appended to list of nonworking cameras.");

            }

        }
        // Console.WriteLine(equalElements);


        // you probably shouldn't be using static StreamWriters here 
        using (writetext) 
        {
            writetext.Write(workingText.ToString());
        }    
        using (writetext2) 
        {
            writetext2.Write(notWorkingText.ToString());
        }          

        Console.ReadLine();


    }