Öncelikle Herkese Merhaba
Görüntü işleme, kaydedilmiş olan, mevcut görüntüleri işlemek, yani mevcut resim ve grafikleri, değiştirmek, yabancılaştırmak ya da iyileştirmek için kullanılır.
Bu yazımda Görüntü işlemeyi C# dili ile kodlamaya çalışacağım. Gri,Binary ve Sobel kenar bulma algoritmalarını aşağıda bulabilirisniz.
Gri ve Binary
gri yapma;
binary yapma;
Program kodları;
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; namespace goruntuIsleme { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog std = new OpenFileDialog(); std.ShowDialog(); pictureBox1.ImageLocation = std.FileName; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } private void button2_Click(object sender, EventArgs e) { try { Bitmap image = new Bitmap(pictureBox1.Image); Bitmap gri = griYap(image); pictureBox2.Image=gri; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; } catch(Exception ex) { MessageBox.Show("Fotoğraf Yükleyiniz!"); } } private Bitmap griYap(Bitmap bmp) { for(int i=0;i<bmp.Height-1;i++) { for(int j=0;j<bmp.Width-1;j++) { int deger=(bmp.GetPixel(j,i).R+bmp.GetPixel(j,i).G+bmp.GetPixel(j,i).B)/3; Color renk = Color.FromArgb(deger,deger,deger); bmp.SetPixel(j,i,renk); } } return bmp; } private void pictureBox2_Click(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { try { Bitmap image = new Bitmap(pictureBox1.Image); Bitmap binary = binaryYap(image); pictureBox2.Image = binary; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; } catch(Exception ex) { MessageBox.Show("Fotoğraf Yükleyiniz!"); } } private Bitmap binaryYap(Bitmap bmp) { int tmp = 0; Bitmap gri = griYap(bmp); int esik=esikBul(gri); Color renk; for (int i = 0; i < gri.Height - 1; i++) { for(int j=0;j<gri.Width-1;j++) { tmp = gri.GetPixel(j, i).G; if(tmp<esik) { renk = Color.FromArgb(0, 0, 0); gri.SetPixel(j, i, renk); } else { renk = Color.FromArgb(255,255,255); gri.SetPixel(j, i, renk); } } } return gri; } private int esikBul(Bitmap gri) { int enb = gri.GetPixel(0, 0).G; int enk = gri.GetPixel(0, 0).G; for (int i = 0; i < gri.Height - 1; i++) { for (int j = 0; j < gri.Width - 1; j++) { if (enb > gri.GetPixel(j, i).G) enb = gri.GetPixel(j, i).G; if (enk < gri.GetPixel(j, i).G) enk = gri.GetPixel(j, i).G; } } int a=enb; int b=enk; int esik = (a+b)/2; return esik; } private void trackBar1_Scroll(object sender, EventArgs e) { } } }
Sobel Kenar bulma
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; namespace SobelKenarBulma { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog std = new OpenFileDialog(); std.ShowDialog(); pictureBox1.ImageLocation = std.FileName; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } private void button4_Click(object sender, EventArgs e) { Bitmap image = new Bitmap(pictureBox1.Image); Bitmap sobel = sobelYap(image); pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.Image = sobel; } private Bitmap sobelYap(Bitmap image) { Bitmap gri = griYap(image); Bitmap buffer = new Bitmap(gri.Width, gri.Height);//görüntünün boyutlarına sahip boş görüntü oluşturuyorsun Color renk; int valx, valy,gradient; int [,]GX = new int[3, 3]; int [,]GY = new int[3, 3]; //Yatay kenar GX[0, 0] = -1; GX[0, 1] = 0; GX[0, 2] = 1; GX[1, 0] = -2; GX[1, 1] = 0; GX[1, 2] = 2; GX[2, 0] = -1; GX[2, 1] = 0; GX[2, 2] = 1; //Dikey kenar GY[0, 0] = -1; GY[0, 1] = -2; GY[0, 2] = -1; GY[1, 0] = 0; GY[1, 1] = 0; GY[1, 2] = 0; GY[2, 0] = 1; GY[2, 1] = 2; GY[2, 2] = 1; for (int i = 0; i < image.Height; i++) { for (int j = 0; j < image.Width; j++) { if (i == 0 || i == gri.Height - 1 || j == 0 || j == gri.Width - 1) { renk = Color.FromArgb(255, 255, 255); buffer.SetPixel(j, i, renk); valx = 0; valy = 0; } else { valx = gri.GetPixel(j - 1, i - 1).R * GX[0, 0] + gri.GetPixel(j,i - 1).R * GX[0, 1] + gri.GetPixel(j + 1, i - 1).R * GX[0, 2] + gri.GetPixel(j - 1, i).R * GX[1, 0] + gri.GetPixel(j, i).R * GX[1, 1] + gri.GetPixel(j+1,i).R * GX[1, 2] + gri.GetPixel(j - 1, i + 1).R * GX[2, 0] + gri.GetPixel(j, i+1).R * GX[2, 1] + gri.GetPixel(j + 1, i + 1).R * GX[2, 2]; valy = gri.GetPixel(j - 1, i - 1).R * GY[0, 0] + gri.GetPixel(j, i - 1).R * GY[0, 1] + gri.GetPixel(j + 1, i - 1).R * GY[0, 2] + gri.GetPixel(j - 1, i).R * GY[1, 0] + gri.GetPixel(j, i).R * GY[1, 1] + gri.GetPixel(j + 1, i).R * GY[1, 2] + gri.GetPixel(j - 1, i + 1).R * GY[2, 0] + gri.GetPixel(j, i + 1).R * GY[2, 1] + gri.GetPixel(j + 1, i + 1).R * GY[2, 2]; gradient =(int)( Math.Abs(valx) + Math.Abs(valy)); if (gradient < 0) gradient = 0; if (gradient > 255) gradient = 255; renk = Color.FromArgb(gradient, gradient, gradient); buffer.SetPixel(j, i,renk); } } } return buffer; ; } private Bitmap griYap(Bitmap bmp) { for (int i = 0; i < bmp.Height - 1; i++) { for (int j = 0; j < bmp.Width - 1; j++) { int deger = (bmp.GetPixel(j, i).R + bmp.GetPixel(j, i).G + bmp.GetPixel(j, i).B) / 3; Color renk = Color.FromArgb(deger, deger, deger); bmp.SetPixel(j, i, renk); } } return bmp; } private void pictureBox2_Click(object sender, EventArgs e) { } } }