Hazır Program

Anasayfa Programlama C++ Builder Histogram Eşitlemek ve Eşlemek


Histogram Eşitlemek ve Eşlemek

E-posta Yazdır

AÇIKLAMA

C++ Builder 6 projesidir.

Resim üzerinde histogram eşitleme ve eşleme işlemlerini gerçekleştirir.

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. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. #pragma resource "*.dfm"
  11.  
  12. //Değişken atamaları yapılıyor...
  13. TForm1 *Form1;
  14. Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
  15. Graphics::TBitmap *pBitmap2 = new Graphics::TBitmap();
  16. int hist[256];
  17. int hist2[256];
  18. double oyf[256];
  19. double odf[256];
  20. double odf2[256];
  21. int donusum[256];
  22. double oyf_desired[256];
  23. double odf_desired[256];
  24. int sayac;
  25. double fark;
  26. Byte *ptr, *ptr2;
  27. //---------------------------------------------------------------------------
  28. // Programın constructor'ı
  29. __fastcall TForm1::TForm1(TComponent* Owner)
  30.         : TForm(Owner)
  31. {
  32. }
  33. //---------------------------------------------------------------------------
  34.  
  35. //Resim aç butonuna tıklandığında yapılacak işlemler...
  36. void __fastcall TForm1::Button1Click(TObject *Sender)
  37. {
  38. // Diyalog kutusu ile resim alındı ve değişkenlere atandı
  39.   OpenPictureDialog1->Execute();
  40.   for(int i=0; i<OpenPictureDialog1->Files->Count; i++)
  41.   {
  42.     pBitmap->LoadFromFile(OpenPictureDialog1->Files[0].Strings[i]);
  43.     pBitmap2->LoadFromFile(OpenPictureDialog1->Files[0].Strings[i]);
  44.   }
  45.  
  46. // hist dizisinin içeriği 0'landı.
  47.   for(int i=0; i<256; i++)
  48.     hist[i]=0;
  49.  
  50. // Resim okunarak resim gri seviyeye dönüştürüldü ve histogramı belirlendi
  51.   for (int i=0; i<pBitmap->Height; i++)
  52.   {
  53.     ptr = (Byte *)pBitmap->ScanLine[i];
  54.     for (int j=0; j<pBitmap->Width; j++)
  55.     {
  56.       hist[(int)(ptr[j*3]*0.11+ptr[j*3+1]*0.59+ptr[j*3+2]*0.30)]++;
  57.       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);
  58.     }
  59.   }
  60.   Image1->Width=pBitmap->Width;
  61.   Image1->Height=pBitmap->Height;
  62.   Image1->Picture->Assign(pBitmap);
  63.  
  64. // oyf hesaplandı ve ekrana histogram çizdirildi
  65.   Chart1->Series[0]->Clear();
  66.   for(int i=0; i<256; i++)
  67.   {
  68.     oyf[i]=(double)hist[i]/(pBitmap->Width*pBitmap->Height);
  69.     Chart1->Series[0]->AddXY(i, hist[i], "", clRed);
  70.   }
  71.  
  72. // olasılık dağılım fonk. hesaplandı ve ekrana çizdirildi
  73.   Chart4->Series[0]->Clear();
  74.   odf[0]=oyf[0];
  75.   for(int i=1; i<256; i++)
  76.   {
  77.     odf[i]=oyf[i]+odf[i-1];
  78.     Chart4->Series[0]->AddXY(i, odf[i], "", clGreen);
  79.   }
  80. }
  81. //---------------------------------------------------------------------------
  82. //Uygula butonuna tıklandığında yapılacak işlemler
  83. void __fastcall TForm1::Button2Click(TObject *Sender)
  84. {
  85. // ön setlemeler (temizleme işlemleri)
  86.   Chart2->Series[0]->Clear();
  87.   Chart5->Series[0]->Clear();
  88.   Chart3->Series[0]->Clear();
  89.   Chart6->Series[0]->Clear();
  90.   for(int i=0; i<256; i++)
  91.     hist2[i]=0;
  92.  
  93.   if(ComboBox1->ItemIndex==0)  //Histogram Equalization yapılacaksa
  94.   {
  95. // gerekli dönüşüm işlemi dönüşüm isimli lookup table'da tutuluyor ve ekrana istenilen histogram ile odf. çizdiriliyor
  96.     for(int i=0; i<256; i++)
  97.     {
  98.       Chart2->Series[0]->AddXY(i, pBitmap->Width*pBitmap->Height/256, "", clRed);
  99.       Chart5->Series[0]->AddXY(i, (double)(i+1)/256.0, "", clGreen);
  100.       donusum[i]=(int)floor(odf[i]*255+0.5);
  101.     }
  102.   }
  103.   else  //Histogram Matching yapılacaksa
  104.   {
  105.     sayac=0;
  106.     for(int i=0; i<256; i++)
  107.     {
  108. // İstenilen olasılık dağılım fonk. hesaplanıyor ve resmin odf ile karşılaştırılıyor
  109.       fark=odf[i]-odf_desired[sayac];
  110.       for(int j=sayac+1; j<256; j++)
  111.       {
  112.         if(abs(odf[i]-odf_desired[j])<=fark)
  113.         {
  114.           fark=odf[i]-odf_desired[j];
  115.           sayac=j;
  116.         }
  117.         else
  118.           break;
  119.       }
  120. // dönüşüm lookup table'ı oluşturuluyor ve ekrana istenilen histogram ile odf. çizdiriliyor
  121.       donusum[i]=sayac;
  122.       Chart2->Series[0]->AddXY(i, (double)(i+1)/256.0*pBitmap->Width*pBitmap->Height, "", clRed);
  123.       Chart5->Series[0]->AddXY(i, odf_desired[i], "", clGreen);
  124.     }
  125.   }
  126.  
  127. //donusum lookup tanle'ına göre dönüşüm gerçekleştiriliyor ve oluşan görüntünün histogramı hesaplanıyor
  128.   for (int i=0; i<pBitmap->Height; i++)
  129.   {
  130.     ptr = (Byte *)pBitmap->ScanLine[i];
  131.     ptr2= (Byte *)pBitmap2->ScanLine[i];
  132.     for (int j=0; j<pBitmap->Width; j++)
  133.     {
  134.       ptr2[j*3]=ptr2[j*3+1]=ptr2[j*3+2]=donusum[ptr[j*3]];
  135.       hist2[ptr2[j*3]]++;
  136.     }
  137.   }
  138.  
  139. // oluşan görüntünün odf. hesaplanıyor
  140.   odf2[0]=(double)hist2[0]/(double)(pBitmap->Width*pBitmap->Height);
  141.   for(int i=1; i<256; i++)
  142.     odf2[i]=odf2[i-1]+(double)hist2[i]/(double)(pBitmap->Width*pBitmap->Height);
  143.  
  144. //  oluşan görüntünün histogramı ile odf çizdiriliyor
  145.   for(int i=0; i<256; i++)
  146.   {
  147.     Chart3->Series[0]->AddXY(i, hist2[i], "", clBlue);
  148.     Chart6->Series[0]->AddXY(i, odf2[i], "", clBlue);
  149.   }
  150.  
  151. //  Görüntüler ekrana yansıtılıyor
  152.   Image2->Width=pBitmap2->Width;
  153.   Image2->Height=pBitmap2->Height;
  154.   Image2->Picture->Assign(pBitmap2);
  155.  
  156. }
  157. //---------------------------------------------------------------------------
  158.  
  159. void __fastcall TForm1::FormCreate(TObject *Sender)
  160. {
  161. //  hist. matching'de istenilen oyf ve odf. sabit olduğundan sadece form oluşturuluyorken hesaplanıyor
  162.   for(int i=0; i<256; i++)
  163.     oyf_desired[i]=(double)((double)i)/(255*128);
  164.  
  165.   odf_desired[0]=oyf_desired[0];
  166.   for(int i=1; i<256; i++)
  167.     odf_desired[i]=oyf_desired[i]+odf_desired[i-1];
  168.  
  169. }
  170. //---------------------------------------------------------------------------
  171.  
  172.  

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."

 

Üye Giriş Formu



Kimler Çevrimiçi

Yok