質問編集履歴
2
インシデントを修正しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,129 +25,133 @@
|
|
25
25
|
|
26
26
|
namespace 二値化とサイズ変更
|
27
27
|
{
|
28
|
-
public partial class Form1 : Form
|
28
|
+
public partial class Form1 : Form
|
29
|
-
{
|
29
|
+
{
|
30
|
-
public Form1()
|
30
|
+
public Form1()
|
31
|
-
{
|
31
|
+
{
|
32
|
-
InitializeComponent();
|
32
|
+
InitializeComponent();
|
33
|
-
}
|
33
|
+
}
|
34
34
|
|
35
|
-
Bitmap bmp1_im1;//資料画像(原画像)のビットマップ
|
35
|
+
Bitmap bmp1_im1;//資料画像(原画像)のビットマップ
|
36
|
-
Bitmap bmp1_im2;//資料画像(二値化後)のビットマップ
|
36
|
+
Bitmap bmp1_im2;//資料画像(二値化後)のビットマップ
|
37
|
-
Bitmap bmp1_im3;//資料画像(フィルタ処理後)のビットマップ
|
37
|
+
Bitmap bmp1_im3;//資料画像(フィルタ処理後)のビットマップ
|
38
|
+
|
39
|
+
PictureBox pb1, pb2, pb3;
|
38
40
|
|
41
|
+
private void Form1_Load(object sender, EventArgs e)
|
42
|
+
{
|
39
|
-
|
43
|
+
pb1 = pictureBox1;
|
44
|
+
pb2 = pictureBox2;
|
45
|
+
pb3 = pictureBox3;
|
40
46
|
|
41
|
-
private void Form1_Load(object sender, EventArgs e)
|
42
|
-
|
47
|
+
}
|
43
|
-
pb1 = pictureBox1;
|
44
|
-
pb2 = pictureBox2;
|
45
|
-
pb3 = pictureBox3;
|
46
48
|
|
49
|
+
private void File_Open_Click(object sender, EventArgs e)
|
50
|
+
{
|
51
|
+
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
52
|
+
{
|
53
|
+
Image im1 = Image.FromFile(openFileDialog1.FileName);
|
54
|
+
DrawPicture1(im1);
|
47
|
-
}
|
55
|
+
}
|
56
|
+
}
|
48
57
|
|
49
|
-
private void File_Open_Click(object sender, EventArgs e)
|
50
|
-
{
|
51
|
-
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
52
|
-
{
|
53
|
-
Image im1 = Image.FromFile(openFileDialog1.FileName);
|
54
|
-
DrawPicture1(im1)
|
58
|
+
private void DrawPicture1(Image im1)
|
55
|
-
}
|
56
|
-
}
|
57
59
|
|
60
|
+
{
|
61
|
+
double a;
|
58
|
-
|
62
|
+
int maxsize = 200;
|
59
63
|
|
64
|
+
if (im1.Width < maxsize && im1.Height < maxsize)
|
60
|
-
{
|
65
|
+
{
|
66
|
+
pb1.Width = pb1.Height = maxsize;
|
67
|
+
Rectangle rect1 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
68
|
+
Rectangle rect2 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
69
|
+
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
70
|
+
Graphics g = Graphics.FromImage(bmp1_im1);
|
71
|
+
g.DrawImage(im1, rect2, rect1, GraphicsUnit.Pixel);
|
61
|
-
|
72
|
+
g.Dispose();
|
73
|
+
pb1.Image = bmp1_im1;
|
74
|
+
}
|
75
|
+
else
|
76
|
+
{
|
77
|
+
if (im1.Width > im1.Height)
|
78
|
+
{
|
79
|
+
/*a = (double)maxsize / (double)im1.Width;*/
|
62
|
-
|
80
|
+
pb1.Width = maxsize;
|
81
|
+
pb1.Height = maxsize /* (int)(a * im1.Height)*/;
|
82
|
+
}
|
83
|
+
else
|
84
|
+
{
|
85
|
+
/*a = (double)maxsize / (double)im1.Height;*/
|
86
|
+
pb1.Height = maxsize;
|
87
|
+
pb1.Width =maxsize /*(int)(a * im1.Width)*/;
|
88
|
+
}
|
89
|
+
Rectangle rect3 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
90
|
+
Rectangle rect4 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
91
|
+
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
92
|
+
Graphics g = Graphics.FromImage(bmp1_im1);
|
93
|
+
g.DrawImage(im1, rect4, rect3, GraphicsUnit.Pixel);
|
94
|
+
g.Dispose();
|
95
|
+
pb1.Image = bmp1_im1;
|
96
|
+
}
|
97
|
+
}
|
63
98
|
|
64
|
-
|
99
|
+
private void File_SaveBMP_Click(object sender, EventArgs e)
|
65
|
-
{
|
100
|
+
{
|
66
|
-
|
101
|
+
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
|
67
|
-
Rectangle rect1 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
68
|
-
Rectangle rect2 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
69
|
-
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
70
|
-
|
102
|
+
{
|
71
|
-
|
103
|
+
pb2.Image.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
|
72
|
-
g.Dispose();
|
73
|
-
pb1.Image = bmp1_im1;
|
74
|
-
}
|
104
|
+
}
|
75
|
-
else
|
76
|
-
{
|
77
|
-
if (im1.Width > im1.Height)
|
78
|
-
{
|
79
|
-
/*a = (double)maxsize / (double)im1.Width;*/
|
80
|
-
pb1.Width = maxsize;
|
81
|
-
pb1.Height = maxsize /* (int)(a * im1.Height)*/;
|
82
|
-
}
|
105
|
+
}
|
83
|
-
else
|
84
|
-
{
|
85
|
-
/*a = (double)maxsize / (double)im1.Height;*/
|
86
|
-
pb1.Height = maxsize;
|
87
|
-
pb1.Width =maxsize /*(int)(a * im1.Width)*/;
|
88
|
-
}
|
89
|
-
Rectangle rect3 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
90
|
-
Rectangle rect4 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
91
|
-
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
92
|
-
Graphics g = Graphics.FromImage(bmp1_im1);
|
93
|
-
g.DrawImage(im1, rect4, rect3, GraphicsUnit.Pixel);
|
94
|
-
g.Dispose();
|
95
|
-
pb1.Image = bmp1_im1;
|
96
|
-
}
|
97
|
-
}
|
98
106
|
|
99
|
-
private void
|
107
|
+
private void P_GrayScale_Click(object sender, EventArgs e)
|
100
|
-
{
|
108
|
+
{
|
109
|
+
int i, j, nx, ny;
|
110
|
+
int gray;
|
111
|
+
Color col;
|
112
|
+
nx = pb3.Width;
|
113
|
+
ny = pb3.Height;
|
101
|
-
|
114
|
+
bmp1_im2 = new Bitmap (pb3.Image);
|
115
|
+
for (j = 0; j < ny; j++)
|
116
|
+
for (i = 0; i < nx; i++)
|
102
|
-
{
|
117
|
+
{
|
118
|
+
col = bmp1_im2.GetPixel(i, j);
|
119
|
+
if (col.R > 200 && col.B > 200 && col.G > 200) gray = 255;
|
120
|
+
else gray = 0;
|
103
|
-
|
121
|
+
bmp1_im2.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
|
104
|
-
}
|
122
|
+
}
|
123
|
+
pb2.Image = bmp1_im2;
|
105
|
-
}
|
124
|
+
}
|
125
|
+
|
126
|
+
|
127
|
+
private void gausian_Click(object sender, EventArgs e)
|
128
|
+
{
|
129
|
+
int nx, ny;
|
130
|
+
int gray;
|
106
131
|
|
107
|
-
private void P_GrayScale_Click(object sender, EventArgs e)
|
108
|
-
{
|
109
|
-
int i, j, nx, ny;
|
110
|
-
int gray;
|
111
|
-
Color col;
|
112
|
-
nx =
|
132
|
+
nx = pb1.Width;
|
113
|
-
ny =
|
133
|
+
ny = pb1.Height;
|
114
|
-
bmp1_im2 = new Bitmap (pb3.Image);
|
115
|
-
for (j = 0; j < ny; j++)
|
116
|
-
for (i = 0; i < nx; i++)
|
117
|
-
{
|
118
|
-
col = bmp1_im2.GetPixel(i, j);
|
119
|
-
if (col.R > 200 && col.B > 200 && col.G > 200) gray = 255;
|
120
|
-
else gray = 0;
|
121
|
-
bmp1_im2.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
|
122
|
-
}
|
123
|
-
pb2.Image = bmp1_im2;
|
124
|
-
}
|
125
134
|
|
135
|
+
// 画像の読み込み(グレースケールに変換)
|
126
|
-
|
136
|
+
bmp1_im3 = new Bitmap(pb1.Image);
|
127
|
-
{
|
128
|
-
int nx, ny;
|
129
|
-
int gray;
|
130
137
|
|
138
|
+
// フィルタ用のカーネル
|
139
|
+
const int kernelSize = 3; // カーネルサイズ
|
140
|
+
double[,] kernel = new double[kernelSize, kernelSize]{
|
141
|
+
{1/16.0, 1/8.0, 1/16.0},
|
142
|
+
{1/8.0, 1/4.0, 1/8.0},
|
143
|
+
{1/16.0, 1/8.0, 1/16.0}};
|
131
|
-
|
144
|
+
/* フィルタ処理
|
145
|
+
bmp1_im3 = Filter(pb3.Image, kernel);
|
146
|
+
*/
|
132
|
-
|
147
|
+
pb3.Image = bmp1_im3;
|
148
|
+
|
133
149
|
|
150
|
+
|
134
|
-
|
151
|
+
}
|
135
|
-
bmp1_im3 = new Bitmap(pb1.Image);
|
136
152
|
|
137
|
-
// フィルタ用のカーネル
|
138
|
-
const int kernelSize = 3; // カーネルサイズ
|
139
|
-
double[,] kernel = new double[kernelSize, kernelSize]{
|
140
|
-
{1/16.0, 1/8.0, 1/16.0},
|
141
|
-
{1/8.0, 1/4.0, 1/8.0},
|
142
|
-
{1/16.0, 1/8.0, 1/16.0}};
|
143
|
-
/* フィルタ処理
|
144
|
-
bmp1_im3 = Filter(pb3.Image, kernel); ←ここの部分です
|
145
|
-
|
153
|
+
}
|
146
|
-
pb3.Image = bmp1_im3;
|
147
|
-
|
148
154
|
}
|
149
|
-
}
|
150
|
-
}
|
151
155
|
```
|
152
156
|
|
153
157
|
### 試したこと
|
1
Markdownを利用するように言われたので、そこを修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,8 +1,17 @@
|
|
1
|
-
|
1
|
+
### 前提・実現したいこと
|
2
2
|
|
3
|
-
C#で画像処理
|
3
|
+
C#で元画像をガウシアンフィルタでフィルタ処理した後に、二値化するという作業をしています。
|
4
|
-
下の
|
4
|
+
フィルタ機能を実装中に以下のエラーメッセージが発生しました。
|
5
5
|
|
6
|
+
### 発生している問題・エラーメッセージ
|
7
|
+
|
8
|
+
```
|
9
|
+
名前'Filter'は現在のコンテキスト内に存在しません。
|
10
|
+
```
|
11
|
+
|
12
|
+
### 該当のソースコード
|
13
|
+
|
14
|
+
```C#
|
6
15
|
using System;
|
7
16
|
using System.Collections.Generic;
|
8
17
|
using System.ComponentModel;
|
@@ -16,134 +25,136 @@
|
|
16
25
|
|
17
26
|
namespace 二値化とサイズ変更
|
18
27
|
{
|
19
|
-
|
28
|
+
public partial class Form1 : Form
|
20
|
-
|
29
|
+
{
|
21
|
-
|
30
|
+
public Form1()
|
22
|
-
|
31
|
+
{
|
23
|
-
|
32
|
+
InitializeComponent();
|
24
|
-
|
33
|
+
}
|
25
34
|
|
26
|
-
|
35
|
+
Bitmap bmp1_im1;//資料画像(原画像)のビットマップ
|
27
|
-
|
36
|
+
Bitmap bmp1_im2;//資料画像(二値化後)のビットマップ
|
28
|
-
|
37
|
+
Bitmap bmp1_im3;//資料画像(フィルタ処理後)のビットマップ
|
29
|
-
|
30
|
-
PictureBox pb1, pb2, pb3;
|
31
38
|
|
32
|
-
private void Form1_Load(object sender, EventArgs e)
|
33
|
-
{
|
34
|
-
|
39
|
+
PictureBox pb1, pb2, pb3;
|
35
|
-
pb2 = pictureBox2;
|
36
|
-
pb3 = pictureBox3;
|
37
40
|
|
41
|
+
private void Form1_Load(object sender, EventArgs e)
|
38
|
-
|
42
|
+
{
|
43
|
+
pb1 = pictureBox1;
|
44
|
+
pb2 = pictureBox2;
|
45
|
+
pb3 = pictureBox3;
|
39
46
|
|
40
|
-
private void File_Open_Click(object sender, EventArgs e)
|
41
|
-
{
|
42
|
-
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
43
|
-
{
|
44
|
-
Image im1 = Image.FromFile(openFileDialog1.FileName);
|
45
|
-
DrawPicture1(im1);
|
46
|
-
|
47
|
+
}
|
47
|
-
}
|
48
48
|
|
49
|
+
private void File_Open_Click(object sender, EventArgs e)
|
50
|
+
{
|
51
|
+
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
52
|
+
{
|
53
|
+
Image im1 = Image.FromFile(openFileDialog1.FileName);
|
49
|
-
|
54
|
+
DrawPicture1(im1);
|
55
|
+
}
|
56
|
+
}
|
50
57
|
|
51
|
-
{
|
52
|
-
double a;
|
53
|
-
|
58
|
+
private void DrawPicture1(Image im1)
|
54
59
|
|
55
|
-
if (im1.Width < maxsize && im1.Height < maxsize)
|
56
|
-
|
60
|
+
{
|
57
|
-
pb1.Width = pb1.Height = maxsize;
|
58
|
-
Rectangle rect1 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
59
|
-
Rectangle rect2 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
60
|
-
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
61
|
-
Graphics g = Graphics.FromImage(bmp1_im1);
|
62
|
-
g.DrawImage(im1, rect2, rect1, GraphicsUnit.Pixel);
|
63
|
-
|
61
|
+
double a;
|
64
|
-
pb1.Image = bmp1_im1;
|
65
|
-
}
|
66
|
-
else
|
67
|
-
{
|
68
|
-
if (im1.Width > im1.Height)
|
69
|
-
{
|
70
|
-
/*a = (double)maxsize / (double)im1.Width;*/
|
71
|
-
|
62
|
+
int maxsize = 200;
|
72
|
-
pb1.Height = maxsize /* (int)(a * im1.Height)*/;
|
73
|
-
}
|
74
|
-
else
|
75
|
-
{
|
76
|
-
/*a = (double)maxsize / (double)im1.Height;*/
|
77
|
-
pb1.Height = maxsize;
|
78
|
-
pb1.Width =maxsize /*(int)(a * im1.Width)*/;
|
79
|
-
}
|
80
|
-
Rectangle rect3 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
81
|
-
Rectangle rect4 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
82
|
-
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
83
|
-
Graphics g = Graphics.FromImage(bmp1_im1);
|
84
|
-
g.DrawImage(im1, rect4, rect3, GraphicsUnit.Pixel);
|
85
|
-
g.Dispose();
|
86
|
-
pb1.Image = bmp1_im1;
|
87
|
-
}
|
88
|
-
}
|
89
63
|
|
90
|
-
|
64
|
+
if (im1.Width < maxsize && im1.Height < maxsize)
|
91
|
-
|
65
|
+
{
|
66
|
+
pb1.Width = pb1.Height = maxsize;
|
67
|
+
Rectangle rect1 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
68
|
+
Rectangle rect2 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
69
|
+
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
70
|
+
Graphics g = Graphics.FromImage(bmp1_im1);
|
92
|
-
|
71
|
+
g.DrawImage(im1, rect2, rect1, GraphicsUnit.Pixel);
|
72
|
+
g.Dispose();
|
73
|
+
pb1.Image = bmp1_im1;
|
74
|
+
}
|
75
|
+
else
|
93
|
-
|
76
|
+
{
|
77
|
+
if (im1.Width > im1.Height)
|
78
|
+
{
|
79
|
+
/*a = (double)maxsize / (double)im1.Width;*/
|
80
|
+
pb1.Width = maxsize;
|
94
|
-
|
81
|
+
pb1.Height = maxsize /* (int)(a * im1.Height)*/;
|
95
|
-
|
82
|
+
}
|
83
|
+
else
|
84
|
+
{
|
85
|
+
/*a = (double)maxsize / (double)im1.Height;*/
|
86
|
+
pb1.Height = maxsize;
|
87
|
+
pb1.Width =maxsize /*(int)(a * im1.Width)*/;
|
96
|
-
|
88
|
+
}
|
89
|
+
Rectangle rect3 = new Rectangle(0, 0, im1.Width, im1.Height);//イメージのサイズ
|
90
|
+
Rectangle rect4 = new Rectangle(0, 0, pb1.Width, pb1.Height);//PictureBox1のサイズ
|
91
|
+
bmp1_im1 = new Bitmap(pb1.Width, pb1.Height);
|
92
|
+
Graphics g = Graphics.FromImage(bmp1_im1);
|
93
|
+
g.DrawImage(im1, rect4, rect3, GraphicsUnit.Pixel);
|
94
|
+
g.Dispose();
|
95
|
+
pb1.Image = bmp1_im1;
|
96
|
+
}
|
97
|
+
}
|
97
98
|
|
98
|
-
|
99
|
+
private void File_SaveBMP_Click(object sender, EventArgs e)
|
99
|
-
|
100
|
+
{
|
100
|
-
int i, j, nx, ny;
|
101
|
-
int gray;
|
102
|
-
Color col;
|
103
|
-
nx = pb3.Width;
|
104
|
-
ny = pb3.Height;
|
105
|
-
|
101
|
+
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
|
106
|
-
for (j = 0; j < ny; j++)
|
107
|
-
for (i = 0; i < nx; i++)
|
108
|
-
|
102
|
+
{
|
109
|
-
col = bmp1_im2.GetPixel(i, j);
|
110
|
-
if (col.R > 200 && col.B > 200 && col.G > 200) gray = 255;
|
111
|
-
else gray = 0;
|
112
|
-
|
103
|
+
pb2.Image.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
|
113
|
-
|
104
|
+
}
|
114
|
-
pb2.Image = bmp1_im2;
|
115
|
-
|
105
|
+
}
|
116
|
-
|
117
|
-
|
118
|
-
private void gausian_Click(object sender, EventArgs e)
|
119
|
-
{
|
120
|
-
int nx, ny;
|
121
|
-
int gray;
|
122
106
|
|
107
|
+
private void P_GrayScale_Click(object sender, EventArgs e)
|
108
|
+
{
|
109
|
+
int i, j, nx, ny;
|
110
|
+
int gray;
|
111
|
+
Color col;
|
123
|
-
|
112
|
+
nx = pb3.Width;
|
124
|
-
|
113
|
+
ny = pb3.Height;
|
114
|
+
bmp1_im2 = new Bitmap (pb3.Image);
|
115
|
+
for (j = 0; j < ny; j++)
|
116
|
+
for (i = 0; i < nx; i++)
|
117
|
+
{
|
118
|
+
col = bmp1_im2.GetPixel(i, j);
|
119
|
+
if (col.R > 200 && col.B > 200 && col.G > 200) gray = 255;
|
120
|
+
else gray = 0;
|
121
|
+
bmp1_im2.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
|
122
|
+
}
|
123
|
+
pb2.Image = bmp1_im2;
|
124
|
+
}
|
125
125
|
|
126
|
-
// 画像の読み込み(グレースケールに変換)
|
127
|
-
|
126
|
+
private void gausian_Click(object sender, EventArgs e)
|
127
|
+
{
|
128
|
+
int nx, ny;
|
129
|
+
int gray;
|
128
130
|
|
131
|
+
nx = pb1.Width;
|
132
|
+
ny = pb1.Height;
|
133
|
+
|
134
|
+
// 画像の読み込み(グレースケールに変換)
|
135
|
+
bmp1_im3 = new Bitmap(pb1.Image);
|
136
|
+
|
129
|
-
|
137
|
+
// フィルタ用のカーネル
|
130
|
-
|
138
|
+
const int kernelSize = 3; // カーネルサイズ
|
131
|
-
|
139
|
+
double[,] kernel = new double[kernelSize, kernelSize]{
|
132
|
-
|
140
|
+
{1/16.0, 1/8.0, 1/16.0},
|
133
|
-
|
141
|
+
{1/8.0, 1/4.0, 1/8.0},
|
134
|
-
|
142
|
+
{1/16.0, 1/8.0, 1/16.0}};
|
135
|
-
|
143
|
+
/* フィルタ処理
|
136
|
-
|
144
|
+
bmp1_im3 = Filter(pb3.Image, kernel); ←ここの部分です
|
137
|
-
|
145
|
+
*/
|
138
|
-
|
146
|
+
pb3.Image = bmp1_im3;
|
139
|
-
|
140
|
-
|
147
|
+
|
141
|
-
}
|
142
148
|
}
|
149
|
+
}
|
150
|
+
}
|
151
|
+
```
|
143
152
|
|
144
|
-
|
153
|
+
### 試したこと
|
145
154
|
|
155
|
+
'Filter'の定義をしないとだめだと思い、試しましたがエラーがでるだけで、使用できませんでした。フィルタ処理の知識がないプログラム初心者ですみません。
|
156
|
+
|
146
157
|
### 補足情報(FW/ツールのバージョンなど)
|
147
158
|
|
148
|
-
|
159
|
+
大学のPCで visual studio 2005でコードを書いています。
|
149
|
-
|
160
|
+
※詳しいデザイン画面を貼るので少しおまちください。
|