Hazır Program

Anasayfa Programlama C++ Builder Frekans Domeni Filtreleme


Frekans Domeni Filtreleme

E-posta Yazdır

AÇIKLAMA

C++ Builder 6 projesidir.

Resim işleme ve filtre uygulama konusunda ayarlanan filtreyi resime uygular. Resim işleme konusunda örnek teşkil eder. Program içindeki ayarlara göre Alçak yada Yüksek Geçiren filtreyi resime uygular.

Programın Tamamını Aşağıdaki Linkten İndirebilirsiniz

Program Kodu:

  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. #include <math.h>
  8. #include <jpeg.hpp>
  9. #include "Mixfft.c"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. #pragma resource "*.dfm"
  13.  
  14. //Değişken atamaları yapılıyor...
  15. TForm1 *Form1;
  16. Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
  17. Graphics::TBitmap *pBitmap2 = new Graphics::TBitmap();
  18. Graphics::TBitmap *pBitmap3 = new Graphics::TBitmap();
  19. TJPEGImage *jp = new TJPEGImage();
  20. Byte *ptr, *ptr2;
  21. double **resim, **resim_imag, **resim2, **resim2_imag;
  22. void fft(int n,double are[],double aim[],double bre[],double bim[]);
  23. double **Huv, maks1, min1;
  24. double **resim_filtered_fft, **resim_filtered_fft_imag, **resim_filtered, **resim_filtered_imag;
  25. int en, boy;
  26.  
  27. //---------------------------------------------------------------------------
  28. // 2-D FFT kodu (Tek boyutlu FFT yardımıyla oluşturuldu).
  29. void Mat_FFT2(double **Output_real, double **Output_imag, double **Input_real, double **Input_imag, int width, int height)
  30. {
  31.   int i, j;
  32.   double *T_re, *T_im, *R_re, *R_im;
  33.  
  34.   T_re=new double[height];
  35.   T_im=new double[height];
  36.   R_re=new double[height];
  37.   R_im=new double[height];
  38.  
  39.   for(i=0; i<height; i++)
  40.     fft(width, Input_real[i], Input_imag[i], Output_real[i], Output_imag[i]);
  41.  
  42.   for(i=0; i<width; i++)
  43.   {
  44.     for(j=0; j<height; j++)
  45.     {
  46.       T_re[j] = Output_real[j][i];
  47.       T_im[j] = Output_imag[j][i];
  48.     }
  49.     fft(height, T_re, T_im, R_re, R_im);
  50.     for(j=0; j<height; j++)
  51.     {
  52.       Output_real[j][i] = R_re[j];
  53.       Output_imag[j][i] = R_im[j];
  54.     }
  55.   }
  56.  
  57.   delete[] T_re;
  58.   delete[] T_im;
  59.   delete[] R_re;
  60.   delete[] R_im;
  61. }
  62.  
  63. //---------------------------------------------------------------------------
  64. // Inverse FFT kodu
  65. void Mat_IFFT2(double **Output_real, double **Output_imag, double **Input_real, double **Input_imag, int width, int height)
  66. {
  67.   int i, j;
  68.   double *T_re, *T_im, *R_re, *R_im;
  69.  
  70.   T_re=new double[height];
  71.   T_im=new double[height];
  72.   R_re=new double[height];
  73.   R_im=new double[height];
  74.  
  75.   for(i=0; i<height; i++)
  76.     ifft(width, Input_real[i], Input_imag[i], Output_real[i], Output_imag[i]);
  77.  
  78.   for(i=0; i<width; i++)
  79.   {
  80.     for(j=0; j<height; j++)
  81.     {
  82.       T_re[j] = Output_real[j][i];
  83.       T_im[j] = Output_imag[j][i];
  84.     }
  85.     ifft(height, T_re, T_im, R_re, R_im);
  86.     for(j=0; j<height; j++)
  87.     {
  88.       Output_real[j][i] = R_re[j];
  89.       Output_imag[j][i] = R_im[j];
  90.     }
  91.   }
  92.  
  93.   delete[] T_re;
  94.   delete[] T_im;
  95.   delete[] R_re;
  96.   delete[] R_im;
  97. }
  98.  
  99. //---------------------------------------------------------------------------
  100. // Programın constructor'ı
  101. __fastcall TForm1::TForm1(TComponent* Owner)
  102.         : TForm(Owner)
  103. {
  104. }
  105. //---------------------------------------------------------------------------
  106.  
  107. //Resim aç butonuna tıklandığında yapılacak işlemler...
  108. void __fastcall TForm1::Button1Click(TObject *Sender)
  109. {
  110. // Diyalog kutusu ile resim alındı
  111.   OpenDialog1->Execute();
  112.  
  113.   double deger, temp;
  114.   maks1=-1000000000;
  115.   min1=1000000000;
  116.  
  117. //resimle ilgili oluşturulan matrislerin bellekte tutulduğu yerler serbest bırakılıyor
  118.   for(int i=0; i<boy; i++)
  119.     delete[] resim[i];
  120.   delete[] resim;
  121.   for(int i=0; i<boy; i++)
  122.     delete[] resim_imag[i];
  123.   delete[] resim_imag;
  124.   for(int i=0; i<boy; i++)
  125.     delete[] resim2[i];
  126.   delete[] resim2;
  127.   for(int i=0; i<boy; i++)
  128.     delete[] resim2_imag[i];
  129.   delete[] resim2_imag;
  130.   for(int i=0; i<boy; i++)
  131.     delete[] Huv[i];
  132.   delete[] Huv;
  133.   for(int i=0; i<boy; i++)
  134.     delete[] resim_filtered_fft[i];
  135.   delete[] resim_filtered_fft;
  136.   for(int i=0; i<boy; i++)
  137.     delete[] resim_filtered_fft_imag[i];
  138.   delete[] resim_filtered_fft_imag;
  139.   for(int i=0; i<boy; i++)
  140.     delete[] resim_filtered[i];
  141.   delete[] resim_filtered;
  142.   for(int i=0; i<boy; i++)
  143.     delete[] resim_filtered_imag[i];
  144.   delete[] resim_filtered_imag;
  145.  
  146. // Resim açılıyor (jpg resimler de destekleniyor)
  147.   for(int i=0; i<OpenDialog1->Files->Count; i++)
  148.   {
  149.     //açılan dosya jpg mi, bmp mi?
  150.     if(OpenDialog1->FileName.SubString(OpenDialog1->FileName.Length()-2,3)=="jpg")
  151.     {
  152.       // jpeg ise TJPEGImage yapısı ile okunuyor be bmp'e atanıyor
  153.       jp->LoadFromFile(OpenDialog1->Files[0].Strings[i]);
  154.       pBitmap->Assign(jp);
  155.       pBitmap2->Assign(jp);
  156.     }
  157.     else
  158.     {
  159.       pBitmap->LoadFromFile(OpenDialog1->Files[0].Strings[i]);
  160.       pBitmap2->LoadFromFile(OpenDialog1->Files[0].Strings[i]);
  161.     }
  162.     // Açılan resimlerin 8 bit olması durumunda, yapılacak okumanın doğru olması için
  163.     pBitmap->PixelFormat=pf24bit;
  164.     pBitmap2->PixelFormat=pf24bit;
  165.     en=pBitmap->Width;
  166.     boy=pBitmap->Height;
  167.   }
  168.  
  169. // Matrisler için gerekli bellek tahsisi yapılıyor
  170.   resim=new double*[boy];
  171.   for(int i=0; i<boy; i++)
  172.     resim[i]=new double[en];
  173.   resim_imag=new double*[boy];
  174.   for(int i=0; i<boy; i++)
  175.     resim_imag[i]=new double[en];
  176.   resim2=new double*[boy];
  177.   for(int i=0; i<boy; i++)
  178.     resim2[i]=new double[en];
  179.   resim2_imag=new double*[boy];
  180.   for(int i=0; i<boy; i++)
  181.     resim2_imag[i]=new double[en];
  182.   Huv=new double*[boy];
  183.   for(int i=0; i<boy; i++)
  184.     Huv[i]=new double[en];
  185.   resim_filtered_fft=new double*[boy];
  186.   for(int i=0; i<boy; i++)
  187.     resim_filtered_fft[i]=new double[en];
  188.   resim_filtered_fft_imag=new double*[boy];
  189.   for(int i=0; i<boy; i++)
  190.     resim_filtered_fft_imag[i]=new double[en];
  191.   resim_filtered=new double*[boy];
  192.   for(int i=0; i<boy; i++)
  193.     resim_filtered[i]=new double[en];
  194.   resim_filtered_imag=new double*[boy];
  195.   for(int i=0; i<boy; i++)
  196.     resim_filtered_imag[i]=new double[en];
  197.  
  198. // Resim okunarak gri seviyeye dönüştürüldü ve frekans domeninde ortalanması için (-1)^(x+y) ile çarpılıyor
  199.   for (int i=0; i<boy; i++)
  200.   {
  201.     ptr = (Byte *)pBitmap->ScanLine[i];
  202.     for (int j=0; j<en; j++)
  203.     {
  204.       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);
  205.       resim[i][j]*=pow(-1,(i+j));
  206.       resim_imag[i][j]=0;
  207.       resim2_imag[i][j]=0;
  208.     }
  209.   }
  210.  
  211. // Açılan resim dosyası ekranda gösteriliyor
  212.   Image1->Width=en;
  213.   Image1->Height=boy;
  214.   Image1->Picture->Assign(pBitmap);
  215.  
  216. // Resmin FFT'si alınıyor
  217.   Mat_FFT2(resim2, resim2_imag, resim, resim_imag, en, boy);
  218.  
  219. //Resimin fftsinin doğal logaritması 0-255 arasına açılarak ekrana basılıyor
  220.   for (int i=0; i<boy; i++)
  221.   {
  222.     ptr = (Byte *)pBitmap->ScanLine[i];
  223.     for (int j=0; j<en; j++)
  224.     {
  225.       deger=log(0.05+sqrt(resim2[i][j]*resim2[i][j]+resim2_imag[i][j]*resim2_imag[i][j]));
  226.       if(deger>maks1)
  227.         maks1=deger;
  228.       if(deger<min1)
  229.         min1=deger;
  230.     }
  231.   }
  232.   for (int i=0; i<boy; i++)
  233.   {
  234.     ptr = (Byte *)pBitmap->ScanLine[i];
  235.     for (int j=0; j<en; j++)
  236.     {
  237.       deger=(log(0.05+sqrt(resim2[i][j]*resim2[i][j]+resim2_imag[i][j]*resim2_imag[i][j]))-min1)/(maks1-min1)*255;
  238.       ptr[j*3]=deger;
  239.       ptr[j*3+1]=deger;
  240.       ptr[j*3+2]=deger;
  241.     }
  242.   }
  243.  
  244. // Resmin FFT'si ekrana basılıyor.
  245.   pBitmap2->Assign(pBitmap);
  246.   Image2->Width=en;
  247.   Image2->Height=boy;
  248.   Image2->Picture->Assign(pBitmap);
  249. }
  250. //---------------------------------------------------------------------------
  251. //Uygula butonuna tıklandığında yapılacak işlemler
  252. void __fastcall TForm1::Button2Click(TObject *Sender)
  253. {
  254.   int yaricap=StrToInt(Edit1->Text);
  255.   int filtre_tipi=0;
  256.   double n_degeri;
  257.  
  258. // Filtrenin türü belirleniyor (alçak geçiren ya da yüksek geçiren)
  259.   filtre_tipi=ComboBox2->ItemIndex;
  260. // H(u,v) filtresi oluşturuluyor...
  261.   if(ComboBox1->ItemIndex==0)  //Ideal Filtering yapılacaksa
  262.   {
  263.     for (int i=0; i<boy; i++)
  264.     {
  265.       ptr = (Byte *)pBitmap->ScanLine[i];
  266.       for (int j=0; j<en; j++)
  267.       {
  268.         if(sqrt((i-boy/2)*(i-boy/2)+(j-en/2)*(j-en/2))<yaricap)
  269.         {
  270.           Huv[i][j]=1-filtre_tipi;
  271.         }
  272.         else
  273.         {
  274.           Huv[i][j]=0+filtre_tipi;
  275.         }
  276.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=Huv[i][j]*255;
  277.       }
  278.     }
  279.   }
  280.   if(ComboBox1->ItemIndex==1)  //Butterworth Filtering yapılacaksa
  281.   {
  282.     n_degeri=Edit2->Text.ToDouble();
  283.     for (int i=0; i<boy; i++)
  284.     {
  285.       ptr = (Byte *)pBitmap->ScanLine[i];
  286.       for (int j=0; j<en; j++)
  287.       {
  288.         Huv[i][j]=fabs(filtre_tipi-1/(1+pow(sqrt((i-boy/2)*(i-boy/2)+(j-en/2)*(j-en/2))/yaricap,2*n_degeri)));
  289.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=Huv[i][j]*255;
  290.       }
  291.     }
  292.   }
  293.   if(ComboBox1->ItemIndex==2)  //Gaussian Filtering yapılacaksa
  294.   {
  295.     for (int i=0; i<boy; i++)
  296.     {
  297.       ptr = (Byte *)pBitmap->ScanLine[i];
  298.       for (int j=0; j<en; j++)
  299.       {
  300.         Huv[i][j]=fabs(filtre_tipi-exp(-((i-boy/2)*(i-boy/2)+(j-en/2)*(j-en/2))/(double)(2*yaricap*yaricap)));
  301.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=Huv[i][j]*255;
  302.       }
  303.     }
  304.   }
  305.  
  306. // Filtre görüntüleri ekrana yansıtılıyor
  307.   Image3->Width=en;
  308.   Image3->Height=boy;
  309.   Image3->Picture->Assign(pBitmap);
  310.  
  311. }
  312. //---------------------------------------------------------------------------
  313.  
  314. void __fastcall TForm1::Button3Click(TObject *Sender)
  315. {
  316.   double deger;
  317.   byte eklenecek;
  318.  
  319. // resmin fft'si filtre ile çarpılıyor...
  320.   for (int i=0; i<boy; i++)
  321.   {
  322.     ptr = (Byte *)pBitmap2->ScanLine[i];
  323.     for (int j=0; j<en; j++)
  324.     {
  325.       resim_filtered_fft[i][j]=Huv[i][j]*resim2[i][j];
  326.       resim_filtered_fft_imag[i][j]=Huv[i][j]*resim2_imag[i][j];
  327.     }
  328.   }
  329. // Zaman domenine geçiliyor
  330.   Mat_IFFT2(resim_filtered, resim_filtered_imag, resim_filtered_fft, resim_filtered_fft_imag, en, boy);
  331. // Eğer yüksek geçiren filtre ise görüntüye 128 ekleniyor (daha iyi gözükmesi için)
  332.   if(ComboBox2->ItemIndex==1)
  333.     eklenecek=128;
  334.   else
  335.     eklenecek=0;
  336. // Filtrelenmiş görüntü ekrana basılıyor
  337.   for (int i=0; i<boy; i++)
  338.   {
  339.     ptr = (Byte *)pBitmap2->ScanLine[i];
  340.     for (int j=0; j<en; j++)
  341.     {
  342.       deger=resim_filtered[i][j]*pow(-1,i+j);
  343.       deger+=eklenecek;
  344.       if(deger>255)
  345.         deger=255;
  346.       if(deger<0)
  347.         deger=0;
  348.       ptr[j*3]=deger;
  349.       ptr[j*3+1]=deger;
  350.       ptr[j*3+2]=deger;
  351.     }
  352.   }
  353.   Image4->Width=pBitmap2->Width;
  354.   Image4->Height=pBitmap2->Height;
  355.   Image4->Picture->Assign(pBitmap2);
  356. }
  357. //---------------------------------------------------------------------------
  358.  
  359. void __fastcall TForm1::ComboBox1Change(TObject *Sender)
  360. {
  361. // eğer butterworth filtresi seçilmiş ise n değeri de kullanıcıdan girilebilecek
  362.   if(ComboBox1->ItemIndex==1)
  363.   {
  364.     Label2->Visible=true;
  365.     Edit2->Visible=true;
  366.   }
  367.   else
  368.   {
  369.     Label2->Visible=false;
  370.     Edit2->Visible=false;
  371.   }
  372. }
  373. //---------------------------------------------------------------------------
  374.  
  375. void __fastcall TForm1::FormCreate(TObject *Sender)
  376. {
  377. // başlangıç ayarları yapılıyor...
  378.   en=boy=1;
  379.   resim=new double*[1];
  380.   for(int i=0; i<1; i++)
  381.     resim[i]=new double[1];
  382.   resim_imag=new double*[1];
  383.   for(int i=0; i<1; i++)
  384.     resim_imag[i]=new double[1];
  385.   resim2=new double*[1];
  386.   for(int i=0; i<1; i++)
  387.     resim2[i]=new double[1];
  388.   resim2_imag=new double*[1];
  389.   for(int i=0; i<1; i++)
  390.     resim2_imag[i]=new double[1];
  391.   Huv=new double*[1];
  392.   for(int i=0; i<1; i++)
  393.     Huv[i]=new double[1];
  394.   resim_filtered_fft=new double*[1];
  395.   for(int i=0; i<1; i++)
  396.     resim_filtered_fft[i]=new double[1];
  397.   resim_filtered_fft_imag=new double*[1];
  398.   for(int i=0; i<1; i++)
  399.     resim_filtered_fft_imag[i]=new double[1];
  400.   resim_filtered=new double*[1];
  401.   for(int i=0; i<1; i++)
  402.     resim_filtered[i]=new double[1];
  403.   resim_filtered_imag=new double*[1];
  404.   for(int i=0; i<1; i++)
  405.     resim_filtered_imag[i]=new double[1];
  406. }
  407. //---------------------------------------------------------------------------
  408.  
  409.  

Yorumlar
Yeni Ekle Ara
+/-
Yorum yaz
Adınız:
E-posta:
 
Web Sayfas1:
Başlık:
UBB Kodu:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Lütfen resimdeki güvenlik kodunu giriniz.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Son Güncelleme ( Pazar, 24 Mayıs 2009 18:03 )  

Üye Giriş Formu



Kimler Çevrimiçi

Yok