Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "lipa.h"
- #include <iostream>
- //LipaLib - Learning Image Processing Autonomic Library
- // =========== FUNCTION EXAMPLE =================//
- void rgbTogray(Image3CH& rgbImg, Image1CH& grayImg) // arguments with & because we want to save changes to images after performing funtion
- {
- //Check if image sizes are equal
- if (rgbImg.width() == grayImg.width() && rgbImg.height() == grayImg.height())
- {
- for (int i = 0; i < rgbImg.width(); i++) //iterate by rows
- for (int j = 0; j < rgbImg.height(); j++) //iterate by cols
- {
- grayImg(i, j).Intensity() = (rgbImg(i, j).Red() + rgbImg(i, j).Green() + rgbImg(i, j).Blue()) / 3; // I = (1/3)*(R+G+B) (i,j) - (number of row, number of col)
- }
- }
- else
- {
- std::cerr << "Image sizes mismatch" << std::endl; //print error
- return;
- }
- }
- // ==============================================//
- void binaryzacja(Image1CH& in, Image1CH& out, double treshold) //Funckja odpowiedzialna za binaryzację obrazu, gdzie parametr treshold pozwala na wybranie progu binaryzacji, aby uzyskać jedynie potrzebne informacje z obrazu.
- {
- if (in.width() == out.width() && in.height() == out.height())
- {
- for (int i = 0; i < in.width(); i++) //iterate by rows
- for (int j = 0; j < in.height(); j++) //iterate by cols
- {
- if (in(i, j).I() > treshold) // Gdy wartość intensywności piksela jest powyżej progu, przypisuje wartość 1 - odpowiada koloru białemu.
- {
- out(i, j).I() = 1;
- }
- else
- out(i, j).I() = 0; // Gdy wartość intensywności piksela jest poniżej progu, przypisuje wartość 0 - odpowiada koloru czarnemu.
- }
- }
- else
- {
- std::cerr << "Image sizes mismatch" << std::endl; //print error
- return;
- }
- }
- void segmentacja(Image1CH& in, Image1CH& out, int parametr) // Funkcja odpowiadająca za segmentację, gdzie parametr pozwala na wybór segmentowanych obiektów (karty - 1 lub symbole - 0).
- {
- //Tablica dynamiczna przechowująca etykiety.
- double nr_etykiety = 1;
- int **Etykiety = new int*[in.width()];
- for (int i = 0; i < in.width(); i++)
- {
- Etykiety[i] = new int[in.height()];
- }
- //Przypisanie każdemu z pikseli obrazu etykiety oraz wpisanie jej do tablicy dynamicznej.
- for (int i = 0; i < in.width(); i++)
- for (int j = 0; j < in.height(); j++)
- {
- if (in(i, j).I() == parametr)
- {
- Etykiety[i][j] = nr_etykiety;
- nr_etykiety++;
- }
- else
- {
- Etykiety[i][j] = 0;
- }
- }
- bool first1 = true;
- bool first2 = true;
- int z = 0;
- for(int i=0; i<in.width();i++)
- for (int j = 0; j < in.height(); j++)
- {
- if (i == 0 || j == 0 || i == in.width() - 1 || j == in.height() - 1)
- {
- Etykiety[i][j] = 1;
- }
- }
- while(first1 == true || first2 == true )
- {
- //Segmentacja Top-down. Ustalenie wartości etykiet każdego z pikseli na podstawie sąsiedztwa.
- first1 = false;
- for (int i = 1; i < (in.width() - 1); i++)
- {
- for (int j = 1; j < (in.height() - 1); j++)
- {
- int min = in.width()*in.height();
- if (Etykiety[i][j] > 0) //Sprawdzenie etykiet, gdyż elementy z etykietą 0 nie są poddane segmentacji .
- {
- for (int k = 0; k < 3; k++) // 3 odpowiada rozmiarowi otoczenia jakie jest rozpatrywane.
- {
- for (int l = 0; l < 3; l++)
- {
- if (Etykiety[i + k - 1][j + l - 1] > 0)//Sprawdzenie czy sąsiad nie jest 0.
- {
- if (Etykiety[i + k - 1][j + l - 1] < min) //Sprawdzenie czy kolejne etykiety sąsiadów są mniejsze od minimalnego.
- {
- min = Etykiety[i + k - 1][j + l - 1]; //Uaktulnienie najmniejszej wartości etykiety sąsiadów.
- }
- }
- }
- }
- if (Etykiety[i][j] != min)
- {
- Etykiety[i][j] = min;
- first1 = true;
- }
- }
- }
- }
- //Segmentacja Bottom-up. Ustalenie wartości etykiet każdego z pikseli na podstawie sąsiedztwa.
- first2 = false;
- for (int i = (in.width() - 2); i > 0; i--)
- {
- for (int j = (in.height() - 2); j > 0; j--)
- {
- int min = in.width()*in.height();
- if (Etykiety[i][j] > 0)
- {
- for (int k = 2; k > -1; k--)
- {
- for (int l = 2; l > -1; l--)
- {
- if (Etykiety[i + k - 1][j + l - 1] > 0)
- {
- if (Etykiety[i + k - 1][j + l - 1] < min) //Sprawdzenie czy kolejne etykiety sąsiadów są mniejsze od minimalnego.
- {
- min = Etykiety[i + k - 1][j + l - 1];
- }
- }
- }
- }
- if (Etykiety[i][j] != min)
- {
- Etykiety[i][j] = min;
- z++;
- first2 = true;
- }
- }
- }
- }
- }
- //Wpisanie etykiet do obrazu wyjściowego.
- for (int i = 0; i < in.width(); i++)
- for (int j = 0; j < in.height(); j++)
- {
- out(i, j).I() = Etykiety[i][j];
- }
- //Usuwanie tablicy
- for (int i = 0; i < in.width(); i++)
- {
- delete[] Etykiety[i];
- }
- delete[] Etykiety;
- }
- void dylatacja(Image1CH& in, Image1CH& out)
- {
- //dylatacja
- for (int i = 0; i < (in.width() - 1); i++)
- {
- for (int j = 0; j < (in.height() - 1); j++)
- {
- if (i < 1 || i>in.width() - 1 - 1 || j<1 || j>in.height() - 1 - 1)
- {
- out(i, j).I() = in(i, j).I();
- }
- else
- {
- if (in(i, j).I() == 1)
- {
- for (int k = 0; k < 3; k++)
- {
- for (int l = 0; l < 3; l++)
- {
- out((i + k - 1), (j + l - 1)).I() = 1;
- }
- }
- }
- }
- }
- }
- }
- void erozja(Image1CH& in, Image1CH& out)
- {
- //erozja
- for (int i = 0; i < (in.width() - 1); i++)
- {
- for (int j = 0; j < (in.height() - 1); j++)
- {
- if (i < 1 || i>in.width() - 1 - 1 || j<1 || j>in.height() - 1 - 1)
- {
- out(i, j).I() = in(i, j).I();
- }
- else
- {
- double min = 1;
- for (int k = 0; k < 3; k++)
- {
- for (int l = 0; l < 3; l++)
- {
- if (in((i + k - 1), (j + l - 1)).I() < min)
- {
- min = in((i + k - 1), (j + l - 1)).I();
- }
- }
- }
- out(i, j).I() = min;
- }
- }
- }
- }
- int liczba(Image1CH& in) // Funkcja licząca ilość segmentów w obrazie.
- {
- //Stworzenie nowej tablicy dynamicznej przechowującej nowe, posegregowane etykiety.
- int size = 5000;
- int *Segment = new int[size];
- Segment[0] = 0;
- int licz = 0;
- int powtorzenie;
- for (int i = 0; i < in.width(); i++)
- for (int j = 0; j < in.height(); j++)
- {
- powtorzenie = 0;
- if (in(i, j).I() > 0)
- {
- if (licz == 0)
- {
- Segment[licz] = in(i, j).I();
- licz++;
- }
- for (int m = 0; m < licz; m++)
- {
- if (Segment[m] == in(i, j).I())
- {
- powtorzenie++;
- }
- }
- if (powtorzenie == 0)
- {
- Segment[licz] = in(i, j).I();
- licz++;
- }
- }
- }
- delete[] Segment;
- return licz-2;
- }
- void wydzielenie(Image1CH& in1, Image1CH& in2, Image3CH& out)
- {
- for (int i = 0; i < in2.width(); i++)
- for (int j = 0; j < in2.height(); j++)
- {
- if (in2(i, j).I() == 1)
- {
- out(i, j).R() = 0;
- out(i, j).G() = 0;
- out(i, j).B() = 0;
- }
- else if (in2(i, j).I() > 1)
- {
- out(i, j).R() = in2(i, j).I();
- }
- else if (in1(i, j).I() > 0)
- {
- for (int k = 0; k < 3; k++)
- for (int l = 0; l < 3; l++)
- {
- if (in2(k, l).I() > 0)
- {
- out(i, j).B() = in1(k, l).I();
- out(i, j).G() = in2(k, l).I();
- }
- }
- }
- }
- }
- void pola(Image3CH& in, int*& pole)
- {
- int z = 0;
- int powtorzenie = 0;
- int size = 50;
- int *Tab = new int[size];
- Tab[0] = 0;
- for (int i = 0; i < in.width(); i++)
- for (int j = 0; j < in.height(); j++)
- {
- powtorzenie = 0;
- if (in(i, j).B() > 0 && in(i, j).G() > 0)
- {
- if (z == 0)
- {
- pole[z] = 1;
- Tab[z] = in(i, j).B();
- z++;
- }
- for (int k = 0; k < z; k++)
- {
- if (Tab[k] == in(i, j).B())
- {
- pole[k]++;
- powtorzenie++;
- }
- }
- if (powtorzenie == 0)
- {
- pole[z] = 1;
- Tab[z] = in(i, j).B();
- z++;
- }
- }
- }
- delete[] Tab;
- }
- //void unsharpMask(Image3CH &in, Image3CH &out)
- //{
- // double kernel[5][5] = { 1, 1, 1, 1, 1,
- // 1, 2, 4, 2, 1,
- // 2, 4, 8, 4, 2,
- // 1, 2, 4, 2, 1,
- // 1, 1, 1, 1, 1 };
- // double accumulatorR = 0;
- // double accumulatorG = 0;
- // double accumulatorB = 0;
- //
- // if (in.width() == out.width() && in.height() == out.height())
- // {
- // for (int i = 2; i < in.width() - 2; i++) //iterate by rows
- // for (int j = 2; j < in.height() - 2; j++) //iterate by cols
- // {
- // accumulatorR = 0;
- // accumulatorG = 0;
- // accumulatorB = 0;
- // for (int k = 0; k < 5; k++) //iterate by rows
- // for (int l = 0; l < 5; l++) //iterate by cols
- // {
- // accumulatorR += in(i + k - 2, j + l - 2).R() * kernel[k][l];
- // accumulatorG += in(i + k - 2, j + l - 2).G() * kernel[k][l];
- // accumulatorB += in(i + k - 2, j + l - 2).B() * kernel[k][l];
- // }
- // out(i, j).R() = 0.5*(in(i,j).R() - (accumulatorR / 52))+in(i,j).R();
- // out(i, j).G() = 0.5*(in(i, j).G() - (accumulatorG / 52)) + in(i, j).G();
- // out(i, j).B() = 0.5*(in(i, j).B() - (accumulatorB / 52)) + in(i, j).B();
- //
- // }
- // }
- // else
- // {
- // std::cerr << "Image sizes mismatch" << std::endl; //print error
- // return;
- // }
- //
- //}
- void medianMask(Image1CH &in, Image1CH &out)
- {
- double kernel[3][3] = { 1, 1, 1,
- 1, 1, 1,
- 1, 1, 1 };
- int min = 1;
- int temp;
- int *Tab = new int[9];
- if (in.width() == out.width() && in.height() == out.height())
- {
- for (int i = 1; i < in.width() - 1; i++) //iterate by rows
- for (int j = 1; j < in.height() - 1; j++) //iterate by cols
- {
- for (int k = 0; k < 3; k++) //iterate by rows
- for (int l = 0; l < 3; l++) //iterate by cols
- {
- if (in(i + k - 1, j + l - 1).I() < min)
- {
- min = in(i + k - 1, j + l - 1).I();
- Tab[k*l] = min;
- }
- // Bubble Sorting
- if (Tab[l] > Tab[l +1])
- {
- temp = Tab[l];
- Tab[l] = Tab[l + 1];
- Tab[l + 1] = temp;
- }
- }
- out(i, j).I() = Tab[4];
- }
- }
- else
- {
- std::cerr << "Image sizes mismatch" << std::endl; //print error
- return;
- }
- }
- int main()
- {
- int liczba_segmentow1 = 0;
- int liczba_segmentow2 = 0;
- Image3CH ColourImage(1928, 1448); // Create new image Image3CH - three channels image (width,height)
- Image1CH GrayscaleImage(ColourImage.width(), ColourImage.height());
- Image1CH Binarny(ColourImage.width(), ColourImage.height());
- Image1CH Erozja(ColourImage.width(), ColourImage.height());
- Image1CH Dylatacja(ColourImage.width(), ColourImage.height());
- Image1CH Zamkniety(ColourImage.width(), ColourImage.height());
- Image1CH Segmenty1(ColourImage.width(), ColourImage.height());
- Image1CH Segmenty2(ColourImage.width(), ColourImage.height());
- Image3CH Segmenty3(ColourImage.width(), ColourImage.height());
- Image1CH Median(ColourImage.width(), ColourImage.height());
- //ColourImage.LoadImage("img\\ideal.png", LPL_LOAD_FITTED);
- //ColourImage.LoadImage("img\\blurred.png", LPL_LOAD_FITTED);
- //ColourImage.LoadImage("img\\gradient2.png", LPL_LOAD_FITTED);
- ColourImage.LoadImage("img\\noised.png", LPL_LOAD_FITTED);
- int *Pole = new int[50];
- Pole[0] = 0;
- rgbTogray(ColourImage, GrayscaleImage);
- medianMask(GrayscaleImage, Median);
- binaryzacja(GrayscaleImage, Binarny, 0.4);
- erozja(Binarny, Dylatacja);
- dylatacja(Dylatacja, Zamkniety);
- segmentacja(Zamkniety, Segmenty2, 0);
- segmentacja(Zamkniety, Segmenty1, 1);
- liczba_segmentow1 = liczba(Segmenty1);
- liczba_segmentow2 = liczba(Segmenty2);
- std::cout << " Liczba kart: " << liczba_segmentow1 << " ";
- std::cout <<" Liczba wszystkich znakow: "<< liczba_segmentow2 << " ";
- wydzielenie(Segmenty1, Segmenty2, Segmenty3);
- pola(Segmenty3, Pole);
- for (int i = 0; i < 50; i++)
- {
- std::cout << Pole[i] << " ";
- }
- //Unsharp.ShowImage("Obraz po filtracji");
- ColourImage.ShowImage("Obraz wejsciowy"); //Show image
- Binarny.ShowImage("Obraz binarny");
- Segmenty1.ShowImage("Obraz posegmentowany");
- Segmenty2.ShowImage("Obraz posegmentowany");
- Segmenty3.ShowImage("Obraz posegmentowany");
- delete[] Pole;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement