AÇIKLAMA
C++ Builder 6 projesidir.
Seçilen bir resim üzerinde (jpg veya bmp) Average,Laplacian,Median,Sobel ve Skeleton filtrelerini seçilen kare matrise göre uygular. Ayarlabilenir matrisler sadece Average ve Median filtreleri için geçerlidir.
Programın Tamamını Aşağıdaki Linkten İndirebilirsiniz
Program Kodu:
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include <math.h> #include <jpeg.hpp> //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" //Değişken atamaları yapılıyor... TForm1 *Form1; Graphics::TBitmap *pBitmap = new Graphics::TBitmap(); Graphics::TBitmap *pBitmap2 = new Graphics::TBitmap(); Graphics::TBitmap *pBitmap3 = new Graphics::TBitmap(); TJPEGImage *jp = new TJPEGImage(); Byte *ptr, *ptr2; byte **resim; //--------------------------------------------------------------------------- // Quick sort kodu void q_sort(byte *numbers, int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = numbers[left]; while (left < right) { while ((numbers[right] >= pivot) && (left < right)) right--; if (left != right) { numbers[left] = numbers[right]; left++; } while ((numbers[left] <= pivot) && (left < right)) left++; if (left != right) { numbers[right] = numbers[left]; right--; } } numbers[left] = pivot; pivot = left; left = l_hold; right = r_hold; if (left < pivot) q_sort(numbers, left, pivot-1); if (right > pivot) q_sort(numbers, pivot+1, right); } //--------------------------------------------------------------------------- // Basit sıralama kodu void sirala(byte *dizi, int eleman_sayisi) { byte temp; for(int i=0; i<eleman_sayisi; i++) { for(int j=eleman_sayisi-1; j>i; j--) { if(dizi[j]>dizi[j-1]) { temp=dizi[j]; dizi[j]=dizi[j-1]; dizi[j-1]=temp; } } } } //--------------------------------------------------------------------------- // Programın constructor'ı __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //Resim aç butonuna tıklandığında yapılacak işlemler... void __fastcall TForm1::Button1Click(TObject *Sender) { // Diyalog kutusu ile resim alındı ve değişkenlere atandı OpenDialog1->Execute(); for(int i=0; i<OpenDialog1->Files->Count; i++) { //açılan dosya jpg mi, bmp mi? if(OpenDialog1->FileName.SubString(OpenDialog1->FileName.Length()-2,3)=="jpg") { // jpeg ise TJPEGImage yapısı ile okunuyor be bmp'e atanıyor jp->LoadFromFile(OpenDialog1->Files[0].Strings[i]); pBitmap->Assign(jp); pBitmap2->Assign(jp); } else { pBitmap->LoadFromFile(OpenDialog1->Files[0].Strings[i]); pBitmap2->LoadFromFile(OpenDialog1->Files[0].Strings[i]); } // Açılan resimlerin 8 bit olması durumunda, yapılacak okumanın doğru olması için pBitmap->PixelFormat=pf24bit; pBitmap2->PixelFormat=pf24bit; } resim=new byte*[pBitmap->Height]; for(int i=0; i<pBitmap->Height; i++) resim[i]=new byte[pBitmap->Width]; // Resim okunarak gri seviyeye dönüştürüldü for (int i=0; i<pBitmap->Height; i++) { ptr = (Byte *)pBitmap->ScanLine[i]; for (int j=0; j<pBitmap->Width; j++) { resim[i][j]=ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=(int)(ptr[j*3]*0.11+ptr[j*3+1]*0.59+ptr[j*3+2]*0.30); } } pBitmap2->Assign(pBitmap); Image1->Width=pBitmap->Width; Image1->Height=pBitmap->Height; Image1->Picture->Assign(pBitmap); } //--------------------------------------------------------------------------- //Uygula butonuna tıklandığında yapılacak işlemler void __fastcall TForm1::Button2Click(TObject *Sender) { int mask_size=StrToInt(ComboBox2->Text.SetLength(1)); int temp, temp2; byte *median_dizi; if(ComboBox1->ItemIndex==0) //Average Filtering yapılacaksa { for(int i=(int)(mask_size/2); i<(pBitmap->Height-(int)(mask_size/2)); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=(int)(mask_size/2); j<(pBitmap->Width-(int)(mask_size/2)); j++) { temp=0; //masktaki bütün değerler toplanıyor ve ortalaması alınıyor for(int k=-(int)(mask_size/2); k<=(int)(mask_size/2); k++) { for(int m=-(int)(mask_size/2); m<=(int)(mask_size/2); m++) { temp+=resim[i+k][j+m]; } } temp/=(mask_size*mask_size); ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp; } } } if(ComboBox1->ItemIndex==1) //Laplacian Filtering yapılacaksa { for(int i=1; i<(pBitmap->Height-1); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=1; j<(pBitmap->Width-1); j++) { // iki tür maske de kullanılabilir (programda ilk maske tercih edilmiş) temp=(5*resim[i][j]-(resim[i-1][j]+resim[i][j-1]+resim[i][j+1]+resim[i+1][j])); // temp=(9*resim[i][j]-(resim[i-1][j]+resim[i][j-1]+resim[i][j+1]+resim[i+1][j]+resim[i-1][j-1]+resim[i-1][j+1]+resim[i+1][j-1]+resim[i+1][j+1])); if(temp>255) temp=255; if(temp<0) temp=0; ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp; } } } if(ComboBox1->ItemIndex==2) //Median Filtering yapılacaksa { median_dizi=new byte[mask_size*mask_size]; for(int i=(int)(mask_size/2); i<(pBitmap->Height-(int)(mask_size/2)); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=(int)(mask_size/2); j<(pBitmap->Width-(int)(mask_size/2)); j++) { temp=0; //maskenin bütün elemanları sıralanıp orta değer alınıyor for(int k=-(int)(mask_size/2); k<=(int)(mask_size/2); k++) { for(int m=-(int)(mask_size/2); m<=(int)(mask_size/2); m++) { median_dizi[k+(mask_size/2)*mask_size+m+(mask_size/2)]=resim[i+k][j+m]; } } //sıralama algoritması olarak (dizi boyu küçük olduğundan) basit sıralama kullanılıyor // q_sort(median_dizi, 0, mask_size*mask_size-1); sirala(median_dizi, mask_size*mask_size); ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=median_dizi[(int)(mask_size*mask_size/2)]; } } } if(ComboBox1->ItemIndex==3) //Sobel filtresi ise { for(int i=1; i<(pBitmap->Height-1); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=1; j<(pBitmap->Width-1); j++) { //sobel maskesi uygulanıp magnitude değeri alınıyor temp=(-resim[i-1][j-1]-2*resim[i-1][j]-resim[i-1][j+1]+resim[i+1][j-1]+2*resim[i+1][j]+resim[i+1][j+1]); temp2=(-resim[i-1][j-1]-2*resim[i][j-1]-resim[i+1][j-1]+resim[i-1][j+1]+2*resim[i][j+1]+resim[i+1][j+1]); //255'i aşan ve 0'dan küçük değerler setleniyor if(temp>255) temp=255; if(temp<0) temp=0; ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp; } } } if(ComboBox1->ItemIndex==4) // skeleton işlemleri uygulanacaksa { // önce laplacian uygulanıyor for(int i=1; i<(pBitmap->Height-1); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=1; j<(pBitmap->Width-1); j++) { temp=(5*resim[i][j]-(resim[i-1][j]+resim[i][j-1]+resim[i][j+1]+resim[i+1][j])); if(temp>255) temp=255; if(temp<0) temp=0; ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp; } } //filtre görüntüsü kaydediliyor for(int i=1; i<(pBitmap->Height-1); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=1; j<(pBitmap->Width-1); j++) { resim[i][j]=ptr[j*3]; } } //bu görüntünün ilerde tekrar kullanılması için bir kopyası saklanıyor pBitmap3->Assign(pBitmap2); //sobeli alınıyor ve kaydediliyor for(int i=1; i<(pBitmap->Height-1); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=1; j<(pBitmap->Width-1); j++) { temp=(-resim[i-1][j-1]-2*resim[i-1][j]-resim[i-1][j+1]+resim[i+1][j-1]+2*resim[i+1][j]+resim[i+1][j+1]); temp2=(-resim[i-1][j-1]-2*resim[i][j-1]-resim[i+1][j-1]+resim[i-1][j+1]+2*resim[i][j+1]+resim[i+1][j+1]); if(temp>255) temp=255; if(temp<0) temp=0; ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp; } } for(int i=1; i<(pBitmap->Height-1); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=1; j<(pBitmap->Width-1); j++) { resim[i][j]=ptr[j*3]; } } //5x5 boyutunda average filtering yapılıyor ve kaydediliyor mask_size=5; for(int i=(int)(mask_size/2); i<(pBitmap->Height-(int)(mask_size/2)); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=(int)(mask_size/2); j<(pBitmap->Width-(int)(mask_size/2)); j++) { temp=0; for(int k=-(int)(mask_size/2); k<=(int)(mask_size/2); k++) { for(int m=-(int)(mask_size/2); m<=(int)(mask_size/2); m++) { temp+=resim[i+k][j+m]; } } temp/=(mask_size*mask_size); ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp; } } for(int i=1; i<(pBitmap->Height-1); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=1; j<(pBitmap->Width-1); j++) { resim[i][j]=ptr[j*3]; } } // laplacian görüntüsü ile çarpılıyor ve kaydediliyor for(int i=0; i<(pBitmap->Height); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; ptr2 = (Byte *)pBitmap3->ScanLine[i]; for(int j=0; j<(pBitmap->Width); j++) { temp=resim[i][j]*ptr2[j*3]/255; if(temp<0) resim[i][j]=0; if(temp>255) resim[i][j]=255; else resim[i][j]=temp; ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=resim[i][j]; } } //orjinal resimle toplanıyor ve kaydediliyor for(int i=0; i<(pBitmap->Height); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; ptr2 = (Byte *)pBitmap->ScanLine[i]; for(int j=0; j<(pBitmap->Width); j++) { temp=resim[i][j]+ptr2[j*3]; if(temp<0) resim[i][j]=0; if(temp>255) resim[i][j]=255; else resim[i][j]=temp; ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=resim[i][j]; } } //power-low dönüşümü uygulanıyor for(int i=0; i<(pBitmap->Height); i++) { ptr = (Byte *)pBitmap2->ScanLine[i]; for(int j=0; j<(pBitmap->Width); j++) { if(temp<0) resim[i][j]=0; if(temp>255) resim[i][j]=255; else resim[i][j]=temp; ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=resim[i][j]; } } //diğer maskelerin ana görüntü üzerine uygulanması için resetleme işlemi for (int i=0; i<pBitmap->Height; i++) { ptr = (Byte *)pBitmap->ScanLine[i]; for (int j=0; j<pBitmap->Width; j++) { resim[i][j]=ptr[j*3]; } } } // Görüntüler ekrana yansıtılıyor Image2->Width=pBitmap2->Width; Image2->Height=pBitmap2->Height; Image2->Picture->Assign(pBitmap2); } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox1Change(TObject *Sender) { // eğer laplacian, sobel ya da skeleton işlemleri uygulanacaksa mask size seçilemesin if(ComboBox1->ItemIndex==1 || ComboBox1->ItemIndex==3 || ComboBox1->ItemIndex==4) { ComboBox2->ItemIndex=0; ComboBox2->Enabled=False; } else { ComboBox2->Enabled=True; } } //---------------------------------------------------------------------------
| Yorumlar |
|
Powered by !JoomlaComment 3.26
