图像比较,并返回百分比 [英] Image Comparing and return Percentage

查看:197
本文介绍了图像比较,并返回百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int DiferentPixels = 0;
Bitmap first = new Bitmap("First.jpg");
Bitmap second = new Bitmap("Second.jpg");
Bitmap container = new Bitmap(first.Width, first.Height);
for (int i = 0; i < first.Width; i++)
{
    for (int j = 0; j < first.Height; j++)
    {
    int r1 = second.GetPixel(i, j).R;
    int g1 = second.GetPixel(i, j).G;
    int b1 = second.GetPixel(i, j).B;

    int r2 = first.GetPixel(i, j).R;
    int g2 = first.GetPixel(i, j).G;
    int b2 = first.GetPixel(i, j).B;

    if (r1 != r2 && g1 != g2 && b1 != b2)
    {
    DiferentPixels++;
    container.SetPixel(i, j, Color.Red);
    }
    else
    container.SetPixel(i, j, first.GetPixel(i, j));
    }
}
int TotalPixels = first.Width * first.Height;
float dierence = (float)((float)DiferentPixels / (float)TotalPixels);
float percentage = dierence * 100;

使用code IM比较2图像的foreach像素的这一部分,是的它工作的IT回报的差异百分比,因此它首先图像的每个像素与像素在第二次图像。但这里有什么问题的同一指标相比,我有一个巨大的precision也许它不应该这样的工作,比较,也许有一些更好的算法,这是更快,更灵活。
因此,任何人有一个想法如何可以改变的比较,我应该继续与或者我应该比较每个像素的颜色或....

With this portion of Code im comparing 2 Images foreach Pixels and yes it work's it return's Percentage of difference ,so it compares each Pixel of First Image with pixel in same index of Second Image .But what is wrong here ,i have a huge precision maybe it should not work like that ,the comparison ,and maybe there are some better algorithms which are faster and more flexible . So anyone has an idea how can i transform the comparison ,should i continue with that or should i compare Colors of Each Pixels or ....

PS:如果任何人有一个解决方案是如何使这个并行code,我也接受!像扩大这4个线程,他们会做一个四核心的更快吧?

PS : If anyone has a solution how to make Parallel this code ,i would also accept it ! Like expanding this to 4 Threads would they do it faster right in a Quad Core?

推荐答案

一个明显的变化是调用 GetPixel 只有一次位图,然后直接返回颜色结构工作:

One obvious change would be call GetPixel only once per Bitmap, and then work with the returned Color structs directly:

for (int i = 0; i < first.Width; ++i)
{
    for (int j = 0; j < first.Height; ++j)
    {
        Color secondColor = second.GetPixel(i, j);
        Color firstColor = first.GetPixel(i, j);

        if (firstColor != secondColor)
        {
            DiferentPixels++;
            container.SetPixel(i, j, Color.Red);
        }
        else
        {
            container.SetPixel(i, j, firstColor);
        }
    }
}

这篇关于图像比较,并返回百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆