Visual Basic 中学校 掲示板 投稿の管理
タグのない投稿を抽出
統計
RSS
Visual Basic 中学校
投稿一覧
画像のモザイク化
この投稿へのリンク
https://keijiban.umayadia.com/ThreadDetail.aspx?ThreadId=30626#CommentId84598
この投稿の削除
削除パスワード
削除する
コメント本文
投稿者
ギル
 (学生)
投稿日時
2021/7/21 22:37:38
Public Class Form1
Dim mosaic_size_w, mosaic_size_h, r_data(160, 120), g_data(160, 120), b_data(160, 120) As Integer
Dim R, G, B, R_average, G_average, B_average As Integer
Dim myColor As Color
Dim myBMP, myBMPM As New Bitmap(160, 120) 'myBMPMがないと原画像もモザイク化してしまうので
Dim Coupled As Boolean = True 'モザイクサイズ(幅、高さ)の連動・非運動フラグ
Dim OpenFileDialog1 As New OpenFileDialog
Dim SaveFileDialog1 As New OpenFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "画像ファイル(*.jpg *.gif *.bmp)|*.jpg;*.gif;*.bmp"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myBMP = Image.FromFile(OpenFileDialog1.FileName) '原画像ファイル読み込み
For y As Integer = 0 To PictureBox1.Height - 1
For x As Integer = 0 To PictureBox1.Width - 1
myColor = myBMP.GetPixel(x, y)
r_data(x, y) = myColor.R 'Red成分
g_data(x, y) = myColor.G 'Green成分
b_data(x, y) = myColor.B 'Blue成分
Next
Next
PictureBox1.Image = myBMP
End If
End Sub
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
mosaic_size_w = NumericUpDown1.Value
If Coupled = True Then
NumericUpDown2.Value = mosaic_size_w
mosaic_size_h = mosaic_size_w
End If
End Sub
Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged
mosaic_size_h = NumericUpDown2.Value
If Coupled = True Then
NumericUpDown1.Value = mosaic_size_h
mosaic_size_w = mosaic_size_h
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
Coupled = True
Else
Coupled = False
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i, j, pixels As Integer
Debug.WriteLine("mosaic_size_w=" & mosaic_size_w) ' モザイクサイズ(幅)の確認
Debug.WriteLine("mosaic_size_w=" & mosaic_size_w) ' モザイクサイズ(高さ)の確認
' モザイク単位ごとの処理
For y As Integer = 0 To PictureBox1.Height - 1 Step mosaic_size_h
For x As Integer = 0 To PictureBox1.Width - 1 Step mosaic_size_w
R = 0 ' R平均値初期設定
G = 0 ' G平均値初期設定
B = 0 ' B平均値初期設定
For j = 0 To Math.Min(mosaic_size_h, PictureBox1.Height - y) - 1
For i = 0 To Math.Min(mosaic_size_w, PictureBox1.Width - x) - 1
R = R + r_data(x + i, y + j)
G = G + g_data(x + i, y + j)
B = B + b_data(x + i, y + j)
Next
Next
'モザイク単位内の総ピクセル数
pixels = Math.Min(mosaic_size_w, PictureBox1.Width - x) * Math.Min(mosaic_size_h, PictureBox1.Height - y)
R_average = Int(R / pixels)
G_average = Int(G / pixels)
B_average = Int(B / pixels)
Next
Next
PictureBox2.Image = myBMPM
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End ' プログラムの終了
End Sub
End Class
自分でやったもので全体としてこんな感じでデバックを開始するとpicturebox2でのモザイク画像が表示されないという感じになっています。