如何创造棕褐色调?

时间:2023-02-14 08:12:14

What are the basic operations needed to create a sepia tone? My reference point is the perl imagemagick library, so I can easily use any basic operation. I've tried to quantize (making it grayscale), colorize, and then enhance the image but it's still a bit blurry.

创建棕褐色调需要什么基本操作?我的参考点是perl imagemagick库,所以我可以轻松使用任何基本操作。我试图量化(使其灰度),着色,然后增强图像,但它仍然有点模糊。

4 个解决方案

#1


Sample code of a sepia converter in C# is available in my answer here: What is wrong with this sepia tone conversion algorithm?

我在这里的答案中提供了C#中的棕褐色转换器的示例代码:这种棕褐色调转换算法有什么问题?

The algorithm comes from this page, each input pixel color is transformed in the following way:

该算法来自此页面,每个输入像素颜色按以下方式转换:

outputRed = (inputRed * .393) + (inputGreen *.769) + (inputBlue * .189)
outputGreen = (inputRed * .349) + (inputGreen *.686) + (inputBlue * .168)
outputBlue = (inputRed * .272) + (inputGreen *.534) + (inputBlue * .131)

If any of these output values is greater than 255, you simply set it to 255. These specific values are the values for sepia tone that are recommended by Microsoft.

如果这些输出值中的任何一个大于255,则只需将其设置为255.这些特定值是Microsoft推荐的棕褐色调值。

#2


This is in C#, however, the basic concepts are the same. You will likely be able to convert this into perl.

这是在C#中,但基本概念是相同的。您可能会将其转换为perl。

  private void SepiaBitmap(Bitmap bmp)
{
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
        System.Drawing.Imaging.PixelFormat.Format32bppRgb);

    IntPtr ptr = bmpData.Scan0;

    int numPixels = bmpData.Width * bmp.Height;
    int numBytes = numPixels * 4;
    byte[] rgbValues = new byte[numBytes];

    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, numBytes);
    for (int i = 0; i < rgbValues.Length; i += 4)
    {
        rgbValues[i + 2] = (byte)Math.Min((.393 * red) + (.769 * green) + (.189 * (blue)), 255.0); //red
        rgbValues[i + 1] = (byte)Math.Min((.349 * red) + (.686 * green) + (.168 * (blue)), 255.0); //green
        rgbValues[i + 0] = (byte)Math.Min((.272 * red) + (.534 * green) + (.131 * (blue)), 255.0); //blue
        if ((rgbValues[i + 2]) > 255)
        {
            rgbValues[i + 2] = 255; 
        }

        if ((rgbValues[i + 1]) > 255)
        {
            rgbValues[i + 1] = 255;
        }
        if ((rgbValues[i + 0]) > 255)
        {
            rgbValues[i + 0] = 255;
        }
    }

    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes);
    this.Invalidate();
    bmp.UnlockBits(bmpData);

}

#3


It's easy if you use the imagemagic command line.

如果你使用imagemagic命令行很容易。

http://www.imagemagick.org/script/convert.php

Use the "-sepia-tone threshold" argument when converting.

转换时使用“-sepia-tone threshold”参数。

Strangely enough, the PerlMagick API doesn't seem to include a method for doing this directly:

奇怪的是,PerlMagick API似乎没有包含直接执行此操作的方法:

http://www.imagemagick.org/script/perl-magick.php

...and no reference to any Sepia method.

......并没有提到任何深褐色方法。

#4


Take a look at how it's implemented in the AForge.NET library, the C# code is here.

看看它是如何在AForge.NET库中实现的,C#代码就在这里。

The basics seem to be

基本似乎是

  • transform it to the YIQ color space
  • 将其转换为YIQ颜色空间

  • modify it
  • transform back to RGB
  • 转换回RGB

The full alrogithm is in the source code, plus the RGB -> YIQ and YIQ -> RGB transformations are explained.

完整的alrogithm在源代码中,加上RGB - > YIQ和YIQ - > RGB转换。

#1


Sample code of a sepia converter in C# is available in my answer here: What is wrong with this sepia tone conversion algorithm?

我在这里的答案中提供了C#中的棕褐色转换器的示例代码:这种棕褐色调转换算法有什么问题?

The algorithm comes from this page, each input pixel color is transformed in the following way:

该算法来自此页面,每个输入像素颜色按以下方式转换:

outputRed = (inputRed * .393) + (inputGreen *.769) + (inputBlue * .189)
outputGreen = (inputRed * .349) + (inputGreen *.686) + (inputBlue * .168)
outputBlue = (inputRed * .272) + (inputGreen *.534) + (inputBlue * .131)

If any of these output values is greater than 255, you simply set it to 255. These specific values are the values for sepia tone that are recommended by Microsoft.

如果这些输出值中的任何一个大于255,则只需将其设置为255.这些特定值是Microsoft推荐的棕褐色调值。

#2


This is in C#, however, the basic concepts are the same. You will likely be able to convert this into perl.

这是在C#中,但基本概念是相同的。您可能会将其转换为perl。

  private void SepiaBitmap(Bitmap bmp)
{
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
        System.Drawing.Imaging.PixelFormat.Format32bppRgb);

    IntPtr ptr = bmpData.Scan0;

    int numPixels = bmpData.Width * bmp.Height;
    int numBytes = numPixels * 4;
    byte[] rgbValues = new byte[numBytes];

    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, numBytes);
    for (int i = 0; i < rgbValues.Length; i += 4)
    {
        rgbValues[i + 2] = (byte)Math.Min((.393 * red) + (.769 * green) + (.189 * (blue)), 255.0); //red
        rgbValues[i + 1] = (byte)Math.Min((.349 * red) + (.686 * green) + (.168 * (blue)), 255.0); //green
        rgbValues[i + 0] = (byte)Math.Min((.272 * red) + (.534 * green) + (.131 * (blue)), 255.0); //blue
        if ((rgbValues[i + 2]) > 255)
        {
            rgbValues[i + 2] = 255; 
        }

        if ((rgbValues[i + 1]) > 255)
        {
            rgbValues[i + 1] = 255;
        }
        if ((rgbValues[i + 0]) > 255)
        {
            rgbValues[i + 0] = 255;
        }
    }

    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes);
    this.Invalidate();
    bmp.UnlockBits(bmpData);

}

#3


It's easy if you use the imagemagic command line.

如果你使用imagemagic命令行很容易。

http://www.imagemagick.org/script/convert.php

Use the "-sepia-tone threshold" argument when converting.

转换时使用“-sepia-tone threshold”参数。

Strangely enough, the PerlMagick API doesn't seem to include a method for doing this directly:

奇怪的是,PerlMagick API似乎没有包含直接执行此操作的方法:

http://www.imagemagick.org/script/perl-magick.php

...and no reference to any Sepia method.

......并没有提到任何深褐色方法。

#4


Take a look at how it's implemented in the AForge.NET library, the C# code is here.

看看它是如何在AForge.NET库中实现的,C#代码就在这里。

The basics seem to be

基本似乎是

  • transform it to the YIQ color space
  • 将其转换为YIQ颜色空间

  • modify it
  • transform back to RGB
  • 转换回RGB

The full alrogithm is in the source code, plus the RGB -> YIQ and YIQ -> RGB transformations are explained.

完整的alrogithm在源代码中,加上RGB - > YIQ和YIQ - > RGB转换。