38
Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com GÖRÜNTÜ İŞLEME-10.HAFTA MANTIKSAL OPERATÖRLER Mantıksal operatörler, resmin gerçek değerlerine dokunmadan, boolean ifadeler (true yada false) kullanılarak yeni bilgiler veya görüntüler çıkarmak için kullanılır. Örneğin “Ağaç hem yeşil hemde büyük ise” ifadesi A AND B şeklinde ifade edilebilir. Bu durumda her iki kavramda doğru (true) ise sonuç True olacaktır ve ona göre işlem yapılacaktır. Bu şekilde girilen iki tane görüntü üzerinde yada tek bir görüntü üzerinde, piksel değerlerine mantıksal operatörlerin doğruluk tablosu kuralları uygulanarak görüntüler üzerinde mantıksal işlemler gerçekleştirebiliriz. Normalde, aynı boyutta iki görüntüden çıktı görüntüsünü üretmek için, giriş görüntüsülerine ait birbirine karşılık gelen pikseller karşılaştırılır. Tek bir giriş görüntüsünü sabit bir mantıksal değerle mantıksal olarak birleştirmek de mümkündür. Bu durumda giriş görüntüsündeki her piksel karşılık gelen çıkış pikselini üretmek için aynı sabit ile karşılaştırılır. Mantıksal işlemler, tamsayı piksel değerlerine sahip görüntüler üzerinde, bu tamsayıların ikili gösterimleri (binary) yapılarak bunlar üzerinde bitsel bir şekilde işlem yapılarak, çıkış piksel değerine bitsel sonuçlar üretilip buradan tam sayı değerine ulaşılabilir. Örneğin, 47 ve 255 tam sayılarını 8 bitlik tam sayıları kullanarak birlikte XOR yapmak istediğimizi varsayalım. Bunları ikili biçimde göserirsek 45 sayısı 00101111 ve 255 sayısı 11111111'dir. Bunları bitsel olarak birlikte XORing yaparak, ikili olarak 11010000 değerine ulaşılır. Bunun karşılığı ise tam sayı olarak 208 dir. Mantıksal işlemler illa böyle bitsel (1/0) olarak uygulanması gerekmez. Örneğin, bazı uygulamalarda sıfır değerleri false kabul edilirken, sıfır haricindeki herhangi bir değer true olarak kabul edilebilir. Çıktı görüntüsünü elde etmek için ise 1-bit mantıksal işlemleri uygulanır. Çıktı görüntüsü basit bir ikili (binary) görüntünün kendisi olabilir ya da belki de ikili (binar) çıktı görüntüsü (sadece siyah ve beyaz görüntü) giriş görüntülerinden hehangi biriyle çarpılarak gri seviye bir görüntü elde edilebilir.. İnteger sayıyı ikili (binary) sayıya 8 bit olarak dönüştürme int intSayi1 = Convert.ToInt16(txtDeger1.Text); string binarySayi = Convert.ToString(intSayi1, 2).PadLeft(8, '0'); txtDeger1.Text = binarySayi; int intSayi2 = Convert.ToInt32(binarySayi, 2); txtDeger2.Text = intSayi2.ToString(); A-Mantıksal AND/NAND Operatörleri Mantıksal operatörler, Boolean değerler üzerinde (1 yada 0)(Doğru yada Yanlış) işlem yapılarak elde edilir. AND ve NAND işlem tablosu aşağıdaki gibidir.

GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

GÖRÜNTÜ İŞLEME-10.HAFTA

MANTIKSAL OPERATÖRLER

Mantıksal operatörler, resmin gerçek değerlerine dokunmadan, boolean ifadeler (true yada false) kullanılarak yeni bilgiler veya görüntüler çıkarmak için kullanılır. Örneğin “Ağaç hem yeşil hemde büyük ise” ifadesi A AND B şeklinde ifade edilebilir. Bu durumda her iki kavramda doğru (true) ise sonuç True olacaktır ve ona göre işlem yapılacaktır. Bu şekilde girilen iki tane görüntü üzerinde yada tek bir görüntü üzerinde, piksel değerlerine mantıksal operatörlerin doğruluk tablosu kuralları uygulanarak görüntüler üzerinde mantıksal işlemler gerçekleştirebiliriz. Normalde, aynı boyutta iki görüntüden çıktı görüntüsünü üretmek için, giriş görüntüsülerine ait birbirine karşılık gelen pikseller karşılaştırılır. Tek bir giriş görüntüsünü sabit bir mantıksal değerle mantıksal olarak birleştirmek de mümkündür. Bu durumda giriş görüntüsündeki her piksel karşılık gelen çıkış pikselini üretmek için aynı sabit ile karşılaştırılır.

Mantıksal işlemler, tamsayı piksel değerlerine sahip görüntüler üzerinde, bu tamsayıların ikili gösterimleri (binary) yapılarak bunlar üzerinde bitsel bir şekilde işlem yapılarak, çıkış piksel değerine bitsel sonuçlar üretilip buradan tam sayı değerine ulaşılabilir. Örneğin, 47 ve 255 tam sayılarını 8 bitlik tam sayıları kullanarak birlikte XOR yapmak istediğimizi varsayalım. Bunları ikili biçimde göserirsek 45 sayısı 00101111 ve 255 sayısı 11111111'dir. Bunları bitsel olarak birlikte XORing yaparak, ikili olarak 11010000 değerine ulaşılır. Bunun karşılığı ise tam sayı olarak 208 dir.

Mantıksal işlemler illa böyle bitsel (1/0) olarak uygulanması gerekmez. Örneğin, bazı uygulamalarda sıfır değerleri false kabul edilirken, sıfır haricindeki herhangi bir değer true olarak kabul edilebilir. Çıktı görüntüsünü elde etmek için ise 1-bit mantıksal işlemleri uygulanır. Çıktı görüntüsü basit bir ikili (binary) görüntünün kendisi olabilir ya da belki de ikili (binar) çıktı görüntüsü (sadece siyah ve beyaz görüntü) giriş görüntülerinden hehangi biriyle çarpılarak gri seviye bir görüntü elde edilebilir..

İnteger sayıyı ikili (binary) sayıya 8 bit olarak dönüştürme

int intSayi1 = Convert.ToInt16(txtDeger1.Text); string binarySayi = Convert.ToString(intSayi1, 2).PadLeft(8, '0'); txtDeger1.Text = binarySayi; int intSayi2 = Convert.ToInt32(binarySayi, 2); txtDeger2.Text = intSayi2.ToString();

A-Mantıksal AND/NAND Operatörleri

Mantıksal operatörler, Boolean değerler üzerinde (1 yada 0)(Doğru yada Yanlış) işlem yapılarak elde edilir. AND ve NAND işlem tablosu aşağıdaki gibidir.

Page 2: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Dikkat edilirse iki tablo birbirinin tam zıttıdır. Bu Operatörlerde Birinci resmi alıp, ikinci resimle AND veya NAND işlemine tabi tutularak çıktı resmi oluşturulur. Yada sadece tek bir Girdi resmi alınır ve belli bir sabit sayı ile AND yada NAND işlemine tabi tutulabilir. Tablolara dikkat edilirse bu iki operatör birbirinin tam tersidir.

AND ve NAND operatörü giriş olarak iki adet ikili (binary-siyah/beyaz resim) veya tamsayı ile ifade edilen gri seviye görüntüyü alır ve piksel değerleri üzerinde işlem yaparak ilk görüntünün değerlerini ikinci görüntü ile işlem yaparak üçüncü bir görüntü oluşturulur. Bu operatörün farklı bir uygulaması olarak tek bir giriş görüntüsü alınıp her pikseli sabit bir değer ile işleme tutarak çıktı görüntüsü oluşturulabilir.

İşlem tek bir geçişte doğrudan gerçekleştirilir. Üzerinde çalışılan tüm giriş pikseli değerlerinin aynı sayıda bite sahip olması önemlidir. Giriş görüntülerindeki piksel değerlerinin basit 1 bitlik sayılar olmadığı durumlarda, AND işlemi normalde (ancak her zaman değil) piksel değerlerindeki her bir bit üzerinde ayrı ayrı, bitsel şekilde gerçekleştirilir.

private void AND_NAND_IslemiToolStripMenuItem_Click(object sender, EventArgs e) { Bitmap Resim1, Resim2, CikisResmi; Resim1 = new Bitmap(pictureBox1.Image); Resim2 = new Bitmap(pictureBox2.Image); int ResimGenisligi = Resim1.Width; int ResimYuksekligi = Resim1.Height; CikisResmi = new Bitmap(ResimGenisligi, ResimYuksekligi); Color Renk1, Renk2; int x, y; int R = 0, G = 0, B = 0; for (x = 0; x < ResimGenisligi; x++) //Resmi taramaya şablonun yarısı kadar dış kenarlardan içeride başlayacak ve bitirecek. { for (y = 0; y < ResimYuksekligi; y++) { Renk1 = Resim1.GetPixel(x, y); Renk2 = Resim2.GetPixel(x, y); string binarySayi1 = Convert.ToString(Renk1.R, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. string binarySayi2 = Convert.ToString(Renk2.R, 2).PadLeft(8, '0'); string Bit1 = null, Bit2 = null, StringIkiliSayi = null; for (int i = 0; i < 8; i++) { Bit1 = binarySayi1.Substring(i, 1); Bit2 = binarySayi2.Substring(i, 1);

Page 3: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com ////AND İŞLEMİ //if (Bit1 == "0" && Bit2 == "0") StringIkiliSayi = StringIkiliSayi + "0"; //else if (Bit1 == "1" && Bit2 == "1") StringIkiliSayi = StringIkiliSayi + "1"; //else StringIkiliSayi = StringIkiliSayi + "0"; //NAND İŞLEMİ if (Bit1 == "0" && Bit2 == "0") StringIkiliSayi = StringIkiliSayi + "1"; else if (Bit1 == "1" && Bit2 == "1") StringIkiliSayi = StringIkiliSayi + "0"; else StringIkiliSayi = StringIkiliSayi + "1"; } R = Convert.ToInt32(StringIkiliSayi, 2); //İkili sayıyı tam sayıya dönüştürüyor. CikisResmi.SetPixel(x, y, Color.FromArgb(R, R, R)); //Gri resim } } pictureBox3.Image = CikisResmi;

}

AND için oluşan çıktı.

NAND için oluşan çıktı.

Renkli Resim için Kodlar

private void MANTIKSAL_OPERATOR_Click(object sender, EventArgs e) { Bitmap Resim1, Resim2, CikisResmi; Resim1 = new Bitmap(pictureBox1.Image); Resim2 = new Bitmap(pictureBox2.Image); int ResimGenisligi = 400;

Page 4: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com int ResimYuksekligi = 250; //int ResimGenisligi = Resim1.Width; //int ResimYuksekligi = Resim1.Height; CikisResmi = new Bitmap(ResimGenisligi, ResimYuksekligi); Color Renk1, Renk2; int x, y; int R = 0, G = 0, B = 0; for (x = 0; x < ResimGenisligi; x++) //Resmi taramaya şablonun yarısı kadar dış kenarlardan içeride başlayacak ve bitirecek. { for (y = 0; y < ResimYuksekligi; y++) { Renk1 = Resim1.GetPixel(x, y); Renk2 = Resim2.GetPixel(x, y); string binarySayi1R = Convert.ToString(Renk1.R, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. string binarySayi2R = Convert.ToString(Renk2.R, 2).PadLeft(8, '0'); string binarySayi1G = Convert.ToString(Renk1.G, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. string binarySayi2G = Convert.ToString(Renk2.G, 2).PadLeft(8, '0'); string binarySayi1B = Convert.ToString(Renk1.B, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. string binarySayi2B = Convert.ToString(Renk2.B, 2).PadLeft(8, '0'); string Bit1R = null, Bit1G = null, Bit1B = null, Bit2R = null, Bit2G = null, Bit2B = null; string StringIkiliSayiR = null, StringIkiliSayiG = null, StringIkiliSayiB = null; for (int i = 0; i < 8; i++) { Bit1R = binarySayi1R.Substring(i, 1); Bit2R = binarySayi2R.Substring(i, 1); //AND İŞLEMİ if (Bit1R == "0" && Bit2R == "0") StringIkiliSayiR = StringIkiliSayiR + "0"; else if (Bit1R == "1" && Bit2R == "1") StringIkiliSayiR = StringIkiliSayiR + "1"; else StringIkiliSayiR = StringIkiliSayiR + "0"; Bit1G = binarySayi1G.Substring(i, 1); Bit2G = binarySayi2G.Substring(i, 1); //AND İŞLEMİ if (Bit1G == "0" && Bit2G == "0") StringIkiliSayiG = StringIkiliSayiG + "0"; else if (Bit1G == "1" && Bit2G == "1") StringIkiliSayiG = StringIkiliSayiG + "1"; else StringIkiliSayiG = StringIkiliSayiG + "0"; Bit1B = binarySayi1B.Substring(i, 1); Bit2B = binarySayi2B.Substring(i, 1); //AND İŞLEMİ if (Bit1B == "0" && Bit2B == "0") StringIkiliSayiB = StringIkiliSayiB + "0"; else if (Bit1B == "1" && Bit2B == "1") StringIkiliSayiB = StringIkiliSayiB + "1"; else StringIkiliSayiB = StringIkiliSayiB + "0"; } R = Convert.ToInt32(StringIkiliSayiR, 2); //İkili sayıyı tam sayıya dönüştürüyor. G = Convert.ToInt32(StringIkiliSayiG, 2); //İkili sayıyı tam sayıya dönüştürüyor. B = Convert.ToInt32(StringIkiliSayiB, 2); //İkili sayıyı tam sayıya dönüştürüyor.

Page 5: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com CikisResmi.SetPixel(x, y, Color.FromArgb(R, G, B)); } } pictureBox3.Image = CikisResmi; }

Çıktısı: İki resimdeki yüksek değerli (255) ortak bölgeyi göstermiş oldu. Birinde yüksek diğerinde düşükse onu yok etti. Her ikisi de düşü olunca zaten yok oldu. Zaten AND operatörünü inceleyecek olursak tablo ile aynı mantığı burada verdi. Resimler iki renkli olduğu için bu yorumu yapmak kolay oldu, fakat renkli resimlerde bu kadar değimi görmek kolay olmayacaktır .

Uygulama 1

AND'in en belirgin uygulaması iki görüntünün kesişimini hesaplamaktır. Yani iki görüntü arasında hareket etmeyen, (birinci ve ikinci görüntüde aynı piksel konumlarında bulunan) nesneleri tespit etmek istediğimiz bir durumda kullanabilir.

Aşağıdaki iki resmi kullanarak uygulamayı yapalım.

İki gray seviye görüntüye bitsel olarak basitçe "AND" uygularsak;

Page 6: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Bu uygulamada taşınan nesnenin ortaya çıkan görüntüden kaybolmasını istememize rağmen, eski ve yeni konumunda iki kez görünür olarak ortaya çıktı. Bunun nedeni, nesnenin oldukça düşük piksel değerlerine (mantıksal 0'a benzer) sahipken arka planın yüksek değerlere (mantıksal 1'e benzer) sahip olmasıdır. Bununla birlikte, normalde bir nesneyi mantıksal 1 ile ve arka planı mantıksal 0 ile ilişkilendiririz, bu nedenle aslında NOR görüntülerine eşdeğer olan iki görüntünün negatiflerini AND'ledik. İstenilen sonucu elde etmek için görüntüleri AND işlemlerinden önce tersine çevirmeliyiz.

Şimdi, yalnızca her iki görüntüde de aynı konuma sahip olan nesne vurgulanır. Bununla birlikte, AND'in iki graylevel görüntüsünün bitmesi yine de sorunlara neden olabilir, çünkü iki yüksek piksel değerinin bitsel bir şekilde ANDing'inin yüksek bir çıkış değeri verdiği garanti edilmez (örneğin, 128 AND 127 0 değerini verir). Bu sorunları önlemek için, eşikleme kullanarak gri tonlamalı görüntülerden ikili (binary) sürümler üretmek en iyisidir. Aşağıdaki 2. Resim eşikleme yapılmış resimdir.

Page 7: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Her ne kadar AND işlemi yukarıdaki örnekte olduğu gibi iyi çalışsa da, o sahnede bazen şu şekilde problemlere neden olabilir.

Burada, ortalama yoğunluğu arka plandan daha yüksek ve diğeri daha düşük olan iki nesnemiz var. Bu nedenle, basit eşikleme kullanarak her iki nesneyi içeren bir ikili görüntü oluşturamayız. Aşağıdaki resimlerde de görüldüğü gibi, AND gri tonlamalı görüntüleri almak da başarılı değildir. İkinci sahnede ışık kısmı,

Page 8: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Taşınan nesneyi zayıflatmanın istenen etkisini gösterir. Ancak, ikinci sahne bir şekilde şuna benzer ise ve siyah nesne hareket ettirilirse; ikinci resimdeki görüntü elde edilir. Bu resimde Burada, koyu nesnenin eski ve yeni konumları görülebilir.

Genel olarak, aralarındaki farklılıkları veya benzerlikleri tespit etmek için AND operatörünün (veya diğer mantıksal operatörlerin) iki görüntüye uygulanması, ikili veya eşikleme kullanılarak ikili biçime dönüştürülebiliyorsa en uygunudur.

Diğer mantıksal işleçlerde olduğu gibi, AND ve NAND genellikle daha karmaşık görüntü işleme görevlerinin alt bileşenleri olarak kullanılır.

Uygulama 2

AND'in yaygın kullanımlarından biri maskeleme içindir. Örneğin, küçük bir bölgesini seçerek aydınlatmak istediğimizi varsayalım. Resimdeki belirli bir arabayı vurgulamak için.(Bunu yapmanın birçok yolu var ve biz sadece bir tanesini açıklıyoruz), önce vurgulanacak bölgeye bir boyama işlemi uygulanır (Paint programı ile yapılabilir). Burada gösterildiği gibi siyaha ayarladık.

Page 9: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Bu görüntü daha sonra yalnızca siyah bölgeyi seçmek için eşikleme yapılarak o bölgenin beyaz görünmesi sağlanır. Maske görüntüsü, ilgilendiğimiz bölgede 255 (11111111 binary) piksel değerine ve başka yerlerde sıfır piksel (00000000 binary) değerine sahiptir. Elde edilen maske daha sonra orijinal görüntü ile bitsel olarak AND işlemine tabi tutulursa, vurgulanacak bölge ortaya çıkarılmış olur. Son olarak, aracın olduğu bu görüntüyü 1,1 faktörle ölçeklendirerek aydınlatırız. Ardından orijinal görüntüyü ise 0,8 ölçek faktörü kullanarak karartırsak ve iki görüntüyü birleştirirsek aşağıdaki resmi elde ederiz.

Ödev 1: Benzer bir uygulama dış kısımlar bulanıklaştırılarak yada hedef bölgesi büyütülerek uygulanabilir.

Uygulama 3

AND operatörü, 8 bitlik bir görüntüde “bit dilimleme” adı verilen işlemi gerçekleştirmek için de kullanılabilir. Belirli bir bitin görüntü üzerindeki etkisini belirlemek için, ilgili bitin 1'e ve geri kalan 7 bitin 0'a ayarlanır. sabit bir sayı ile bitsel bir şekilde AND işlemi uygulanır.

Örneğin, görüntünün 8. Bitini (en önemli bite karşılık gelir) işlemi uygularsak ve AND işlemine tabi tuttuktan sonra 128 değeri ile eşiklersek araşağıdaki ikinci görüntüyü elde ederiz.

Page 10: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Aşağıdaki resimler sırasıyla görüntünün 7., 6., 4., 1. bit düzlemlerine uygulanan işlemlere karşılık gelir. Görüntü bilgisinin daha çok yüksek değerli bitlerde olduğu görülecektir. Daha az anlamlı bitler daha ince detayları yada paraziti içerir.

Page 11: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Orijinal resim ve 1 kat ölçeklenmiş hali.

0 bit 1x ölçekleme

1. bit 1x ölçekleme ve 3x ölçekleme

2. bit 5x öçekleme

Page 12: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

3 Bit 10x ölçekleme

4 bit 20x

5 bit 50x

Page 13: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

6 bit 100x

7 200x

Page 14: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

private void BIT_DILIMLEME_Click(object sender, EventArgs e) { Bitmap Resim1, CikisResmi; Resim1 = new Bitmap(pictureBox1.Image); int ResimGenisligi = 400; int ResimYuksekligi = 250; CikisResmi = new Bitmap(ResimGenisligi, ResimYuksekligi); Color Renk1; int x, y; int R = 0, G = 0, B = 0; for (x = 0; x < ResimGenisligi; x++) //Resmi taramaya şablonun yarısı kadar dış kenarlardan içeride başlayacak ve bitirecek. { for (y = 0; y < ResimYuksekligi; y++) { Renk1 = Resim1.GetPixel(x, y); string binarySayi1R = Convert.ToString(Renk1.R, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. string binarySayi1G = Convert.ToString(Renk1.G, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. string binarySayi1B = Convert.ToString(Renk1.B, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. string Bit1R = null, Bit1G = null, Bit1B = null; string StringIkiliSayiR = null, StringIkiliSayiG = null, StringIkiliSayiB = null; int BitSiraNo = Convert.ToInt32(textBox1.Text); for (int i = 0; i < 8; i++) { if (i == BitSiraNo) { Bit1R = binarySayi1R.Substring(i, 1); StringIkiliSayiR = StringIkiliSayiR + Bit1R; Bit1G = binarySayi1G.Substring(i, 1); StringIkiliSayiG = StringIkiliSayiG + Bit1G; Bit1B = binarySayi1B.Substring(i, 1); StringIkiliSayiB = StringIkiliSayiB + Bit1B; } else { StringIkiliSayiR = StringIkiliSayiR + "0"; StringIkiliSayiG = StringIkiliSayiG + "0"; StringIkiliSayiB = StringIkiliSayiB + "0"; } } R = Convert.ToInt32(StringIkiliSayiR, 2); //İkili sayıyı tam sayıya dönüştürüyor. G = Convert.ToInt32(StringIkiliSayiG, 2); //İkili sayıyı tam sayıya dönüştürüyor. B = Convert.ToInt32(StringIkiliSayiB, 2); //İkili sayıyı tam sayıya dönüştürüyor. int Olcek = Convert.ToInt32(textBox2.Text); R = R * Olcek;

Page 15: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com G = G * Olcek; B = B * Olcek; if (R > 255) R = 255; if (G > 255) G = 255; if (B > 255) B = 255; CikisResmi.SetPixel(x, y, Color.FromArgb(R, G, B)); } } pictureBox3.Image = CikisResmi;

}

Uygulama 4

Aşağıdaki resmi çeşitli sabit sayılar ile AND işlemine tabi tutarsak şu sonuçları elde ederiz. Sayı küçük iken görüntü koyu olduğu için görebilmek için ölçeklemek gerekmiştir.

1 sayısı ile AND leme ve 1x Ölçekleme 1 sayısı ile AND leme ve 255x Ölçekleme

Page 16: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

15 sayısı ile AND leme ve 10x Ölçekleme 100 sayısı ile AND leme ve 1x Ölçekleme

230 sayısı ile AND leme ve 1x Ölçekleme 250 sayısı ile AND leme ve 1x Ölçekleme

Benzer şekilde 0 sayısı ile AND leme yaparsak tamamen siyah resim, 255 sayısı ile AND leme yaparsak tamamen Orijinal resmi elde ederiz.

private void ANDSabitSayiToolStripMenuItem_Click(object sender, EventArgs e) { Bitmap Resim1, Resim2, CikisResmi; Resim1 = new Bitmap(pictureBox1.Image); int ResimGenisligi = Resim1.Width; int ResimYuksekligi = Resim1.Height;

Page 17: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com CikisResmi = new Bitmap(ResimGenisligi, ResimYuksekligi); Color Renk1; int x, y; int R = 0, G = 0, B = 0; for (x = 0; x < ResimGenisligi; x++) //Resmi taramaya şablonun yarısı kadar dış kenarlardan içeride başlayacak ve bitirecek. { for (y = 0; y < ResimYuksekligi; y++) { Renk1 = Resim1.GetPixel(x, y); string binarySayi1 = Convert.ToString(Renk1.R, 2).PadLeft(8, '0'); //Gri renk olduğundan tek kanal üzerinden yapılıyor. int SabitSayi = Convert.ToInt32(txtDeger1.Text); string binarySayi2 = Convert.ToString(SabitSayi, 2).PadLeft(8, '0'); string Bit1 = null, Bit2 = null, StringIkiliSayi = null; for (int i = 0; i < 8; i++) { Bit1 = binarySayi1.Substring(i, 1); Bit2 = binarySayi2.Substring(i, 1); //AND İŞLEMİ if (Bit1 == "0" && Bit2 == "0") StringIkiliSayi = StringIkiliSayi + "0"; else if (Bit1 == "1" && Bit2 == "1") StringIkiliSayi = StringIkiliSayi + "1"; else StringIkiliSayi = StringIkiliSayi + "0"; } R = Convert.ToInt32(StringIkiliSayi, 2); //İkili sayıyı tam sayıya dönüştürüyor. int Olcek = Convert.ToInt32(txtDeger2.Text); R = R * Olcek; //Sınırı aşan değerleri 255 ayarlama if (R > 255) R = 255; if (G > 255) G = 255; if (B > 255) B = 255; CikisResmi.SetPixel(x, y, Color.FromArgb(R, R, R)); //Gri resim } } pictureBox2.Image = CikisResmi; }

0

Page 18: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

50 100

150 200

**************************************

Logical OR/NOR

Doğrulama tabloları aşağıda gösterilmiştir. Görüleceği gibi her ikisi birbirinin tam tersidir. Her iki operatörde yukarıda olduğu gibi iki tane gri seviye resmi alıp bundan üçüncü bir resim üretir. Okunan her gri renk değeri 8 bit ikili sayı sistemine çevrilerek karşılıklı bitler OR yada NOR işlemine tabi tutulur.

Uygulama 1

Aşağıdaki iki resim üzerinde OR operatörünü uygulayalım.

Page 19: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

İki görüntünün önce Siyah/Beyaz resme döndürülmesini sağlayalım.

Bu iki resimde OR (Veya) operatörü birinde yada diğerinde bulduğu siyah seviye renkleri (0 ları) 1 yapacağından (beyaz) bu cisimler kaybolur. Yani hareket eden cisim burada kaybolur. Yerinde duran siyah (0) cisim her iki resimdede gözükeceğinden (0 yada 0) çıktısı yine siyah (0) olacaktır.

Page 20: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Aslında burada işlem Arka planın rengine göre yapılmıştır. Buradaki uygulamaya göre Arka plan beyazken hareket eden cisimler ortaya çıkarılmak istenirse NOR operatörünü kullanmak gerekir. Yada resmin tersi alınarak zemin siyah renge (0) çevrilip ondan sonra NOR uygulamak gerekir. Bunun sonunda hereket eden cisimler gözükecektir. Dikkat edilirse aynı zamanda yerinde duran cisimde gözükmüş olacaktır.

Uygulama 2

Bu tip operatörler daha çok kompleks bir görüntü işleme uygulamasının alt bir işlemi olarak kullanılırlar. Örneğin buradaki OR operatörü iki resmi birleştirmek için kullanılır. Aşağıdaki birincisi büyük, diğeri küçük iki resmi OR lama yapalım. Bu işlem için öncelikle ikinci resmi büyütmemiz lazım.

İki resmi aynı boyutlara getirdiğimizde (eklenecek boyutlar siyahla dolduruldu) aşağıdaki hali alır.

Page 21: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Her ikisin OR u uyguladığımızda aşağıdaki görüntü ortaya çıkar.

Bu örnekte resimler oldukça birbirinden farklı seviyede rentedirler. Oysa aşağıdaki örnekte OR işlemi nasıl bir sonuç verecektir görelim.

Uygulama 3

Aşağıdaki resim üzerine yukarıdaki Grafik OR lama yapılırsa elde edilen resimde Grafik düzgün bir şekilde gözükmeyecektir.

Page 22: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Aynı işlemi XOR operatöründe tekrar deneyin. Sonuçları karşılaştırın.

Görüldiğü gibi OR operatöründe de diğer operatörlerde olduğu gibi Piksel Değer Aşması olayı görülmemektedir. Fakat ikili sayı sistemleri ile çalışırken Siyah ve Beyaz resimlerle çalışmak daha garanti sonuç verir. Gri resimlerde sonuçlar sağlıklı olmaz. Örneğin 127 OR 128 değeri 255 verirken, 127 OR 126 değerleri 127 sayısını verir.

********************** AŞAĞIDAKİ UYGULAMALAR DA NOTLAR ARASINA EKLENECEKTİR. ZAMAN İÇİNDE GÜNCEL NOTLARI İNDİRİNİZ.

Logical XOR/XNOR

Common Names: XOR, XNOR, EOR, ENOR

Brief Description

XOR and XNOR are examples of logical operators having the truth-tables shown in Figure

1.

Page 23: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Figure 1 Truth-tables for XOR and XNOR.

The XOR function is only true if just one (and only one) of the input values is true, and false

otherwise. XOR stands for eXclusive OR. As can be seen, the output values of XNOR are

simply the inverse of the corresponding output values of XOR.

The XOR (and similarly the XNOR) operator typically takes two binary or graylevel

images as input, and outputs a third image whose pixel values are just those of the first

image, XORed with the corresponding pixels from the second. A variation of this operator

takes a single input image and XORs each pixel with a specified constant value in order to

produce the output.

How It Works

The operation is performed straightforwardly in a single pass. It is important that all the

input pixel values being operated on have the same number of bits in them, or unexpected

things may happen. Where the pixel values in the input images are not simple 1-bit numbers,

the XOR operation is normally (but not always) carried out individually on each

corresponding bit in the pixel values, in bitwise fashion.

Guidelines for Use

We illustrate the function of XOR using

Page 24: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

and

Since logical operators work more reliably with binary input we first threshold the two

images, thus obtaining

and

Now, we can use XOR to detect changes in the images, since pixels which didn't change

output 0 and pixels which did change result in 1. The image

shows the result of XORing the thresholded images. We can see the old and the new

position of the moved object, whereas the stationary object almost disappeared from the

image. Due to the effects of noise, we can still see some pixels around the boundary of the

stationary object, i.e. pixels whose values in the original image were close to the threshold.

In a scene like

Page 25: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

it is not possible to apply a threshold in order to obtain a binary image, since one of the

objects is lighter than the background whereas the other one is darker. However, we can

combine two grayscale images by XORing them in a bitwise fashion.

shows a scene where the dark object was moved and in

the light object changed its position. XORing each of them with the initial image yields

and

respectively. In both cases, the moved part appears at the old as well as at the new location

and the stationary object almost disappears. This technique is based on the assumption that

XORing two similar grayvalues produces a low output, whereas two distinct inputs yield a

Page 26: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

high output. However, this is not always true, e.g. XORing 127 and 128 yields 255. These

effects can be seen at the boundary of the stationary object, where the pixels have an

intermediate graylevel and might, due to noise, differ slightly between two of the images.

Hence, we can see a line with high values around the stationary object. A similar problem is

that the output for the moved pen is much higher than the output for the moved piece of

paper, although the contrast between their intensities and that of the background value is

roughly the same. Because of these problems it is often better to use image

subtraction or image division for change detection.

As with other logical operators, XOR and XNOR are often used as sub-components of more

complex image processing tasks. XOR has the interesting property that if we

XOR A with B to get Q, then the bits of Q are the same as A where the corresponding bit

from B is zero, but they are of the opposite value where the corresponding bit from B is one.

So for instance using binary notation, 1010 XORed with 1100 gives 0110. For this

reason, B could be thought of as a bit-reversal mask. Since the operator is symmetric, we

could just as well have treated A as the mask and B as the original.

Extending this idea to images, it is common to see an 8-bit XOR image mask containing

only the pixel values 0 (00000000 binary) and 255 (11111111 binary). When this is XORed

pixel-by-pixel with an original image it reverses the bits of pixels values where the mask is

255, and leaves them as they are where the mask is zero. The pixels with reversed bits

normally `stand out' against their original color and so this technique is often used to

produce a cursor that is visible against an arbitrary colored background. The other advantage

of using XOR like this is that to undo the process (for instance when the cursor moves

away), it is only necessary to repeat the XOR using the same mask and all the flipped pixels

will become unflipped. Therefore it is not necessary to explicitly store the original colors of

the pixels affected by the mask. Note that the flipped pixels are not always visible against

their unflipped color --- light pixels become dark pixels and dark pixels become light pixels,

but middling gray pixels become middling gray pixels!

The image

shows a simple graylevel image. Suppose that we wish to overlay this image with

its histogram shown in

so that the two can be compared easily. One way is to use XOR. We first use an image

editor to enlarge the histogram until it is the same size as the first image. The result is shown

in

Page 27: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

To perform the overlay we simply XOR this image with the first image in bitwise fashion to

produce

Here, the text is quite easy to read, because the original image consists of large and rather

light or rather dark areas. If we proceed in the same way with

we obtain

Note how the writing is dark against light backgrounds and light against dark backgrounds

and hardly visible against gray backgrounds. Compare the result with that described

under OR. In fact XORing is not particularly good for producing easy to read text on gray

backgrounds --- we might do better just to add a constant offset to the image pixels that we

wish to highlight (assuming wraparound under addition overflow) --- but it is often used to

quickly produce highlighted pixels where the background is just black and white or where

legibility is not too important.

Interactive Experimentation

You can interactively experiment with this operator by clicking here.

Page 28: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Exercises

1. XOR

and

Compare the result with the output of XORing their negatives. Do you see the same

effect as for other logical operators?

2. Use the technique discussed above to produce a cursor on

Place the cursor on different location of the image and examine the performance on a

background with high, low, intermediate and mixed pixel values.

****************************

Invert/Logical NOT

Common Names: Logical NOT, invert, photographic negative

Page 29: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Brief Description

Logical NOT or invert is an operator which takes a binary or graylevel image as input and

produces its photographic negative, i.e. dark areas in the input image become light and light

areas become dark.

How It Works

To produce the photographic negative of a binary image we can employ the logical NOT

operator. Its truth-table is shown in Figure 1.

Figure 1 Truth-table for logical NOT.

Each pixel in the input image having a logical 1 (often referred to as foreground) has a

logical 0 (associated with the background in the output image and vice versa. Hence,

applying logical NOT to a binary image changes its polarity.

The logical NOT can also be used for a graylevel image being stored in byte pixel format by

applying it in a bitwise fashion. The resulting value for each pixel is the input value

subtracted from 255:

Some applications of invert also support integer or float pixel format. In this case, we can't

use the logical NOT operator, therefore the pixel values of the inverted image are simply

given by

If this output image is normalized for an 8-bit display, we again obtain the photographic

negative of the original input image.

Page 30: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Guidelines for Use When processing a binary image with a logical or morphological operator, its polarity is often important. Hence, the logical NOT operator is often used to change the polarity of a binary image as a part of some larger process. For example, if we OR

and

the resulting image,

shows the union of the background, because it is represented with a logical 1. However, if

we OR

and

Page 31: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

which are the inverted versions of the above image we obtain

Now, the result contains the union of the two circles.

We illustrate another example of the importance of the polarity of a binary image using

the dilation operator. Dilation expands all white areas in a binary image. Hence, if we dilate

the object, being represented with a logical 1, grows and the holes in the object shrink. We

obtain

If we dilate

which was obtained by applying logical NOT to the original image, we get

Here, the background is expanded and the object became smaller.

Page 32: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Invert can be used for the same purpose on grayscale images, if they are processed with a

morphological or logical operator.

Invert is also used to print the photographic negative of an image or to make the features in

an image appear clearer to a human observer. This can, for example, be useful for medical

images, where the objects often appear in black on a white background. Inverting the image

makes the objects appear in white on a dark background, which is often more suitable for the

human eye. From the original image

of a tissue slice, we obtain the photographic negative

Interactive Experimentation

You can interactively experiment with this operator by clicking here.

Exercises

1. Apply the erode operator to

and

Which polarity of the image allows you to suppress the circles?

2. Compare the results of ORing

Page 33: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

and

and ORing their photographic negatives.

3. Take the photographic negative of

Does it improve the visibility of the features in the image?

****************************

Bitshift Operators

Common Names: Bitshifting

Page 34: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Brief Description

The bitshift operator works on images represented in byte or integer pixel format, where

each pixel value is stored as a binary number with a fixed amount of bits. Bitshifting shifts

the binary representation of each pixel to the left or to the right by a pre-defined number of

positions. Shifting a binary number by one bit is equivalent to multiplying (when shifting to

the left) or dividing (when shifting to the right) the number by 2.

How It Works

The operation is performed straightforwardly in a single pass. If the binary representation of

a number is shifted in one direction, we obtain an empty position on the opposite side. There

are generally three possibilities of how to fill in this empty position: we can pad the empty

bits with a 0 or a 1 or we can wrap around the bits which are shifted out of the binary

representation of the number on the other side. The last possibility is equivalent

to rotating the binary number.

The choice of technique used depends on the implementation of the operator and on the

application. In most cases, bitshifting is used to implement a fast multiplication or division.

In order to obtain the right results for this application, we have to pad the empty bits with

a 0. Only in the case of dividing a negative number by a power of 2, do we need to fill the

left bits with a 1, because a negative number is represented as the two's-complement of the

positive number, i.e. the sign bit is a 1. The result of applying bitshifting in this way is

illustrated in the following formula:

An example is shown in Figure 1.

Page 35: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

Figure 1 Examples for using bitshifting for multiplication and division. Note that the bottom example uses a signed-byte convention where a byte represents a number between -128 and +127

If bitshifting is used for multiplication, it might happen that the result exceeds the maximum

possible pixel value. This is the case when a 1 is shifted out of the binary representation of

the pixel value. This information is lost and the effect is that the value is wrapped

around from zero.

Guidelines for Use

The main application for the bitshift operator is to divide or multiply an image by a power

of 2. The advantage over the normal pixel division and pixel multiplication operators is that

bitshifting is computationally less expensive.

For example, if we want to add two images we can use bitshifting to make sure that the

result will not exceed the maximum pixel value. We illustrate this example using

and

Page 36: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

where the latter is the skeleton gained from the thresholded version of the former. To better

visualize the result of the skeletonization we might want to overlay these two images.

However, if we add them straightforwardly we obtain pixel values greater than the

maximum value. First shifting both images to the right by one bit yields

and

which then can be added without causing any overflow problems. The result can be seen in

Here, we can see that shifting a pixel to the right does, as a normal pixel division, decrease

the contrast in the image.

On the other hand, shifting the binary representation of a pixel to the left increases the image

contrast, like the pixel multiplication. For example,

is an image taken under poor lighting conditions. Shifting each pixel in the image to the left

by one bit, which is identical to multiplying it with 2, yields

Although the operator worked well in this example, we have to be aware that the result of

the multiplication might exceed the maximum pixel value. Then, the effect for the pixel

value is that it is wrapped around from 0. For example, if we shift each pixel in the above

Page 37: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

image by two bits, at some pixels a 1 is shifted out of the binary representation of the image,

resulting in a loss of information. This can be seen in

In general, we should make sure that the values in the input image are sufficiently small or

we have to be careful when we interpret the resulting image. Alternatively, we can change

the pixel value format prior to applying the bitshift operator, e.g. change from byte format

to integer format.

Although multiplication and division are the main applications for bitshifting it might also

be used for other, often very specialized, purposes. For example, we can store two 4-

bit images in a byte array if we shift one of the two images by 4 bits and mask out the

unused bits. Using the logical OR operator we can combine the two images into one without

losing any information. Sometimes it might also be useful to rotate the binary representation

of each bit, apply some other operator to the image and finally rotate the pixels back to the

initial order.

Interactive Experimentation

You can interactively experiment with this operator by clicking here.

Exercises

1. Use pixel addition to overlay

and its edge image

Apply the bitshift operator to the original image in order to increase its contrast.

Convert the image into an integer format prior to the shifting to preserve all image

information. Compare the result of the addition with the one you get without the

bitshifting.

Page 38: GÖRÜNTÜ İŞL M-10 - IbrahimCayiroglu.Com

Karabük Üniversitesi, Mühendislik Fakültesi.........................................................................................................www.ibrahimcayiroglu.com

2. What is the result of dividing -7 (binary:1001) by 2 using bitshifting. What is the result of dividing +7 (binary:0111) by 2?