C# 这个图像相似性代码是线程安全的吗?

C# 这个图像相似性代码是线程安全的吗?,c#,image-processing,thread-safety,C#,Image Processing,Thread Safety,我正在寻找帮助,检查两张图片之间的相似性,我遇到了,这是指。虽然这两篇文章都提供了有用的信息,但我决定使用上的方法——这非常简单 考虑到这一背景,我决定使用下面的方法来比较两幅图像。我的问题是——这种方法是否能很好地扩展?我的意思是说它是线程安全的吗?如果它不是线程安全的,我应该改变什么使它成为线程安全的 using System; using System.Drawing; class Program { static void Main() { Bitmap

我正在寻找帮助,检查两张图片之间的相似性,我遇到了,这是指。虽然这两篇文章都提供了有用的信息,但我决定使用上的方法——这非常简单

考虑到这一背景,我决定使用下面的方法来比较两幅图像。我的问题是——这种方法是否能很好地扩展?我的意思是说它是线程安全的吗?如果它不是线程安全的,我应该改变什么使它成为线程安全的

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
        Bitmap img1 = new Bitmap("Lenna50.jpg");
        Bitmap img2 = new Bitmap("Lenna100.jpg");

        if (img1.Size != img2.Size)
        {
            Console.Error.WriteLine("Images are of different sizes");
            return;
        }

        float diff = 0;

        for (int y = 0; y < img1.Height; y++)
        {
            for (int x = 0; x < img1.Width; x++)
            {
                Color pixel1 = img1.GetPixel(x, y);
                Color pixel2 = img2.GetPixel(x, y);

                diff += Math.Abs(pixel1.R - pixel2.R);
                diff += Math.Abs(pixel1.G - pixel2.G);
                diff += Math.Abs(pixel1.B - pixel2.B);
            }
        }

        Console.WriteLine("diff: {0} %", 100 * (diff / 255) / (img1.Width * img1.Height * 3));
    }
}
使用系统;
使用系统图;
班级计划
{
静态void Main()
{
位图img1=新位图(“Lenna50.jpg”);
位图img2=新位图(“Lenna100.jpg”);
if(img1.Size!=img2.Size)
{
Console.Error.WriteLine(“图像大小不同”);
返回;
}
浮动差=0;
对于(int y=0;y
更新:这是更新的类和线程代码。我不太习惯穿线。当我在本地测试时,它运行良好。我想知道这是否是一个可以使用的方法。我还想知道是否应该使用Thread.Sleep,这样这个线程就不会阻塞其他线程

using System;
using System.Drawing;
using System.Threading;

    public class CompareImages
    {
        public static string statusmsg = string.Empty;

        public static void CheckImages(string imagepath1, string imagepath2)
        {
            // Create thread
            Thread thread = new Thread(() => CheckImageDiff(imagepath1, imagepath2));
            thread.Start();
            thread.Join(); // Does this mean I don't have to do Thread.Sleep(__)?
        }

        public static void CheckImageDiff(string path1, string path2)
        {
            Bitmap img1 = new Bitmap(path1);
            Bitmap img2 = new Bitmap(path2);

            if (img1.Size != img2.Size)
            {
                statusmsg = "Images are of different sizes. Images should be the same size.";
            }
            else
            {
                float diff = 0;

                for (int y = 0; y < img1.Height; y++)
                {
                    for (int x = 0; x < img1.Width; x++)
                    {
                        Color pixel1 = img1.GetPixel(x, y);
                        Color pixel2 = img2.GetPixel(x, y);

                        diff += Math.Abs(pixel1.R - pixel2.R);
                        diff += Math.Abs(pixel1.G - pixel2.G);
                        diff += Math.Abs(pixel1.B - pixel2.B);
                    }
                }

                statusmsg = "diff: {0} %";
                statusmsg = string.Format(statusmsg, 100 * (diff / 255) / (img1.Width * img1.Height * 3));
            }

            // Write status msg to database or make available to other method...

        }

    }
使用系统;
使用系统图;
使用系统线程;
公共类比较
{
公共静态字符串statusmsg=string.Empty;
公共静态无效检查映像(字符串imagepath1、字符串imagepath2)
{
//创建线程
线程线程=新线程(()=>CheckImageDiff(imagepath1,imagepath2));
thread.Start();
thread.Join();//这是否意味着我不必执行thread.Sleep(_u;)?
}
公共静态void CheckImageDiff(字符串路径1、字符串路径2)
{
位图img1=新位图(路径1);
位图img2=新位图(路径2);
if(img1.Size!=img2.Size)
{
statusmsg=“图像大小不同。图像大小应相同。”;
}
其他的
{
浮动差=0;
对于(int y=0;y
这个问题的答案在很大程度上取决于线程到底做什么、访问什么类型的数据以及如何访问数据。由于您没有显示任何线程(除了主线程),因此无法真正回答此问题。没有涉及任何线程,所以您为什么要问?我添加了更多代码以澄清。基本上,我们的目标是比较2个或更多上传的图像,并确定它们有多相似。映像将驻留在文件系统或某种云blob存储(如Azure blob存储)中。