Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TemplateMatching3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void templateButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "BMP|*.BMP|すべてのファイル|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
this.templateTextBox.Text = ofd.FileName;
this.templatePictureBox.Image = LoadImage(this.templateTextBox.Text);
}
}
private void imagesButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true; // 複数の画像を選択できるようにする
ofd.Filter = "BMP|*.BMP|すべてのファイル|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
foreach (string s in ofd.FileNames)
{
this.imagesTextBox.Text += s + "\r\n";
}
}
}
//戻り値:違いの度合いを返す。
private long match(Bitmap image)
{
Color i;
Color t;
int width, height;
long sum = 0;
// 2枚の画像の左上をあわせて、重なっている部分のみマッチさせる方法で処理
width = image.Width < template.Width ? image.Width : template.Width;
height = image.Height < template.Height ? image.Height : template.Height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
i = image.GetPixel(x, y);
t = template.GetPixel(x, y);
sum += Math.Abs(i.R - t.R) + Math.Abs(i.G - t.G) + Math.Abs(i.B - t.B);
}
}
return sum;
}
private void goButton_Click(object sender, EventArgs e)
{
this.outLabel.Text = "お待ちください\r\nイメージ読込中";
this.outLabel.Update();
template = LoadImage(this.templateTextBox.Text);
if (template == null)
{
this.outLabel.Text = "テンプレ画像エラー";
this.outLabel.Update();
return;
}
this.templatePictureBox.Image = template;
string[] files = this.imagesTextBox.Text.Split('\n');
int filesIndex = 0;
int imagesIndex = 0;
images.Initialize();
while (imagesIndex < maxImage && filesIndex < files.Length)
{
images[imagesIndex] = LoadImage(files[filesIndex]);
if (images[imagesIndex] == null)
{
filesIndex++;
continue;
}/*
try
{
//MessageBox.Show(files[filesIndex]); // デバッグ用
images[imagesIndex] = new Bitmap(files[filesIndex]);
}
catch
{
filesIndex++;
continue;
}*/
filesIndex++;
imagesIndex++;
}
imageNumber = imagesIndex;
this.outLabel.Text = "お待ちください\r\nマッチング中";
this.outLabel.Update();
long[] imagesDifference = new long[imageNumber];
for (int i = 0; i < imageNumber; i++)
{
imagesDifference[i] = match(images[i]);
}
// 低速のバブルソート。参考:http://ufcpp.net/study/algorithm/sort_bubble.html
for (int i = 0; i < imageNumber - 1; i++)
{
for (int j = imageNumber - 1; j > i; j--)
{
if (imagesDifference[j] < imagesDifference[j - 1])
{
Swap(ref imagesDifference[j], ref imagesDifference[j - 1]);
Swap(ref images[j], ref images[j - 1]);
}
}
}
if (imageNumber > 0)
{
this.pictureBox1.Image = images[0];
this.match1Label.Text = "マッチ1 :" + imagesDifference[0];
}
if (imageNumber > 1)
{
this.pictureBox2.Image = images[1];
this.match2Label.Text = "マッチ2 :" + imagesDifference[1];
}
if (imageNumber > 2)
{
this.pictureBox3.Image = images[2];
this.match3Label.Text = "マッチ3 :" + imagesDifference[2];
}
this.outLabel.Text = "OK\n\r処理回数:" + imageNumber;
this.outLabel.Update();
}
static void Swap(ref long a, ref long b)
{
long w = a;
a = b;
b = w;
}
static void Swap(ref Bitmap a, ref Bitmap b)
{
Bitmap w = a;
a = b;
b = w;
}
static Bitmap LoadImage(string path)
{
try
{
return new Bitmap(path);
}
catch
{
return LoadImageFromNet(path);
}
}
static Bitmap LoadImageFromNet(string url)
{
// 参考:http://msdn.microsoft.com/ja-jp/library/z7ha67kw.aspx
try
{
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
return new Bitmap(responseStream);
}
catch { }
return null;
}
const int maxImage = 30;
Bitmap template;
Bitmap[] images = new Bitmap[maxImage];
int imageNumber;
}
}
Form1.Designer.cs
namespace TemplateMatching3
{
partial class Form1
{
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
/// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows フォーム デザイナーで生成されたコード
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.templateButton = new System.Windows.Forms.Button();
this.imagesButton = new System.Windows.Forms.Button();
this.templateTextBox = new System.Windows.Forms.TextBox();
this.imagesTextBox = new System.Windows.Forms.TextBox();
this.goButton = new System.Windows.Forms.Button();
this.outLabel = new System.Windows.Forms.Label();
this.templatePictureBox = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.match1Label = new System.Windows.Forms.Label();
this.match2Label = new System.Windows.Forms.Label();
this.match3Label = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.templatePictureBox)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(219, 179);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(110, 105);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.Location = new System.Drawing.Point(335, 179);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(110, 105);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox2.TabIndex = 0;
this.pictureBox2.TabStop = false;
//
// pictureBox3
//
this.pictureBox3.Location = new System.Drawing.Point(451, 179);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(110, 105);
this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox3.TabIndex = 0;
this.pictureBox3.TabStop = false;
//
// templateButton
//
this.templateButton.Location = new System.Drawing.Point(12, 12);
this.templateButton.Name = "templateButton";
this.templateButton.Size = new System.Drawing.Size(86, 37);
this.templateButton.TabIndex = 1;
this.templateButton.Text = "テンプレ選択";
this.templateButton.UseVisualStyleBackColor = true;
this.templateButton.Click += new System.EventHandler(this.templateButton_Click);
//
// imagesButton
//
this.imagesButton.Location = new System.Drawing.Point(12, 55);
this.imagesButton.Name = "imagesButton";
this.imagesButton.Size = new System.Drawing.Size(86, 37);
this.imagesButton.TabIndex = 1;
this.imagesButton.Text = "イメージ選択";
this.imagesButton.UseVisualStyleBackColor = true;
this.imagesButton.Click += new System.EventHandler(this.imagesButton_Click);
//
// templateTextBox
//
this.templateTextBox.Location = new System.Drawing.Point(104, 19);
this.templateTextBox.Name = "templateTextBox";
this.templateTextBox.Size = new System.Drawing.Size(457, 19);
this.templateTextBox.TabIndex = 2;
//
// imagesTextBox
//
this.imagesTextBox.AcceptsReturn = true;
this.imagesTextBox.Location = new System.Drawing.Point(105, 55);
this.imagesTextBox.Multiline = true;
this.imagesTextBox.Name = "imagesTextBox";
this.imagesTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.imagesTextBox.Size = new System.Drawing.Size(456, 84);
this.imagesTextBox.TabIndex = 2;
//
// goButton
//
this.goButton.Location = new System.Drawing.Point(15, 179);
this.goButton.Name = "goButton";
this.goButton.Size = new System.Drawing.Size(82, 29);
this.goButton.TabIndex = 3;
this.goButton.Text = "実行";
this.goButton.UseVisualStyleBackColor = true;
this.goButton.Click += new System.EventHandler(this.goButton_Click);
//
// outLabel
//
this.outLabel.AutoSize = true;
this.outLabel.Location = new System.Drawing.Point(13, 220);
this.outLabel.Name = "outLabel";
this.outLabel.Size = new System.Drawing.Size(0, 12);
this.outLabel.TabIndex = 4;
//
// templatePictureBox
//
this.templatePictureBox.Location = new System.Drawing.Point(103, 179);
this.templatePictureBox.Name = "templatePictureBox";
this.templatePictureBox.Size = new System.Drawing.Size(110, 105);
this.templatePictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.templatePictureBox.TabIndex = 0;
this.templatePictureBox.TabStop = false;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(109, 164);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(59, 12);
this.label1.TabIndex = 5;
this.label1.Text = "テンプレート";
//
// match1Label
//
this.match1Label.AutoSize = true;
this.match1Label.Location = new System.Drawing.Point(217, 164);
this.match1Label.Name = "match1Label";
this.match1Label.Size = new System.Drawing.Size(38, 12);
this.match1Label.TabIndex = 5;
this.match1Label.Text = "マッチ1";
//
// match2Label
//
this.match2Label.AutoSize = true;
this.match2Label.Location = new System.Drawing.Point(333, 164);
this.match2Label.Name = "match2Label";
this.match2Label.Size = new System.Drawing.Size(38, 12);
this.match2Label.TabIndex = 5;
this.match2Label.Text = "マッチ2";
//
// match3Label
//
this.match3Label.AutoSize = true;
this.match3Label.Location = new System.Drawing.Point(449, 164);
this.match3Label.Name = "match3Label";
this.match3Label.Size = new System.Drawing.Size(38, 12);
this.match3Label.TabIndex = 5;
this.match3Label.Text = "マッチ3";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(575, 293);
this.Controls.Add(this.match3Label);
this.Controls.Add(this.match2Label);
this.Controls.Add(this.match1Label);
this.Controls.Add(this.label1);
this.Controls.Add(this.outLabel);
this.Controls.Add(this.goButton);
this.Controls.Add(this.imagesTextBox);
this.Controls.Add(this.templateTextBox);
this.Controls.Add(this.imagesButton);
this.Controls.Add(this.templateButton);
this.Controls.Add(this.templatePictureBox);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.templatePictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.Button templateButton;
private System.Windows.Forms.Button imagesButton;
private System.Windows.Forms.TextBox templateTextBox;
private System.Windows.Forms.TextBox imagesTextBox;
private System.Windows.Forms.Button goButton;
private System.Windows.Forms.Label outLabel;
private System.Windows.Forms.PictureBox templatePictureBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label match1Label;
private System.Windows.Forms.Label match2Label;
private System.Windows.Forms.Label match3Label;
}
}