Programlama Sitesi Hazır Program | C++ Builder Java C/C++ Php .Net

Anasayfa Programlama C++ Builder Resimlere Maske Uygulamak


Resimlere Maske Uygulamak

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

Linki Görebilmeniz İçin Üye Olmanız Gerekmektedir...

Üye Kayıt

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. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma resource "*.dfm"
  12.  
  13. //Değişken atamaları yapılıyor...
  14. TForm1 *Form1;
  15. Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
  16. Graphics::TBitmap *pBitmap2 = new Graphics::TBitmap();
  17. Graphics::TBitmap *pBitmap3 = new Graphics::TBitmap();
  18. TJPEGImage *jp = new TJPEGImage();
  19. Byte *ptr, *ptr2;
  20. byte **resim;
  21. //---------------------------------------------------------------------------
  22. // Quick sort kodu
  23. void q_sort(byte *numbers, int left, int right)
  24. {
  25.   int pivot, l_hold, r_hold;
  26.  
  27.   l_hold = left;
  28.   r_hold = right;
  29.   pivot = numbers[left];
  30.   while (left < right)
  31.   {
  32.     while ((numbers[right] >= pivot) && (left < right))
  33.       right--;
  34.     if (left != right)
  35.     {
  36.       numbers[left] = numbers[right];
  37.       left++;
  38.     }
  39.     while ((numbers[left] <= pivot) && (left < right))
  40.       left++;
  41.     if (left != right)
  42.     {
  43.       numbers[right] = numbers[left];
  44.       right--;
  45.     }
  46.   }
  47.   numbers[left] = pivot;
  48.   pivot = left;
  49.   left = l_hold;
  50.   right = r_hold;
  51.   if (left < pivot)
  52.     q_sort(numbers, left, pivot-1);
  53.   if (right > pivot)
  54.     q_sort(numbers, pivot+1, right);
  55. }
  56.  
  57. //---------------------------------------------------------------------------
  58. // Basit sıralama kodu
  59. void sirala(byte *dizi, int eleman_sayisi)
  60. {
  61.   byte temp;
  62.  
  63.   for(int i=0; i<eleman_sayisi; i++)
  64.   {
  65.     for(int j=eleman_sayisi-1; j>i; j--)
  66.     {
  67.       if(dizi[j]>dizi[j-1])
  68.       {
  69.         temp=dizi[j];
  70.         dizi[j]=dizi[j-1];
  71.         dizi[j-1]=temp;
  72.       }
  73.     }
  74.   }
  75. }
  76. //---------------------------------------------------------------------------
  77. // Programın constructor'ı
  78. __fastcall TForm1::TForm1(TComponent* Owner)
  79.         : TForm(Owner)
  80. {
  81. }
  82. //---------------------------------------------------------------------------
  83.  
  84. //Resim aç butonuna tıklandığında yapılacak işlemler...
  85. void __fastcall TForm1::Button1Click(TObject *Sender)
  86. {
  87. // Diyalog kutusu ile resim alındı ve değişkenlere atandı
  88.   OpenDialog1->Execute();
  89.  
  90.   for(int i=0; i<OpenDialog1->Files->Count; i++)
  91.   {
  92.     //açılan dosya jpg mi, bmp mi?
  93.     if(OpenDialog1->FileName.SubString(OpenDialog1->FileName.Length()-2,3)=="jpg")
  94.     {
  95.       // jpeg ise TJPEGImage yapısı ile okunuyor be bmp'e atanıyor
  96.       jp->LoadFromFile(OpenDialog1->Files[0].Strings[i]);
  97.       pBitmap->Assign(jp);
  98.       pBitmap2->Assign(jp);
  99.     }
  100.     else
  101.     {
  102.       pBitmap->LoadFromFile(OpenDialog1->Files[0].Strings[i]);
  103.       pBitmap2->LoadFromFile(OpenDialog1->Files[0].Strings[i]);
  104.     }
  105.     // Açılan resimlerin 8 bit olması durumunda, yapılacak okumanın doğru olması için
  106.     pBitmap->PixelFormat=pf24bit;
  107.     pBitmap2->PixelFormat=pf24bit;
  108.   }
  109.   resim=new byte*[pBitmap->Height];
  110.   for(int i=0; i<pBitmap->Height; i++)
  111.     resim[i]=new byte[pBitmap->Width];
  112.  
  113. // Resim okunarak gri seviyeye dönüştürüldü
  114.   for (int i=0; i<pBitmap->Height; i++)
  115.   {
  116.     ptr = (Byte *)pBitmap->ScanLine[i];
  117.     for (int j=0; j<pBitmap->Width; j++)
  118.     {
  119.       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);
  120.     }
  121.   }
  122.   pBitmap2->Assign(pBitmap);
  123.   Image1->Width=pBitmap->Width;
  124.   Image1->Height=pBitmap->Height;
  125.   Image1->Picture->Assign(pBitmap);
  126. }
  127. //---------------------------------------------------------------------------
  128. //Uygula butonuna tıklandığında yapılacak işlemler
  129. void __fastcall TForm1::Button2Click(TObject *Sender)
  130. {
  131.   int mask_size=StrToInt(ComboBox2->Text.SetLength(1));
  132.   int temp, temp2;
  133.   byte *median_dizi;
  134.  
  135.   if(ComboBox1->ItemIndex==0)  //Average Filtering yapılacaksa
  136.   {
  137.     for(int i=(int)(mask_size/2); i<(pBitmap->Height-(int)(mask_size/2)); i++)
  138.     {
  139.       ptr = (Byte *)pBitmap2->ScanLine[i];
  140.       for(int j=(int)(mask_size/2); j<(pBitmap->Width-(int)(mask_size/2)); j++)
  141.       {
  142.         temp=0;
  143.         //masktaki bütün değerler toplanıyor ve ortalaması alınıyor
  144.         for(int k=-(int)(mask_size/2); k<=(int)(mask_size/2); k++)
  145.         {
  146.           for(int m=-(int)(mask_size/2); m<=(int)(mask_size/2); m++)
  147.           {
  148.             temp+=resim[i+k][j+m];
  149.           }
  150.         }
  151.         temp/=(mask_size*mask_size);
  152.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp;
  153.       }
  154.     }
  155.   }
  156.   if(ComboBox1->ItemIndex==1)  //Laplacian Filtering yapılacaksa
  157.   {
  158.     for(int i=1; i<(pBitmap->Height-1); i++)
  159.     {
  160.       ptr = (Byte *)pBitmap2->ScanLine[i];
  161.       for(int j=1; j<(pBitmap->Width-1); j++)
  162.       {
  163.         // iki tür maske de kullanılabilir (programda ilk maske tercih edilmiş)
  164.         temp=(5*resim[i][j]-(resim[i-1][j]+resim[i][j-1]+resim[i][j+1]+resim[i+1][j]));
  165. //        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]));
  166.         if(temp>255)
  167.           temp=255;
  168.         if(temp<0)
  169.           temp=0;
  170.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp;
  171.       }
  172.     }
  173.   }
  174.   if(ComboBox1->ItemIndex==2)  //Median Filtering yapılacaksa
  175.   {
  176.     median_dizi=new byte[mask_size*mask_size];
  177.     for(int i=(int)(mask_size/2); i<(pBitmap->Height-(int)(mask_size/2)); i++)
  178.     {
  179.       ptr = (Byte *)pBitmap2->ScanLine[i];
  180.       for(int j=(int)(mask_size/2); j<(pBitmap->Width-(int)(mask_size/2)); j++)
  181.       {
  182.         temp=0;
  183.         //maskenin bütün elemanları sıralanıp orta değer alınıyor
  184.         for(int k=-(int)(mask_size/2); k<=(int)(mask_size/2); k++)
  185.         {
  186.           for(int m=-(int)(mask_size/2); m<=(int)(mask_size/2); m++)
  187.           {
  188.             median_dizi[k+(mask_size/2)*mask_size+m+(mask_size/2)]=resim[i+k][j+m];
  189.           }
  190.         }
  191.         //sıralama algoritması olarak (dizi boyu küçük olduğundan) basit sıralama kullanılıyor
  192. //        q_sort(median_dizi, 0, mask_size*mask_size-1);
  193.         sirala(median_dizi, mask_size*mask_size);
  194.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=median_dizi[(int)(mask_size*mask_size/2)];
  195.       }
  196.     }
  197.   }
  198.   if(ComboBox1->ItemIndex==3)  //Sobel filtresi ise
  199.   {
  200.     for(int i=1; i<(pBitmap->Height-1); i++)
  201.     {
  202.       ptr = (Byte *)pBitmap2->ScanLine[i];
  203.       for(int j=1; j<(pBitmap->Width-1); j++)
  204.       {
  205.         //sobel maskesi uygulanıp magnitude değeri alınıyor
  206.         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]);
  207.         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]);
  208.         temp=sqrt(temp*temp+temp2*temp2);
  209.         //255'i aşan ve 0'dan küçük değerler setleniyor
  210.         if(temp>255)
  211.           temp=255;
  212.         if(temp<0)
  213.           temp=0;
  214.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp;
  215.       }
  216.     }
  217.   }
  218.   if(ComboBox1->ItemIndex==4) // skeleton işlemleri uygulanacaksa
  219.   {
  220.     // önce laplacian uygulanıyor
  221.     for(int i=1; i<(pBitmap->Height-1); i++)
  222.     {
  223.       ptr = (Byte *)pBitmap2->ScanLine[i];
  224.       for(int j=1; j<(pBitmap->Width-1); j++)
  225.       {
  226.         temp=(5*resim[i][j]-(resim[i-1][j]+resim[i][j-1]+resim[i][j+1]+resim[i+1][j]));
  227.         if(temp>255)
  228.           temp=255;
  229.         if(temp<0)
  230.           temp=0;
  231.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp;
  232.       }
  233.     }
  234.     //filtre görüntüsü kaydediliyor
  235.     for(int i=1; i<(pBitmap->Height-1); i++)
  236.     {
  237.       ptr = (Byte *)pBitmap2->ScanLine[i];
  238.       for(int j=1; j<(pBitmap->Width-1); j++)
  239.       {
  240.         resim[i][j]=ptr[j*3];
  241.       }
  242.     }
  243.     //bu görüntünün ilerde tekrar kullanılması için bir kopyası saklanıyor
  244.     pBitmap3->Assign(pBitmap2);
  245.  
  246.     //sobeli alınıyor ve kaydediliyor
  247.     for(int i=1; i<(pBitmap->Height-1); i++)
  248.     {
  249.       ptr = (Byte *)pBitmap2->ScanLine[i];
  250.       for(int j=1; j<(pBitmap->Width-1); j++)
  251.       {
  252.         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]);
  253.         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]);
  254.         temp=sqrt(temp*temp+temp2*temp2);
  255.         if(temp>255)
  256.           temp=255;
  257.         if(temp<0)
  258.           temp=0;
  259.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp;
  260.       }
  261.     }
  262.     for(int i=1; i<(pBitmap->Height-1); i++)
  263.     {
  264.       ptr = (Byte *)pBitmap2->ScanLine[i];
  265.       for(int j=1; j<(pBitmap->Width-1); j++)
  266.       {
  267.         resim[i][j]=ptr[j*3];
  268.       }
  269.     }
  270.  
  271.     //5x5 boyutunda average filtering yapılıyor ve kaydediliyor
  272.     mask_size=5;
  273.     for(int i=(int)(mask_size/2); i<(pBitmap->Height-(int)(mask_size/2)); i++)
  274.     {
  275.       ptr = (Byte *)pBitmap2->ScanLine[i];
  276.       for(int j=(int)(mask_size/2); j<(pBitmap->Width-(int)(mask_size/2)); j++)
  277.       {
  278.         temp=0;
  279.         for(int k=-(int)(mask_size/2); k<=(int)(mask_size/2); k++)
  280.         {
  281.           for(int m=-(int)(mask_size/2); m<=(int)(mask_size/2); m++)
  282.           {
  283.             temp+=resim[i+k][j+m];
  284.           }
  285.         }
  286.         temp/=(mask_size*mask_size);
  287.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=temp;
  288.       }
  289.     }
  290.     for(int i=1; i<(pBitmap->Height-1); i++)
  291.     {
  292.       ptr = (Byte *)pBitmap2->ScanLine[i];
  293.       for(int j=1; j<(pBitmap->Width-1); j++)
  294.       {
  295.         resim[i][j]=ptr[j*3];
  296.       }
  297.     }
  298.  
  299.     // laplacian görüntüsü ile çarpılıyor ve kaydediliyor
  300.     for(int i=0; i<(pBitmap->Height); i++)
  301.     {
  302.       ptr = (Byte *)pBitmap2->ScanLine[i];
  303.       ptr2 = (Byte *)pBitmap3->ScanLine[i];
  304.       for(int j=0; j<(pBitmap->Width); j++)
  305.       {
  306.         temp=resim[i][j]*ptr2[j*3]/255;
  307.         if(temp<0)
  308.           resim[i][j]=0;
  309.         if(temp>255)
  310.           resim[i][j]=255;
  311.         else
  312.           resim[i][j]=temp;
  313.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=resim[i][j];
  314.       }
  315.     }
  316.  
  317.     //orjinal resimle toplanıyor ve kaydediliyor
  318.     for(int i=0; i<(pBitmap->Height); i++)
  319.     {
  320.       ptr = (Byte *)pBitmap2->ScanLine[i];
  321.       ptr2 = (Byte *)pBitmap->ScanLine[i];
  322.       for(int j=0; j<(pBitmap->Width); j++)
  323.       {
  324.         temp=resim[i][j]+ptr2[j*3];
  325.         if(temp<0)
  326.           resim[i][j]=0;
  327.         if(temp>255)
  328.           resim[i][j]=255;
  329.         else
  330.           resim[i][j]=temp;
  331.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=resim[i][j];
  332.       }
  333.     }
  334.  
  335.     //power-low dönüşümü uygulanıyor
  336.     for(int i=0; i<(pBitmap->Height); i++)
  337.     {
  338.       ptr = (Byte *)pBitmap2->ScanLine[i];
  339.       for(int j=0; j<(pBitmap->Width); j++)
  340.       {
  341.         temp=sqrt((double)(resim[i][j])/255.0)*255;
  342.         if(temp<0)
  343.           resim[i][j]=0;
  344.         if(temp>255)
  345.           resim[i][j]=255;
  346.         else
  347.           resim[i][j]=temp;
  348.         ptr[j*3]=ptr[j*3+1]=ptr[j*3+2]=resim[i][j];
  349.       }
  350.     }
  351.  
  352.     //diğer maskelerin ana görüntü üzerine uygulanması için resetleme işlemi
  353.     for (int i=0; i<pBitmap->Height; i++)
  354.     {
  355.       ptr = (Byte *)pBitmap->ScanLine[i];
  356.       for (int j=0; j<pBitmap->Width; j++)
  357.       {
  358.         resim[i][j]=ptr[j*3];
  359.       }
  360.     }
  361.   }
  362.  
  363. //  Görüntüler ekrana yansıtılıyor
  364.   Image2->Width=pBitmap2->Width;
  365.   Image2->Height=pBitmap2->Height;
  366.   Image2->Picture->Assign(pBitmap2);
  367.  
  368. }
  369. //---------------------------------------------------------------------------
  370.  
  371.  
  372. void __fastcall TForm1::ComboBox1Change(TObject *Sender)
  373. {
  374.   // eğer laplacian, sobel ya da skeleton işlemleri uygulanacaksa mask size seçilemesin
  375.   if(ComboBox1->ItemIndex==1 || ComboBox1->ItemIndex==3 || ComboBox1->ItemIndex==4)
  376.   {
  377.     ComboBox2->ItemIndex=0;
  378.     ComboBox2->Enabled=False;
  379.   }
  380.   else
  381.   {
  382.     ComboBox2->Enabled=True;
  383.   }
  384. }
  385. //---------------------------------------------------------------------------
  386.  
  387.  
  388.  

Yorumlar (0)
Sadece kayıtlı kullanıcılar yorum yazabilir!
Son Güncelleme ( Perşembe, 07 Nisan 2011 20:33 )  

Kimler Çevrimiçi

Yok