460 likes | 490 Views
Digital Image Processing: praktikum #3. RENCANA PRAKTIKUM. Brightness. Source code. public partial class Brightness : Form { Bitmap objBitmap1; Bitmap objBitmap2; public Brightness() { InitializeComponent(); }
E N D
Source code • public partial class Brightness : Form • { • Bitmap objBitmap1; • Bitmap objBitmap2; • public Brightness() • { • InitializeComponent(); • } • private void button1_Click(object sender, EventArgs e) • { • DialogResult d = openFileDialog1.ShowDialog(); • if (d == DialogResult.OK) • { • objBitmap1 = new Bitmap(openFileDialog1.FileName); • pictureBox1.Image = objBitmap1; • } • }
private void button2_Click(object sender, EventArgs e) • { • objBitmap2 = new Bitmap(objBitmap1); • int brightness = Convert.ToInt16(textBox1.Text); • for (int x = 0; x < objBitmap1.Width; x++) • for (int y = 0; y < objBitmap1.Height; y++) • { • Color w = objBitmap1.GetPixel(x, y); • int wR = w.R + brightness; • int wG = w.G + brightness; • int wB = w.B + brightness; • if (wR < 0) wR = 1; • if (wR > 255) wR = 255; • if (wG < 0) wG = 1; • if (wG > 255) wG = 255; • if (wB < 0) wB = 1; • if (wB > 255) wB = 255; • Color new_w = Color.FromArgb(wR, wG, wB); • objBitmap2.SetPixel(x, y, new_w); • } • pictureBox2.Image = objBitmap2; • } • } • }
private void button2_Click(object sender, EventArgs e) • { • objBitmap2 = new Bitmap(objBitmap1); • int a = Convert.ToInt16(textBox1.Text); • for (int x = 0; x < objBitmap1.Width; x++) • for (int y = 0; y < objBitmap1.Height; y++) • { • Color w = objBitmap1.GetPixel(x, y); • int xg = (int)((w.R + w.G + w.B) / 3); • int xb = xg + a; • if (xb > 255) • xb = 255; • else if (xb < 0) • xb = 0; • Color new_w = Color.FromArgb(xb, xb, xb); • objBitmap2.SetPixel(x, y, new_w); • } • pictureBox2.Image = objBitmap2; • }
Penjelasan: • Pengaturan brightness dilakukan dengan operasi penambahan pada grayscale tiap pixel. Pada program ini, variable xg adalah nilai grayscale yang didapat dari tiap pixel. Kemudian xg ditambahkan dengan value pada variable a yaitu variable yang menampung nilai dari textbox dan hasilnya disimpan pada variable xb. Agar tidak melebihi batas warna tertinggi (255) maka jika nilai hasil penjumlahan lebih dari 255 maka ditetapkan menjadi 255. Dan, jika nilai hasil penjumlahan kurang dari 0 maka ditetapkan menjadi 0.
private void trackBar1_Scroll(object sender, EventArgs e) • { • objBitmap2 = new Bitmap(objBitmap1); • int brightness = Convert.ToInt16(trackBar1.Value); • for (int x = 0; x < objBitmap1.Width; x++) • for (int y = 0; y < objBitmap1.Height; y++) • { • Color w = objBitmap1.GetPixel(x, y); • int wR = w.R + brightness; • int wG = w.G + brightness; • int wB = w.B + brightness; • if (wR < 0) wR = 1; • if (wR > 255) wR = 255; • if (wG < 0) wG = 1; • if (wG > 255) wG = 255; • if (wB < 0) wB = 1; • if (wB > 255) wB = 255; • Color new_w = Color.FromArgb(wR, wG, wB); • objBitmap2.SetPixel(x, y, new_w); • } • pictureBox2.Image = objBitmap2; • }
Source code • namespace WindowsFormsApplication1 • { • public partial class Form1 : Form • { • Bitmap objBitmap1; • Bitmap objBitmap2; • public Form1() • { • InitializeComponent(); • } • private void button1_Click(object sender, EventArgs e) • { • DialogResult d = openFileDialog1.ShowDialog(); • if (d == DialogResult.OK) • { • objBitmap1 = new Bitmap(openFileDialog1.FileName); • pictureBox1.Image = objBitmap1; • } • }
private void trackBar1_Scroll(object sender, EventArgs e) • { • objBitmap2 = new Bitmap(objBitmap1); • double contrast = Convert.ToDouble(trackBar1.Value); • contrast = (100.0 + contrast) / 100.0; • contrast *= contrast; • for (int x = 0; x < objBitmap1.Width; x++) • for (int y = 0; y < objBitmap1.Height; y++) • { • Color c = objBitmap1.GetPixel(x, y); • double pR = c.R / 255.0; • pR -= 0.5; • pR *= contrast; • pR += 0.5; • pR *= 255; • if (pR < 0) pR = 0; • if (pR > 255) pR = 255; • double pG = c.G / 255.0; • pG -= 0.5; • pG *= contrast; • pG += 0.5; • pG *= 255; • if (pG < 0) pG = 0; • if (pG > 255) pG = 255; • double pB = c.B / 255.0; • pB -= 0.5; • pB *= contrast; • pB += 0.5; • pB *= 255; • if (pB < 0) pB = 0; • if (pB > 255) pB = 255; • Color new_w = Color.FromArgb((byte)pR, (byte)pG, (byte)pB); • objBitmap2.SetPixel(x, y, new_w); • } • pictureBox2.Image = objBitmap2; • } • } • }
Source code • public partial class Form1 : Form • { • Bitmap objBitmap1; • Bitmap objBitmap2; • public Form1() • { • InitializeComponent(); • } • private void button1_Click(object sender, EventArgs e) • { • DialogResult d = openFileDialog1.ShowDialog(); • if (d == DialogResult.OK) • { • objBitmap1 = new Bitmap(openFileDialog1.FileName); • pictureBox1.Image = objBitmap1; • } • }
private void button2_Click(object sender, EventArgs e) • { • objBitmap2 = new Bitmap(objBitmap1); • //int a = Convert.ToInt16(textBox1.Text); • for (int x = 0; x < objBitmap1.Width; x++) • for (int y = 0; y < objBitmap1.Height; y++) • { • Color w = objBitmap1.GetPixel(x, y); • Color new_w = Color.FromArgb(255-w.R, 255-w.G, 255-w.B); • objBitmap2.SetPixel(x, y, new_w); • } • pictureBox2.Image = objBitmap2; • } • } • }
Source code • namespace WindowsFormsApplication1 • { • public partial class Form1 : Form • { • Image File; • Bitmap objBitmap; • Bitmap objBitmap1; • public Form1() • { • InitializeComponent(); • } • private void button1_Click(object sender, EventArgs e) • { • DialogResult d = openFileDialog1.ShowDialog(); • if (d == DialogResult.OK) • { • objBitmap = new Bitmap(openFileDialog1.FileName); • pictureBox1.Image = objBitmap; • } • }
private void button2_Click(object sender, EventArgs e) • { • File = pictureBox2.Image; • DialogResult d = saveFileDialog1.ShowDialog(); • if (d == DialogResult.OK) • { File.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); } • } • private void trackBar1_Scroll(object sender, EventArgs e) • { • int nilai; • objBitmap1 = new Bitmap(objBitmap); • nilai = Convert.ToInt32(trackBar1.Value); • int getWidth = objBitmap.Width; • int getHeight = objBitmap.Height; • double ratio = 0;
if (getWidth > getHeight) • { • ratio = getWidth / nilai; • getWidth = nilai; • getHeight = (int)(getHeight / ratio); • } • else • { • ratio = getHeight / nilai; • getHeight = nilai; • getWidth = (int)(getWidth / ratio); • } • pictureBox2.Width = getWidth; • pictureBox2.Height = getHeight; • pictureBox2.Image = objBitmap1; • } • } • }
Aktifkan di Toolbox Klik kanan, pilih Choose Items
Source code • 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 ZedGraph; • namespace WindowsFormsApplication1 • { • public partial class Form1 : Form • { • Bitmap img, img2, img3; • int curentImgWidth; • int curentImgHeight; • double zoom = 1; • GraphPane myPane = new GraphPane(); • public Form1() • { • InitializeComponent(); • }
private void button1_Click(object sender, EventArgs e) • { • OpenFileDialog oFile = new OpenFileDialog(); • if (oFile.ShowDialog() == DialogResult.OK) • { • myPane.CurveList.Clear(); • pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; • img = new Bitmap(new Bitmap(oFile.FileName), pictureBox1.Width, pictureBox1.Height); • pictureBox1.Image = img; • histo(); • myPane.AxisChange(); • zedGraphControl1.Refresh(); • curentImgWidth = Convert.ToInt32(pictureBox1.Width); • curentImgHeight = Convert.ToInt32(pictureBox1.Width * img.Height / img.Width); • } • }
private void button2_Click(object sender, EventArgs e) • { • int i, j; • if (img != null) • { • img2 = new Bitmap(img); • for (i = 0; i <= img2.Width - 1; i++) { • for (j = 0; j <= img2.Height - 1; j++) • { • Color originalColor = img2.GetPixel(i, j); • int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11)); • Color newColor = Color.FromArgb(grayScale, grayScale, grayScale); • img2.SetPixel(i, j, newColor); • } • } • pictureBox2.Image = img2; • } • }
private void button3_Click(object sender, EventArgs e) • { • int i, j, rata, nGreen, nRed, nBlue; • if (img != null) • { • img3 = new Bitmap(img); • for (i = 0; i <= img3.Width - 1; i++) • { • for (j = 0; j <= img3.Height - 1; j++) • { • Color pixelColor = img3.GetPixel(i, j); • nRed = pixelColor.R; • nGreen = pixelColor.G; • nBlue = pixelColor.B; • rata = Convert.ToInt32((nRed + nBlue + nGreen) / 3); • if (rata > 127) • rata = 255; • else • rata = 0; • Color newpixelColor = Color.FromArgb(rata, rata, rata); • img3.SetPixel(i, j, newpixelColor); • } • } • pictureBox3.Image = img3; • } • }
private void histo() • { • int[,] temp = new int[256, 1]; • int nilaiPixel; • PointPairList dataGraph = new PointPairList(); • for (int i = 0; i < img.Width; i++) • { • for (int j = 0; j < img.Height; j++) • { • nilaiPixel = img.GetPixel(i, j).R; • //MessageBox.Show(nilaiPixel.ToString()); • temp[nilaiPixel, 0] = temp[nilaiPixel, 0] + 1; • //dataGraph.Add(Convert.ToDouble(gambar.GetPixel(i, j).R), Convert.ToDouble(gambar.GetPixel(i, j).R)); • } • } • for (int i = 0; i < 256; i++) • { • dataGraph.Add(i, temp[i, 0]); • } • LineItem myCurve = myPane.AddCurve("data", dataGraph, Color.Black, SymbolType.None); • myCurve.Line.Fill = new Fill(Color.Black, Color.Black, 45f); • myPane.XAxis.Scale.Max = 258; • zedGraphControl1.AxisChange(); • }
private void Form1_Load(object sender, EventArgs e) • { • myPane.CurveList.Clear(); • myPane = zedGraphControl1.GraphPane; • myPane.XAxis.Title.Text = "Jumlah Pixel"; • myPane.YAxis.Title.Text = "NIlai Pixel"; • myPane.Title.Text = "Histogram"; • } • } • }