これは課題です。
ユーザーがグレースケール画像に対して特定のアクションを実行できるようにする非常に単純な画像処理プログラムを C# で作成しています。
私が現在抱えている問題は、トラックバーに複数のメソッドを渡す必要があることです。これにより、トラックバーは、呼び出されたメソッドのしきい値を変更できます。
Google で検索しましたが、この問題に関するヘルプはほとんどないようです。それで、これを行う方法について誰かアドバイスをいただけますか?
私が現在持っているコードはこれです。
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace DIP_START
{
public partial class Form1 : Form
{
public Bitmap original_image, proc_image;
public int[] bins = new int[256];
int max;
public Form1()
{
InitializeComponent();
original_image = null;
proc_image = null;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Form1_Paint(object sender, PaintEventArgs e)
{
if (original_image != null)
{
Graphics g = e.Graphics;
Rectangle r = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r);
}
}
// OPEN IMAGE FILE
/******************************************************************************************/
private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
{
// show the openFile dialog box
Graphics g = this.CreateGraphics();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
original_image = new Bitmap(openFileDialog1.FileName);
}
Rectangle r = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r);
}
// EXIT APPLICATION
/************************************************************************************/
private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Dispose();
Application.Exit();
}
/***********************************************************************************/
// EDGE DETECTION
/***********************************************************************************/
/***********************************************************************************/
// ROBERTS GRADIENT - WITH THRESHOLD
/***********************************************************************************/
private void withoutToolStripMenuItem_Click(object sender, System.EventArgs e)
{
Graphics g = this.CreateGraphics();
int width = original_image.Width - 1;
int height = original_image.Height - 1;
int threshold = thresholdBar1.Value;
thresholdBar1.Visible = true;
thresholdValueBox.Visible = true;
string thresholdBarVal = Convert.ToString(thresholdBar1.Value);
thresholdValueBox.Text = thresholdBarVal;
Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);
proc_image = original_image.Clone(r2, PixelFormat.Format8bppIndexed);
g.DrawImage(original_image, r2);
BitmapData bmDataOR = original_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
BitmapData bmDataPR = proc_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
int stride = bmDataOR.Stride;
// System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* o = (byte*)(void*)bmDataOR.Scan0;
byte* p = (byte*)(void*)bmDataPR.Scan0;
byte[] pix_val = new byte[4];
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
pix_val[0] = o[y * stride + x];
pix_val[1] = o[(y + 1) * stride + x + 1];
pix_val[2] = o[(y + 1) * stride + x];
pix_val[3] = o[y * stride + x + 1];
int new_pix_val = Math.Abs(pix_val[0] - pix_val[1]) + Math.Abs(pix_val[2] - pix_val[3]);
if (new_pix_val > threshold)
{
p[y * stride + x] = 255;
}
else
p[y * stride + x] = 0;
}
}
proc_image.UnlockBits(bmDataPR);
original_image.UnlockBits(bmDataOR);
g.DrawImage(proc_image, r);
//thresholdBar1.Visible = false;
}
}
/***********************************************************************************/
// NEIGHBOURHOOD AVERAGING WITHOUT THRESHOLD
/***********************************************************************************/
private void withoutThresholdToolStripMenuItem_Click(object sender, System.EventArgs e)
{
Graphics g = this.CreateGraphics();
int width = original_image.Width - 2;
int height = original_image.Height - 2;
int total_val = 0;
int avg = 0;
Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);
proc_image = original_image.Clone(r2, PixelFormat.Format8bppIndexed);
BitmapData bmDataOR = original_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
BitmapData bmDataPR = proc_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
int stride = bmDataOR.Stride;
unsafe
{
byte* o = (byte*)(void*)bmDataOR.Scan0;
byte* p = (byte*)(void*)bmDataPR.Scan0;
byte[] pix_val = new byte[9];
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
pix_val[0] = o[y * stride + x];
pix_val[1] = o[y * stride + x + 1];
pix_val[2] = o[y * stride + x + 2];
pix_val[3] = o[(y + 1) * stride + x];
pix_val[4] = o[(y + 1) * stride + x + 1];
pix_val[5] = o[(y + 1) * stride + x + 2];
pix_val[6] = o[(y + 2) * stride + x];
pix_val[7] = o[(y + 2) * stride + x + 1];
pix_val[8] = o[(y + 2) * stride + x + 2];
for (int i = 0; i < 9; ++i)
{
total_val = total_val + pix_val[i];
}
avg = total_val / 9;
p[y * stride + x] = (byte)avg;
total_val = 0;
}
}
proc_image.UnlockBits(bmDataPR);
original_image.UnlockBits(bmDataOR);
g.DrawImage(proc_image, r);
}
}
}
}
ご覧のとおり、トラックバーは Roberts Gradient にハードコーディングされており、トラックバーを採用するには Neighborhood Averaging も必要です。
トラックバーのイベント見出しの下のVisual Studioで、Roberts Gradientのメソッドを挿入しました