LoginSignup
0
2

More than 5 years have passed since last update.

質量分析データを一次元配列画像にしてみる

Posted at

質量分析で取得したデータを何とか画像にするC#プログラム的な何か

[m/z , relative intensity]形式のCSVに持ってきたものを
一次元の画像にするだけのプログラム。
(x,y散布図を一次元にするプログラム)

hayatoy様の
http://qiita.com/hayatoy/items/0ec3f3da680ae192ddc7
みたいな画像が必要だったけど、作れるプログラムが転がってなかったので泣きながらいろいろなサイト様を参考に作ったもの。
Fileをクローズしていなかったりいろいろとひどいけど備忘録として。
諸々の事情によりD&D機能が必須だったため、C#でtextBoxを2つ使っている。いろいろとひどいのは勘弁してください。

作った画像は
Neural Network Consoleで動作したので、ほかに利用方法があるかも。

注意:OpenCvScharpが必要です

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using OpenCvSharp;


  namespace mzimage
{
       public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    private void textBox1_TextChanged(object sender, EventArgs e)
        {
          string textValue = textBox1.Text;
            textBox2.Text = "";
            StreamReader sr = new StreamReader(textValue);

            var img = new Mat(1, 1000, MatType.CV_8UC3, new Scalar(0, 0, 0));

            try
            {
                int count = 0;
                while (sr.EndOfStream == false)

                {
                    string line = sr.ReadLine();
                    string[] fields = line.Split(',');

                    var dx = Convert.ToDecimal(fields[0]);
                    var dy = Convert.ToDecimal(fields[1]);
                    var x = Convert.ToInt32(dx);
                    var y = Convert.ToInt32(dy);
                    var stry = Convert.ToString(y);

                    System.Drawing.Color bitdata = System.Drawing.ColorTranslator.FromHtml(stry);

                    while (x < 1000) {

                        Vec3b pix = img.At<Vec3b>(1, x);
                      pix[0] = Convert.ToByte(y*2);
                      pix[1] = Convert.ToByte(y*2);
                      pix[2] = Convert.ToByte(y*2);
                        img.Set<Vec3b>(0, x, pix);
                        textBox2.AppendText(Convert.ToString(count));
                        textBox2.AppendText(Convert.ToString(bitdata));
                        textBox2.AppendText(Convert.ToString(pix[2]));
                        textBox2.AppendText("\r\n");
                        count++;
                        break;
                    }
                }

                try
                {
                    var name = DateTime.Now.ToString("yyyyMMddhhss");
                    var savepath = string.Format(@"適切なパス\{0}.png", name);


                    Cv2.ImShow("test", img);
                    Cv2.ImWrite(savepath, img);
                }
                catch { }
            }



            catch
            {
               //finallyで閉じるべき
            }

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

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A)
                textBox2.SelectAll();
        }

        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] filename = (string[])e.Data.GetData(DataFormats.FileDrop, false);
          textBox1.Text = filename[0];
        }
    } 
}

今後の展望1

今回はグレースケールを作成しているが、
pixが最大FFFFFFまで表現できることに気が付いたので、
relative intensityではなくabsolute intensityで表現できそう。

今後の展望2

今回は整数質量で最大値を1000までにしている。
高分解能MSで取得した場合、予めDecimal dxを10倍~にして
xの最大値を大きくすれば対処できそう。

textBox2について

動作確認用なので、textBox2周りはなくしても問題ない。

pix[0] = Convert.ToByte(y*2)...について

relative intensityは0-100までのdouble もしくはdecimal(実際にはint型に変換済み)なので2倍してちょっとでも差が出るように頑張っているだけ。

0
2
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
2