ここから本文です
OneSegの長寿と健康を願うひとりがき
新人間主義(新自由主義に対抗して); 法人(グローバル企業)に人権はない

書庫C# よろけて2歩目

記事検索
検索

全3ページ

[1] [2] [3]

[ 次のページ ]

Image Composer

前回の RGBAlphaImageChanger を利用して
イメージ 1
といった風のFormをこさえて
2つのJPGを合成してみました。
ーーーーー
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RGBAlphaImageChanger;
namespace ImageEtude
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void image01ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Image im = Image.FromFile(openFileDialog1.FileName);
                this.pictureBox1.Image = im;
            }
        }
        private void image02ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Image im = Image.FromFile(openFileDialog1.FileName);
                this.pictureBox2.Image = im;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            RGBA.RGBAValue myrgba01 = new RGBA.RGBAValue(
                (float)nUD01R.Value,
                (float)nUD01G.Value,
                (float)nUD01B.Value,
                (float)nUD01A.Value
                );
            RGBA.RGBAValue myrgba02 = new RGBA.RGBAValue(
               (float)nUD02R.Value,
               (float)nUD02G.Value,
               (float)nUD02B.Value,
               (float)nUD02A.Value
               );
            this.pictureBox3.Image =
            RGBA.BindRGBAImages(pictureBox1.Image, myrgba01,
                                pictureBox2.Image, myrgba02);
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox3.Image.Save(saveFileDialog1.FileName);
            }
        }
    }
}
ーーーーー
画像の重ね合わせの必要があってちょっと調べたが
みなさん苦労しておられるらしい。
 
GitHubGist  さんの
workaholism  / CreateAlphaImage.cs  に
public static Image CreateAlphaImage(Image sourceImage)
という役に立ちそうなプロシージャがあった。

リード開発メモ さんにも
public static class ImageHelper
public static Bitmap CreateAlphaImage(Image srcImage, float alpha)
という よくできたClass があった。
 
これらの良いとこ取りでチョット作ってみた。
うまく動くかな?
ーーーーー
using System.Drawing;
using System.Drawing.Imaging;

namespace RGBAlphaImageChanger
{
    public class RGBA
    {
        public struct RGBAValue
        {
            public float r, g, b, a;
            public RGBAValue(float p1, float p2, float p3, float p4)
            {
                r = p1; g = p2; b = p3; a = p4;
            }
        }
        public static Image CreateRGBAlphaImage(Image sourceImage, RGBAValue myrgba)
        {
            int imageWidth = sourceImage.Width;
            int imageHeight = sourceImage.Height;
            // 新しいビットマップを用意
            Bitmap alphaImage = new Bitmap(imageWidth, imageHeight);
            using (Graphics g = Graphics.FromImage(alphaImage))
            {
                // ColorMatrixオブジェクトの作成
                ColorMatrix cm = new ColorMatrix();
                // ColorMatrixの行列の値を変更
                cm.Matrix00 = myrgba.r;
                cm.Matrix11 = myrgba.g;
                cm.Matrix22 = myrgba.b;
                cm.Matrix33 = myrgba.a;
                cm.Matrix44 = 1;
                // ImageAttributesオブジェクトの作成
                ImageAttributes ia = new ImageAttributes();
                // ColorMatrixを設定
                ia.SetColorMatrix(cm);
                // ImageAttributesを使用して画像を描画
                g.DrawImage(sourceImage,
                            new Rectangle(0, 0, imageWidth, imageHeight),
                            0,
                            0,
                            imageWidth,
                            imageHeight,
                            GraphicsUnit.Pixel,
                            ia);
            }
            return alphaImage;
        }

        public static Image BindRGBAImages(Image Img01,RGBAValue rgba01,Image Img02,RGBAValue rgba02)
        {
            // 原画のRGBA を変換
            Image alphaImage01 = CreateRGBAlphaImage(Img01, rgba01);
            Image alphaImage02 = CreateRGBAlphaImage(Img02, rgba02);
            //戻り画像用のBitmapを作成
            Bitmap bindimage = new Bitmap(alphaImage01);
            Graphics g = Graphics.FromImage(alphaImage01);
            g.DrawImage(alphaImage02, 0, 0, Img02.Width, Img02.Height);
            return bindimage;
        }
    }
}
のUnsafe版です。
配列の添え字を間違えると 大変危険です。 C#では不思議なことに数分で戻って来てくれるようです。
オリジナルにあった最小二乗法も加えてみたかったのですが文字数が足りず略しました。



 unsafe public class ARUnsafe
    {
        public const int MAXENTROPY = 0;
        public const int LEASTSQUARES = 1;
        //
        public int AutoRegression(
            double* inputseries,
            int length,
            int degree,
            double* coefficients,
            int method)
        {
            int i;
            try
            {
                /* Allocate space for working variables */
                double* h = stackalloc double[degree + 1];
                double* g = stackalloc double[degree + 2]; // Used by mempar()
                double* per = stackalloc double[length + 1];
                double* pef = stackalloc double[length + 1]; // Used by mempar()
                double** ar= stackalloc double*[degree+1];
                for (i = 0; i < degree+1; i++)
                {
                    double* rar = stackalloc double[degree+1];
                    ar[i] = rar;
                }
                /* Perform the appropriate AR calculation */
                if (method == MAXENTROPY)
                {
                    if (1 != ARMaxEntropy(inputseries, length, degree, ar, per, pef, h, g))
                    {
                        return (-1);
                    }
                    for (i = 1; i <= degree; i++)
                        coefficients[i - 1] = -ar[degree][i];
                }
                else if (method == LEASTSQUARES)
                {
                    if (1 != ARLeastSquare(inputseries, length, degree, coefficients))
                    {
                        return (-1);
                    }
                }
                else
                {
                    return (-1);
                }
            }
            catch { }
            return (1);
        }
        /*  
           Previously called mempar()
           Originally in FORTRAN, hence the array offsets of 1, Yuk.
           Original code from Kay, 1988, appendix 8D.
  
           Perform Burg's Maximum Entropy AR parameter estimation
           outputting successive models en passant. Sourced from Alex Sergejew
 
           Two small changes made by NH in November 1998:
           tstarz.h no longer included, just say "typedef double REAL" instead
           Declare ar by "REAL **ar" instead of "REAL ar[MAXA][MAXA]
  
           Further "cleaning" by Paul Bourke.....for personal style only.

        Converted to c# unsafe mode by H.Nagano.
        */
        int ARMaxEntropy(
           double* inputseries, int length, int degree, double** ar,
           double* per, double* pef, double* h, double* g)
        {
            int j, n, nn, jj;
            double sn, sd;
            double t1, t2;
            for (j = 1; j <= length; j++)
            {
                pef[j] = 0;
                per[j] = 0;
            }
            for (nn = 2; nn <= degree + 1; nn++)
            {
                n = nn - 2;
                sn = 0.0;
                sd = 0.0;
                jj = length - n - 1;
                for (j = 1; j <= jj; j++)
                {
                    t1 = inputseries[j + n] + pef[j];
                    t2 = inputseries[j - 1] + per[j];
                    sn -= 2.0 * t1 * t2;
                    sd += (t1 * t1) + (t2 * t2);
                }
                g[nn] = sn / sd;
                t1 = g[nn];
                if (n != 0)
                {
                    for (j = 2; j < nn; j++)
                        h[j] = g[j] + (t1 * g[n - j + 3]);
                    for (j = 2; j < nn; j++)
                        g[j] = h[j];
                    jj--;
                }
                for (j = 1; j <= jj; j++)
                {
                    per[j] += (t1 * pef[j]) + (t1 * inputseries[j + nn - 2]);
                    pef[j] = pef[j + 1] + (t1 * per[j + 1]) + (t1 * inputseries[j]);
                }
                for (j = 2; j <= nn; j++)
                    //ar[nn - 1 + (j - 1) * (degree + 1)] = g[j];
                    ar[nn - 1][ j - 1] = g[j];
            }
            return (1);
        }
        /*
      Least squares method
      Original from Rainer Hegger, Last modified: Aug 13th, 1998
      Modified (for personal style and context) by Paul Bourke
    */
        int ARLeastSquare(
           double* inputseries,
           int length,
           int degree,
           double* coefficients)
        {
            int i, j, k, hj, hi;
            double** mat = stackalloc double*[degree];
            for (i = 0; i < degree; i++)
            {
               double* rmat= stackalloc double[degree];
               mat[i] = rmat;
            }
            ///
         }        
    }
だいぶ前に書庫(FOLDER)
  RはExcelからRStudio
でエクセル上でRを使いった
などを 紹介しましたが、
今回はC#上で実装してみました。しかも最大エントロピー法!なんとも速い!
/*  
   Previously called mempar()
   Originally in FORTRAN, hence the array offsets of 1, Yuk.
   Original code from Kay, 1988, appendix 8D.
  
   Perform Burg's Maximum Entropy AR parameter estimation
   outputting successive models en passant. Sourced from Alex Sergejew
 
   Two small changes made by NH in November 1998:
   tstarz.h no longer included, just say "typedef double REAL" instead
   Declare ar by "REAL **ar" instead of "REAL ar[MAXA][MAXA]
  
   Further "cleaning" by Paul Bourke.....for personal style only.
*/
と注釈が入ったC用のソースを C#ように改編しました。
コモンランゲッジランタイム(CLR)で動くよう ポインタを使わずにSafe版にしてみました。
上の注釈で REAL **ar とあるところを double[,,] ar としたところがミソでしょうか?
次回はUnsafe版を紹介します。


public class AR
    {
        public int AutoRegression(
            double[] inputseries,
            int length,
            int degree,
            double[] coefficients)
        {
            int i, t;
            try
            {
                double[] w = new double[length];  // Input series 
                double[] h = new double[degree + 1];
                double[] g = new double[degree + 2]; // Used by mempar()
                double[] per = new double[length + 1];
                double[] pef = new double[length + 1]; // Used by mempar()
                double[,] ar = new double[degree + 1, degree + 1]; // AR coefficients, all degrees
                for (t = 0; t < length; t++) w[t] = inputseries[t];
                /* Perform the appropriate AR calculation */
                if (1 != ARMaxEntropy(w, length, degree, ar, per, pef, h, g))
                {
                    return (-1);
                }
                for (i = 0; i < degree; i++)
                    coefficients[i] = -ar[degree, i];
                return (1);
            }
            catch
            {
                // 
            }
            return (0);
        }
        /*  
           Previously called mempar()
           Originally in FORTRAN, hence the array offsets of 1, Yuk.
           Original code from Kay, 1988, appendix 8D.
  
           Perform Burg's Maximum Entropy AR parameter estimation
           outputting successive models en passant. Sourced from Alex Sergejew
 
           Two small changes made by NH in November 1998:
           tstarz.h no longer included, just say "typedef double REAL" instead
           Declare ar by "REAL **ar" instead of "REAL ar[MAXA][MAXA]
           For safe compile all ponters are changed to array
  
           Further "cleaning" by Paul Bourke.....for personal style only.
           Converted to zero-based arrays by Paul Sanders, June 2007.
          
          This is safe mode of c# by OneSeg.
        */
        int ARMaxEntropy(
           double[] inputseries, int length, int degree, double[,] ar,
           double[] per, double[] pef, double[] h, double[] g)
        {
            int n;
            double t1, t2;
            for (n = 1; n <= degree; n++)
            {
                double sn = 0.0;
                double sd = 0.0;
                int j;
                int jj = length - n;
                for (j = 0; j < jj; j++)
                {
                    t1 = inputseries[j + n] + pef[j];
                    t2 = inputseries[j] + per[j];
                    sn -= 2.0 * t1 * t2;
                    sd += (t1 * t1) + (t2 * t2);
                }
                g[n] = sn / sd;
                t1 = g[n];
                if (n != 1)
                {
                    for (j = 1; j < n; j++) h[j] = g[j] + t1 * g[n - j];
                    for (j = 1; j < n; j++) g[j] = h[j];
                    jj--;
                }
                for (j = 0; j < jj; j++)
                {
                    per[j] += t1 * pef[j] + t1 * inputseries[j + n];
                    pef[j] = pef[j + 1] + t1 * per[j + 1] + t1 * inputseries[j + 1];
                }
                for (j = 0; j < n; j++) ar[n, j] = g[j + 1];
            }
            return (1);
        }
    }


 

Linq To Twitter Form版

LinqToTwitter 結構便利ですが コンソール版では リンクにも飛べないしなあ・・・
という事で Form版を作成。 
イメージ 1
 
テキストからKeywordsも選択できるし (textBox1_KeyDown)
LinkからURLホームページに飛ぶこと  (richTextBox1_LinkClicked)
も可能です。
 
--------------------------------------------------------------------------------------------
    public partial class frmL2T : Form
    {
        bool richTextcleared = true;
        public frmL2T()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = "Linq to twitter";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            WriteDown();
        }
        private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }
        private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            if (richTextcleared == false) this.textBox1.Text = richTextBox1.SelectedText;
        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) WriteDown();
        }
        private void WriteDown()
        {
            if (textBox1.Text != "")
            {
                richTextcleared = true;
                this.richTextBox1.Clear();
                //
                var twitterCtx = new TwitterContext();
                var queryResults =
                from search in twitterCtx.Search
                where search.Type == SearchType.Search &&
                        search.Query == this.textBox1.Text
                select search;
                Search srch = queryResults.Single();
                this.richTextBox1.AppendText(srch.QueryResult);
                srch.Results.ForEach(entry =>
                this.richTextBox1.AppendText(entry.ID + entry.Source + "\n"
                + entry.Text + "\n" + "\n"));
                //
                textBox1.Focus();
                richTextcleared = false;
            }
        }
    }
 

 

全3ページ

[1] [2] [3]

[ 次のページ ]

One_Seg  (一弧)
One_Seg (一弧)
男性 / O型
人気度
Yahoo!ブログヘルプ - ブログ人気度について
友だち(20)
  • helikiti娘[へりきち娘」
  • michi
  • mas*k*12*1momo
  • 取締られ役代表
  • 高宮たかし
  • 紫苑
友だち一覧
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

スマートフォンで見る

モバイル版Yahoo!ブログにアクセス!

スマートフォン版Yahoo!ブログにアクセス!

よしもとブログランキング

もっと見る
本文はここまでですこのページの先頭へ

[PR]お得情報

いまならもらえる!ウィスパーうすさら
薄いしモレを防ぐ尿ケアパッド
話題の新製品を10,000名様にプレゼント
ふるさと納税サイト『さとふる』
実質2000円で特産品がお手元に
11/30までキャンペーン実施中!
数量限定!イオンおまとめ企画
「無料お試しクーポン」か
「値引きクーポン」が必ず当たる!
コンタクトレンズで遠近両用?
「2WEEKメニコンプレミオ遠近両用」
無料モニター募集中!
いまならもらえる!ウィスパーWガード
薄いしモレを防ぐパンティライナー
話題の新製品を10,000名様にプレゼント
お肉、魚介、お米、おせちまで
おすすめ特産品がランキングで選べる
ふるさと納税サイト『さとふる』

その他のキャンペーン

みんなの更新記事