※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

C#で作った何でも虹色にしてしまうフィルタ。
FormにPictureBox1とボタンをおいて、
画像の設定をあなたのお手持ちの画像に変えると使用可能。

コード中のpict_RGB_And_Deviation(100,100, 100,50, 50, 50) は前半3つがフィルタ適用後のイラストの色の平均値、後ろ3つがRGB値の標準偏差を指定できる、偏差の値を大きくすると絵が虹色風になる。



下記コードを別の数式に変えると、丁寧な色鉛筆風フィルタになったり、絵が虹色風になるフィルタになったりと色々いじくると楽しい。

                   tR = 2* (C2.R - aveDev2.aveR) / aveDev2.devR;
                   tR = tR*tR * aveDev1.devR + aveDev1.aveR;
                   
                   tG = 1* (C2.G - aveDev2.aveG) / aveDev2.devG;
                   tG = tG *tG  * aveDev1.devG + aveDev1.aveG;

                   tB = 0.5* (C2.B - aveDev2.aveB) / aveDev2.devB;
                   tB = tB*tB * aveDev1.devB + aveDev1.aveB;



フィルタ使用例、黄金色版。
写真は
http://pocket1236.seesaa.net/

http://jp.123rf.com/photo_2996333.html
をしよう。



発展

フィルタを適用すると絵が乱雑になりやすいので、隣り合った近傍点との平均を取り、色合いが綺麗になるよう工夫するのもいいかもしれない。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
public delegate void ExampleCallback(Bitmap outBit,PictureBox p1);

namespace WindowsFormsApplication2
{
   public partial class Form1 : Form
   {
       int tc = 0;
       //Bitmap lastB;
       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {
       }

       private void button1_Click(object sender, EventArgs e)
       {
           
           
           Bitmap b2 = new Bitmap(pictureBox1.Image);
           


           //pictureBox1.Image  =b1;
           
           pictFilter PTC1 = new WindowsFormsApplication2.pictFilter();
           pict_RGB_And_Deviation aveDev = new pict_RGB_And_Deviation(100,100, 100,50, 50, 50);
           //上記メソッドは、絵にフィルタを掛けた後のRGBの


           pictureBox1.Image=PTC1.standard_deviation_filter(aveDev, b2); 
           //pictureBox1.Image  =PTC1.lineFilter(b1);
           //b1=PTC1.lineFilter(b1);
           
           /*pictFilter PTC = new WindowsFormsApplication2.pictFilter(PTC1.lineFilter (b1), new ExampleCallback(ResultCallback),pictureBox1);
           Thread t1 =new Thread(new ThreadStart(PTC.cercleChange));
           t1.Start();
           t1.Join();
           */
           //pictFilter PTC = new WindowsFormsApplication2.pictFilter();
           //pictureBox1.Image = PTC.lineFilter(b1);
           //pictureBox1.Image = PTC.flatFilter2( PTC.cercleChange      (b1)) ;
           //b1.Dispose();
       }
       public static void ResultCallback(Bitmap outBit,PictureBox p1)
       {
           p1.Image = outBit;
       }


       private void pictureBox1_Click(object sender, EventArgs e)
       {
           tc++;
           if (tc == 1)
           {
               pictureBox1.Image = Image.FromFile("D:/色々/中高生向け戦争小説 ギガンダム討伐/earth1.jpg");
           }
           else if (tc == 2)
           {
               pictureBox1.Image = Image.FromFile("D:/色々/中高生向け戦争小説 ギガンダム討伐/P1020179.JPG");
           }
           else if (tc == 3)
           {
               pictureBox1.Image = Image.FromFile("D:/色々/中高生向け戦争小説 ギガンダム討伐/はばたきフクロウ昼.JPG");
           }
               pictureBox1.Height = 1200;
           pictureBox1.Width  = 1000;
           pictureBox1.Top = 0;
           pictureBox1.Left = 0; 
           
       }

   }
   class pictFilter
   {
       Random rnd = new Random();
       Bitmap pictOut = null;
       int h;//絵の高さ
       int w;//絵の横幅
       Bitmap pictIn;
       ExampleCallback callback;
       PictureBox lastPict;
       public pictFilter()
       {
           h = 0;
           w = 0;
       }

       public Bitmap standard_deviation_filter(pict_RGB_And_Deviation aveDev1, Bitmap b2)
       {
           Color C2;
           Bitmap outBit;
           double tR, tG, tB;
           int tR1, tG1, tB1;
           pict_RGB_And_Deviation aveDev2 = new pict_RGB_And_Deviation(b2);

           outBit = new Bitmap(b2.Width, b2.Height);

           for (int i = 0; i < b2.Width; i++)
           {
               for (int j = 0; j < b2.Height; j++)
               {
                   C2 = b2.GetPixel(i, j);
                   //tR = ((C2.R - aveDev2.aveR) / aveDev2.devR) * aveDev1.devR + aveDev1.aveR;
                   //tG = (C2.G - aveDev2.aveG) / aveDev2.devG * aveDev1.devG + aveDev1.aveG;
                   //tB = (C2.B - aveDev2.aveB) / aveDev2.devB * aveDev1.devB + aveDev1.aveB;
                   //tR = Math.Log(5    *    ((C2.R - aveDev2.aveR) / aveDev2.devR)) * aveDev1.devR + aveDev1.aveR;
                   //tG = Math.Log(5    *    ((C2.G - aveDev2.aveG) / aveDev2.devG)) * aveDev1.devG + aveDev1.aveG;
                   //tB = Math.Log(5    *    ((C2.B - aveDev2.aveB) / aveDev2.devB)) * aveDev1.devB + aveDev1.aveB;

                   tR = 2* (C2.R - aveDev2.aveR) / aveDev2.devR;
                   tR = tR*tR * aveDev1.devR + aveDev1.aveR;
                   
                   tG = 1* (C2.G - aveDev2.aveG) / aveDev2.devG;
                   tG = tG *tG  * aveDev1.devG + aveDev1.aveG;

                   tB = 0.5* (C2.B - aveDev2.aveB) / aveDev2.devB;
                   tB = tB*tB * aveDev1.devB + aveDev1.aveB;
                   
                   tR1 = cutNum(0, 255, (int)Math.Floor(tR));
                   tG1 = cutNum(0, 255, (int)Math.Floor(tG));
                   tB1 = cutNum(0, 255, (int)Math.Floor(tB));
                   outBit.SetPixel(i, j, Color.FromArgb(tR1, tG1, tB1));

               }
           }
           //生物の遺伝子構造解析をして病気対策をするとき、遺伝子コードのパターンマッチにパソコンを使うとか、本当はそういうプログラム書けたらなと思ったり、

           return outBit;
       }

       public Bitmap standard_deviation_filter(Bitmap b1,Bitmap b2){
           Color C2;
           Bitmap outBit;
           double tR, tG, tB;
           int tR1,tG1,tB1;
           pict_RGB_And_Deviation aveDev1 = new pict_RGB_And_Deviation(b1);
           pict_RGB_And_Deviation aveDev2 = new pict_RGB_And_Deviation(b2);
           
           outBit = new Bitmap(b2.Width, b2.Height);

           for (int i = 0; i < b2.Width; i++)
           {
               for (int j = 0; j < b2.Height; j++)
               {
                   C2=b2.GetPixel(i,j);
                   //tR = ((C2.R - aveDev2.aveR) / aveDev2.devR) * aveDev1.devR + aveDev1.aveR;
                   //tG = (C2.G - aveDev2.aveG) / aveDev2.devG * aveDev1.devG + aveDev1.aveG;
                   //tB = (C2.B - aveDev2.aveB) / aveDev2.devB * aveDev1.devB + aveDev1.aveB;
                   tR = ((C2.R - aveDev2.aveR) / aveDev2.devR) * ((C2.R - aveDev2.aveR) / aveDev2.devR)  * aveDev1.devR + aveDev1.aveR;
                   tG = ((C2.G - aveDev2.aveG) / aveDev2.devG) * ((C2.G - aveDev2.aveG) / aveDev2.devG)  * aveDev1.devG + aveDev1.aveG;
                   tB = ((C2.B - aveDev2.aveB) / aveDev2.devB) * ((C2.B - aveDev2.aveB) / aveDev2.devB)  * aveDev1.devB + aveDev1.aveB;

                   //tR =((C2.R - aveDev2.aveR) / aveDev2.devR) *((C2.R - aveDev2.aveR) / aveDev2.devR) * aveDev1.devR + aveDev1.aveR;
                   //tG =((C2.G - aveDev2.aveG) / aveDev2.devG) *((C2.G - aveDev2.aveG) / aveDev2.devG) * aveDev1.devG + aveDev1.aveG;
                   //tB =((C2.B - aveDev2.aveB) / aveDev2.devB) *((C2.B - aveDev2.aveB) / aveDev2.devB) * aveDev1.devB + aveDev1.aveB;
                   
                   tR1 =cutNum(0, 255,(int) Math.Floor(tR));
                   tG1 =cutNum(0, 255,(int) Math.Floor(tG));
                   tB1 =cutNum(0, 255,(int) Math.Floor(tB));
                   outBit.SetPixel(i,j,Color.FromArgb(tR1,tG1,tB1));

               }
           }
           //生物の遺伝子構造解析をして病気対策をするとき、遺伝子コードのパターンマッチにパソコンを使うとか、本当はそういうプログラム書けたらなと思ったり、

               return outBit; 
       }






       public pictFilter(Bitmap b1, ExampleCallback callbackDelegate,PictureBox p1)
       {
           this.pictIn=b1; 
           h = 0;
           w = 0;
           callback = callbackDelegate;
           lastPict = p1; 
       }




       private int cutNum(int min, int max, int t)
       {
           if (t < min)
           {
               t = min;
           }
           if (t > max)
           {
               t = max;
           }
           return t;
       }

   }
   public class pict_RGB_And_Deviation
   {
       public double aveR, aveG, aveB;
       public double devR, devG, devB;
       public pict_RGB_And_Deviation(double inAveR, double inAveG, double inAveB, double inDevR, double inDevG, double inDevB)
       {
           aveR = inAveR;
           aveG = inAveG;
           aveB = inAveB;
           devR = inDevR;
           devG = inDevG;
           devB = inDevB;
       }
       public pict_RGB_And_Deviation(Bitmap b1)
       {
           Color C1;
           double countPictSize;
           for (int i = 0; i < b1.Width; i++)
           {
               for (int j = 0; j < b1.Height; j++)
               {
                   C1 = b1.GetPixel(i, j);
                   aveR += (double)C1.R;
                   aveG += (double)C1.G;
                   aveB += (double)C1.B;
               }
           }
           countPictSize = b1.Width * b1.Height;
           aveR /= countPictSize;
           aveG /= countPictSize;
           aveB /= countPictSize;
           for (int i = 0; i < b1.Width; i++)
           {
               for (int j = 0; j < b1.Height; j++)
               {
                   C1 = b1.GetPixel(i, j);
                   devR += ((double)C1.R - aveR) * ((double)C1.R - aveR);
                   devG += ((double)C1.G - aveG) * ((double)C1.G - aveG);
                   devB += ((double)C1.B - aveB) * ((double)C1.B - aveB);
               }
           }
           if (countPictSize > 1)
           {
               countPictSize--;
           }
           else
           {
               countPictSize = 1;
           }

           devR = Math.Sqrt(devR / countPictSize);
           devG = Math.Sqrt(devG / countPictSize);
           devB = Math.Sqrt(devB / countPictSize);
       }
   }

}