【问题标题】:Getting wrong colors after an image manipulations图像处理后颜色错误
【发布时间】:2015-06-01 06:26:06
【问题描述】:

我正在尝试将一些值与我的图像的 RGB 值进行异或,保存该图像并执行后退步骤,以获得原始图像。 问题是,我不知道为什么我的图像不清楚(有一些噪音)。 这是我的代码,下面是一张图片:

Bitmap original = new Bitmap("D:\\img\\1.jpg");
Bitmap inp_bmp = new Bitmap("D:\\img\\1.jpg");

int width = inp_bmp.Width;
int height = inp_bmp.Height;
Color pixel;

for (int y = 0; y < height; y += 1)
{
    for (int x = 0; x < width; x += 1)
    {
        pixel = inp_bmp.GetPixel(x, y);

        int a = pixel.A;
        int r = (pixel.R ^ (1000))%256;
        int g = (pixel.G ^ (185675))%256;
        int b = (pixel.B ^ (78942))%256;
        inp_bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));


    }
}

pictureBox2.Image = inp_bmp;
pictureBox1.Image = original;
inp_bmp.Save("D:\\img\\4.jpg");

图像保存后,我更改

Bitmap inp_bmp = new Bitmap("D:\\img\\1.jpg");

 Bitmap inp_bmp = new Bitmap("D:\\img\\4.jpg");

并删除

//inp_bmp.Save("D:\\img\\4.jpg");

我得到一个像

这样的图像

(左原始,右 - 结果); 正如你所看到的,我在图 4 中得到了一些错误的颜色,为什么? 总而言之接近原著,但还是不对

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 您的代码在这里可以正常使用真实的 jpeg。能发一下原图吗?
  • 对不起朋友,贴出的代码没有问题。这里的结果看起来和原来的完全一样。然而both你的第一个改变了,第二个看起来好像他们有一个减少的调色板,就像一个未抖动的 gif 一样。一定有别的事情发生了!
  • &amp; 0xFF 可能比% 256 加载效率更高,因为它是位运算而不是除法。

标签: c# colors bitmap xor


【解决方案1】:

我猜您的图像不使用 8 位颜色。 % 256 假设您有一个 8 位图像。

【讨论】:

  • 试过没有 % 256 像 int r = (pixel.R ^ 220); int g = (pixel.G ^ 255); int b = (pixel.B ^ 206);还是一样的问题
  • jpeg 始终具有 8 位颜色分量。事实上,据我所知,所有的网络图片都是如此。
【解决方案2】:

好的,我发现了问题。问题在于保存图像。

这有帮助:

 inp_bmp.Save("D:\\img\\4.png", System.Drawing.Imaging.ImageFormat.Png); 

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2023-03-22
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多