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


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: UsersChawlaRaDesktopFilezilleIMGSMTO_Imageworking.txt",true);
    static StreamWriter writetext2 = new StreamWriter(@"c: UsersChawlaRaDesktopFilezilleIMGSMTO_Imagenotworking.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:UsersChawlaRaDesktopFilezilleIMGSMTO_Image"+filename1+".jpg"));
        List <bool> iHash2 = GetHash(new Bitmap(@"C:UsersChawlaRaDesktopFilezilleIMGSMTO_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文件的名称附加到称为工作的文本文件。

基本上我的问题是用户输入。包含图像的目录包含大约1100张图像,第一个图像比较第二张图像,第三张图像,第4张图像,

因此,我不想手动键入每个图像文件名与第二个文件进行比较,我想循环浏览整个Mtoimage目录并在附加相似/不同摄像机的名称与其各自的TXT文件的同时比较图像。

我很难自动执行此操作吗?谢谢你

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

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


        string[] files = Directory.GetFiles(@"C:UsersChawlaRaDesktopFilezilleIMGSMTO_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:stackoverflowtests4476837working.txt", true);
            var writetext2 = new StreamWriter(@"C:stackoverflowtests4476837notworking.txt", true);
            try
            {
                string[] files = Directory.GetFiles(@"C:stackoverflowtests4476837MTO_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:UsersChawlaRaDesktopFilezilleIMGSMTO_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();

    }

相关内容

  • 没有找到相关文章

最新更新