0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C#】画像をグレースケールに変換する

Posted at

はじめに

以前に画像の差分を得るプログラムを作成しましたが、今回は既存のカラー画像をグレースケールに変換するコードを作ってみました。

作成したコード

  • 今回は「0~255で表されるR/G/Bの値の平均値を使って画像をグレースケールにする」という最も単純な方法(というよりも独自の方法?)にしました。
  • カラーをグレースケールに変換するには本来複雑な計算式が必要なのですが、今回は便宜的にR/G/Bの平均値を用いました。
  • 何となくグレーになっていればOK...ということにしておきました。
GrayConverter.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpStudy.Image
{
    /// <summary>
    /// 画像をグレースケールに変換するクラス。
    /// </summary>
    public class GrayConverter
    {
        /// <summary>
        /// 指定した画像をグレースケールに変換する。
        /// </summary>
        /// <param name="bmpPath">変換する画像のファイルパス。</param>
        /// <param name="path">グレースケール画像の保存先となるファイルパス。</param>
        public void ToGrayScale(string bmpPath, string path = @".\gray_image.png")
        {
            // 画像を比較する際に「大きい方の画像」のサイズに合わせて比較する。
            Bitmap bmp = new Bitmap(bmpPath);
            int width = bmp.Width;
            int height = bmp.Height;

            Bitmap grayBmp = new Bitmap(width, height);         // グレースケールの画像。

            // 全ピクセルを1つずつグレースケールに変換する。
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    try
                    {
                        // NTSC規格などに準拠せず、RGB値の平均値をグレースケールに変える。
                        // https://dobon.net/vb/dotnet/graphics/grayscale.html
                        Color pixelColor = bmp.GetPixel(i, j);
                        byte grayScale = Convert.ToByte((pixelColor.R + pixelColor.G + pixelColor.B) / 3);
                        // RGBだけでなく、アルファ値もセットする。
                        Color grayColor = Color.FromArgb(pixelColor.A, grayScale, grayScale, grayScale);
                        grayBmp.SetPixel(i, j, grayColor);
                    }
                    catch
                    {
                        System.Console.Error.WriteLine("(" + i + "," + j + ")ピクセルでエラーが発生しました。");
                    }
                }
            }
            grayBmp.Save(path, ImageFormat.Png);
            return;
        }
    }
}

実行用のテストコード

GrayConverterTest.cs
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CSharpStudy.Image;

namespace CSharpStudyTest
{
    /// <summary>
    /// GrayConverterクラスのテストコード
    /// </summary>
    [TestClass]
    public class GrayConverterTest
    {
        private const string BITMAP1_PATH = @"C:\Users\NKOJIMA\source\repos\CSharpStudy\CSharpStudy\Image\cat1.png";
        private const string GRAY_IMG_PATH = @"C:\Users\NKOJIMA\source\repos\CSharpStudy\CSharpStudy\Image\gray_image.png";

        [TestMethod]
        public void TestMethod1()
        {
            GrayConverter gConverter = new GrayConverter();
            gConverter.ToGrayScale(BITMAP1_PATH, GRAY_IMG_PATH);
        }
    }
}

処理結果

  • 入力画像を一応グレースケールで出力させることには成功しました。
処理に使った画像 処理結果の画像
cat1.png gray_image.png

参考URL

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?