Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "lipa.h"
- #include <iostream>
- #include <string>
- #include <conio.h>
- using namespace std;
- struct lista
- {
- int liczba;
- lista *next;
- };
- void rgbtogray(Image3CH& rgbImg, Image1CH& grayImg) // Zamiana obrazu z kolorowego na monochromatyczny (skala szarości, brak uwzglednienia czerwonego dla lepszego kontrastu)
- {
- if (rgbImg.width() == grayImg.width() && rgbImg.height() == grayImg.height())
- {
- for (int i = 0; i < rgbImg.width(); i++)
- {
- for (int j = 0; j < rgbImg.height(); j++)
- {
- grayImg(i, j).Intensity() = (rgbImg(i, j).Green() + rgbImg(i, j).Blue()) / 2;
- }
- }
- }
- else
- {
- cerr << "Rozne rozmiary obrazow!" << endl;
- return;
- }
- }
- void filtrmedianowy(Image1CH& in, Image1CH& out, int rozmiar) // Pozbycie się noisa
- {
- double *T = new double[rozmiar*rozmiar];
- int indeks = 0;
- int promien = (rozmiar - 1) / 2;
- double min = 1;
- double pom = 0;
- int wsk = 0;
- for (int i = promien; i < in.width() - promien; i++)
- {
- for (int j = promien; j < in.height() - promien; j++)
- {
- for (int p = -promien; p < promien + 1; p++)
- {
- for (int q = -promien; q < promien + 1; q++)
- {
- T[indeks] = in(i + p, j + q).Intensity();
- indeks++;
- }
- }
- indeks = 0;
- for (int k = 0; k < rozmiar*rozmiar; k++)
- {
- pom = T[k];
- for (int l = k; l < rozmiar*rozmiar; l++)
- {
- if (T[l] <= min)
- {
- min = T[l];
- wsk = l;
- }
- }
- T[k] = T[wsk];
- T[wsk] = pom;
- min = 1;
- }
- out(i, j).Intensity() = T[((rozmiar*rozmiar) - 1) / 2];
- }
- }
- delete[] T;
- }
- void binaryzacja(Image1CH& in, Image1CH& out) // Binaryzacja z wyznaczeniem progu
- {
- double p = 0;
- double min1 = 1;
- double max = 0;
- for (int i = 0; i < in.width(); i++)
- {
- for (int j = 0; j < in.height(); j++)
- {
- if (in(i, j).Intensity() > max)
- {
- max = in(i, j).Intensity();
- }
- if (in(i, j).Intensity() < min1)
- {
- min1 = in(i, j).Intensity();
- }
- }
- }
- p = ((max - min1) / 2) - 0.05;
- for (int i = 1; i < in.width() - 1; i++)
- {
- for (int j = 1; j < in.height() - 1; j++)
- {
- if (in(i, j).Intensity() > p)
- {
- out(i, j).Intensity() = 1;
- }
- else
- {
- out(i, j).Intensity() = 0;
- }
- }
- }
- for (int a = 0; a < in.width(); a++)
- {
- out(a, 0).Intensity() = 0;
- out(a, in.height() - 1).Intensity() = 0;
- out(a, 1).Intensity() = 0;
- out(a, in.height() - 2).Intensity() = 0;
- }
- for (int b = 0; b < in.height(); b++)
- {
- out(0, b).Intensity() = 0;
- out(in.width() - 1, b).Intensity() = 0;
- out(1, b).Intensity() = 0;
- out(in.width() - 2, b).Intensity() = 0;
- }
- }
- void dylatacja(Image1CH& in, Image1CH& out)
- {
- bool zmiana = false;
- for (int i = 1; i < in.width() - 1; i++)
- {
- for (int j = 1; j < in.height() - 1; j++)
- {
- if (in(i, j).Intensity() == 1)
- {
- for (int p = -1; p < 2; p++)
- {
- for (int q = -1; q < 2; q++)
- {
- if (in(i + p, j + q).Intensity() == 0)
- {
- zmiana = true;
- }
- }
- }
- }
- if (zmiana == true)
- {
- out(i, j).Intensity() = 0;
- }
- else
- {
- out(i, j).Intensity() = in(i, j).Intensity();
- }
- zmiana = false;
- }
- }
- }
- void erozja(Image1CH& in, Image1CH& out)
- {
- bool zmiana = false;
- for (int i = 1; i < in.width() - 1; i++)
- {
- for (int j = 1; j < in.height() - 1; j++)
- {
- if (in(i, j).Intensity() == 0)
- {
- for (int p = -1; p < 2; p++)
- {
- for (int q = -1; q < 2; q++)
- {
- if (in(i + p, j + q).Intensity() == 1)
- {
- zmiana = true;
- }
- }
- }
- }
- if (zmiana == true)
- {
- out(i, j).Intensity() = 1;
- }
- else
- {
- out(i, j).Intensity() = in(i, j).Intensity();
- }
- zmiana = false;
- }
- }
- }
- void segmentacja(Image1CH& in, int** out, bool tryb) // segmentacja top down, bottom up, top down
- {
- int a = 1;
- double szukana = 0;
- if (tryb == 1)
- {
- szukana = 1;
- }
- if (tryb == 0)
- {
- szukana = 0;
- }
- for (int i = 0; i < in.width(); i++)
- {
- for (int j = 0; j < in.height(); j++)
- {
- if (in(i, j).Intensity() == szukana)
- {
- out[i][j] = a;
- a++;
- }
- else
- {
- out[i][j] = 0;
- }
- }
- }
- int min = in.width()*in.height();
- for (int i = 1; i < in.width() - 1; i++)
- {
- for (int j = 1; j < in.height() - 1; j++)
- {
- for (int p = -1; p < 2; p++)
- {
- for (int q = -1; q < 2; q++)
- {
- if (out[i + p][j + q] < min && out[i + p][j + q] != 0)
- {
- min = out[i + p][j + q];
- }
- }
- }
- for (int k = -1; k < 2; k++)
- {
- for (int l = -1; l < 2; l++)
- {
- if (out[i + k][j + l] != 0)
- {
- out[i + k][j + l] = min;
- }
- }
- }
- min = in.width()*in.height();
- }
- }
- min = in.width()*in.height();
- for (int i = in.width() - 2; i > 0; i--)
- {
- for (int j = in.height() - 2; j > 0; j--)
- {
- for (int p = 1; p > -2; p--)
- {
- for (int q = 1; q > -2; q--)
- {
- if (out[i + p][j + q] < min && out[i + p][j + q] != 0)
- {
- min = out[i + p][j + q];
- }
- }
- }
- for (int k = 1; k > -2; k--)
- {
- for (int l = 1; l > -2; l--)
- {
- if (out[i + k][j + l] != 0)
- {
- out[i + k][j + l] = min;
- }
- }
- }
- min = in.width()*in.height();
- }
- }
- min = in.width()*in.height();
- for (int i = 1; i < in.width() - 1; i++)
- {
- for (int j = 1; j < in.height() - 1; j++)
- {
- for (int p = -1; p < 2; p++)
- {
- for (int q = -1; q < 2; q++)
- {
- if (out[i + p][j + q] < min && out[i + p][j + q] != 0)
- {
- min = out[i + p][j + q];
- }
- }
- }
- for (int k = -1; k < 2; k++)
- {
- for (int l = -1; l < 2; l++)
- {
- if (out[i + k][j + l] != 0)
- {
- out[i + k][j + l] = min;
- }
- }
- }
- min = in.width()*in.height();
- }
- }
- }
- void segregacja(Image1CH& in, int** T, lista *g, lista *g1, bool kol) // Zapisanie do jednej listy wszystkich elementow, a do drugiej tych ktore nas interesuja
- {
- lista *p = g;
- lista *p1 = g;
- lista *p2 = g;
- lista *p3 = g;
- lista *pomo1 = g1;
- lista *pomo2 = g1;
- lista *pomo3 = g1;
- bool x = 0;
- bool y = 0;
- int licznik = 0;
- int licznik1 = 0;
- int minimum = 1300;
- int aku = 0;
- for (int i = 0; i < in.width(); i++)
- {
- for (int j = 0; j < in.height(); j++)
- {
- do
- {
- if (T[i][j] == p->liczba)
- {
- x = 1;
- }
- p = p->next;
- } while (p != 0);
- p = g;
- if (x == 0)
- {
- p1->liczba = T[i][j];
- p1->next = new lista;
- p1 = p1->next;
- p1->liczba = 0;
- p1->next = 0;
- }
- x = 0;
- }
- }
- while (p2 != 0)
- {
- if (p2->liczba > 0 && p2->liczba != 0)
- {
- licznik++;
- cerr << p2->liczba << '\t';
- }
- p2 = p2->next;
- }
- if (kol == 1)
- {
- cerr << endl << "Wykryto " << licznik << " bialych elementow" << endl;
- }
- if (kol == 0)
- {
- cerr << endl << "Wykryto " << licznik << " czarnych elementow" << endl;
- }
- while (p3 != 0)
- {
- if (p3->liczba > 0)
- {
- for (int u = 0; u < in.width(); u++)
- {
- for (int v = 0; v < in.height(); v++)
- {
- if (T[u][v] == p3->liczba)
- {
- aku++;
- }
- }
- }
- if (aku < minimum)
- {
- for (int k = 0; k < in.width(); k++)
- {
- for (int l = 0; l < in.height(); l++)
- {
- if (T[k][l] == p3->liczba)
- {
- T[k][l] = 0;
- }
- }
- }
- }
- }
- aku = 0;
- p3 = p3->next;
- }
- for (int n = 0; n < in.width(); n++)
- {
- for (int m = 0; m < in.height(); m++)
- {
- do
- {
- if (T[n][m] == pomo1->liczba)
- {
- y = 1;
- }
- pomo1 = pomo1->next;
- } while (pomo1 != 0);
- pomo1 = g1;
- if (y == 0)
- {
- if (kol == 1)
- {
- pomo2->liczba = T[n][m];
- pomo2->next = new lista;
- pomo2 = pomo2->next;
- pomo2->liczba = 0;
- pomo2->next = 0;
- }
- else if (kol == 0)
- {
- if (T[n][m] != 1)
- {
- pomo2->liczba = T[n][m];
- pomo2->next = new lista;
- pomo2 = pomo2->next;
- pomo2->liczba = 0;
- pomo2->next = 0;
- }
- }
- }
- y = 0;
- }
- }
- while (pomo3 != 0)
- {
- if (pomo3->liczba > 0 && pomo3->liczba != 0)
- {
- licznik1++;
- cerr << pomo3->liczba << '\t';
- }
- pomo3 = pomo3->next;
- }
- cerr << endl;
- if (kol == 1)
- {
- cerr << endl << endl << "Wykryto " << licznik1 << " docelowych bialych elementow" << endl << endl;
- }
- if (kol == 0)
- {
- cerr << endl << endl << "Wykryto " << licznik1 << " docelowych czarnych elementow" << endl << endl;
- }
- }
- void przynaleznosc(int & f1, int & f2, int & f3, int & f4, int & f1k, int & f2k, int & f3k, int & f4k, lista *gc, lista *gb, int** Tc, int** Tb, Image1CH& in, Image3CH& kolor) // Sprawdzenie na jakiej karcie lezy badany element, zliczenie go i rozpoznanie
- {
- lista *pomc = gc;
- lista *pomb = gb;
- bool x = 0;
- double srednia = 0;
- double suma = 0;
- int d = 0;
- int szerokosc = 0;
- int k = 3;
- int progs = 53;
- double progr = 0.3;
- while (pomc->next->next != 0)
- {
- for (int i = 3; i < in.width() - 3; i++)
- {
- for (int j = 3; j < in.height() - 3; j++)
- {
- if (Tc[i][j] == pomc->next->liczba)
- {
- for (int k = -2; k < 3; k++)
- {
- for (int l = -2; l < 3; l++)
- {
- if (Tc[i + k][j + l] == 0 && x == 0)
- {
- if (pomb->next->liczba == Tb[i + k][j + l])
- {
- for (int n = 1; n < 4; n++)
- {
- for (int m = 1; m < 4; m++)
- {
- if (kolor(i + n, j + m).Red() != 1 && Tc[i + n, j + m] != 0)
- {
- suma = suma + kolor(i + n, j + m).Red();
- d++;
- }
- }
- }
- srednia = suma / d;
- if (srednia < progr)
- {
- f1k = 1;
- }
- if (srednia >= progr)
- {
- for (int u = 3; u < in.width() - 3; u++)
- {
- for (int v = 3; v < in.height() - 3; v++)
- {
- if (Tc[u][v] != 0 && Tc[u][v] != 1 && Tb[u - 1][v] == pomb->next->liczba)
- {
- for (int z = 0; z < 100; z++)
- {
- if (Tc[u + z][v] == Tc[u][v])
- {
- szerokosc++;
- }
- }
- u = in.width();
- v = in.height();
- }
- }
- }
- if (szerokosc < progs)
- {
- f1k = 3;
- }
- if (szerokosc >= progs)
- {
- f1k = 2;
- }
- szerokosc = 0;
- k = 1;
- }
- d = 0;
- suma = 0;
- srednia = 0;
- f1++;
- }
- else if (pomb->next->next->liczba == Tb[i + k][j + l])
- {
- for (int n = 1; n < 4; n++)
- {
- for (int m = 1; m < 4; m++)
- {
- if (kolor(i + n, j + m).Red() != 1 && Tc[i + n, j + m] != 0)
- {
- suma = suma + kolor(i + n, j + m).Red();
- d++;
- }
- }
- }
- srednia = suma / d;
- if (srednia < progr)
- {
- f2k = 1;
- }
- if (srednia >= progr)
- {
- for (int u = 3; u < in.width() - 3; u++)
- {
- for (int v = 3; v < in.height() - 3; v++)
- {
- if (Tc[u][v] != 0 && Tc[u][v] != 1 && Tb[u - 1][v] == pomb->next->next->liczba)
- {
- for (int z = 0; z < 100; z++)
- {
- if (Tc[u + z][v] == Tc[u][v])
- {
- szerokosc++;
- }
- }
- u = in.width();
- v = in.height();
- }
- }
- }
- if (szerokosc < progs)
- {
- f2k = 3;
- }
- if (szerokosc >= progs)
- {
- f2k = 2;
- }
- szerokosc = 0;
- k = 1;
- }
- d = 0;
- suma = 0;
- srednia = 0;
- f2++;
- }
- else if (pomb->next->next->next->liczba == Tb[i + k][j + l])
- {
- for (int n = 1; n < 4; n++)
- {
- for (int m = 1; m < 4; m++)
- {
- if (kolor(i + n, j + m).Red() != 1 && Tc[i + n, j + m] != 0)
- {
- suma = suma + kolor(i + n, j + m).Red();
- d++;
- }
- }
- }
- srednia = suma / d;
- if (srednia < progr)
- {
- f3k = 1;
- }
- if (srednia >= progr)
- {
- for (int u = 3; u < in.width() - 3; u++)
- {
- for (int v = 3; v < in.height() - 3; v++)
- {
- if (Tc[u][v] != 0 && Tc[u][v] != 1 && Tb[u - 1][v] == pomb->next->next->next->liczba)
- {
- for (int z = 0; z < 100; z++)
- {
- if (Tc[u + z][v] == Tc[u][v])
- {
- szerokosc++;
- }
- }
- u = in.width();
- v = in.height();
- }
- }
- }
- if (szerokosc < progs)
- {
- f3k = 3;
- }
- if (szerokosc >= progs)
- {
- f3k = 2;
- }
- szerokosc = 0;
- k = 1;
- }
- d = 0;
- suma = 0;
- srednia = 0;
- f3++;
- }
- else if (pomb->next->next->next->next->liczba == Tb[i + k][j + l])
- {
- for (int n = 1; n < 4; n++)
- {
- for (int m = 1; m < 4; m++)
- {
- if (kolor(i + n, j + m).Red() != 1 && Tc[i + n, j + m] != 0)
- {
- suma = suma + kolor(i + n, j + m).Red();
- d++;
- }
- }
- }
- srednia = suma / d;
- if (srednia < progr)
- {
- f4k = 1;
- }
- if (srednia >= progr)
- {
- for (int u = 3; u < in.width() - 3; u++)
- {
- for (int v = 3; v < in.height() - 3; v++)
- {
- if (Tc[u][v] != 0 && Tc[u][v] != 1 && Tb[u - 1][v] == pomb->next->next->next->next->liczba)
- {
- for (int z = 0; z < 100; z++)
- {
- if (Tc[u + z][v] == Tc[u][v])
- {
- szerokosc++;
- }
- }
- u = in.width();
- v = in.height();
- }
- }
- }
- if (szerokosc < progs)
- {
- f4k = 3;
- }
- if (szerokosc >= progs)
- {
- f4k = 2;
- }
- szerokosc = 0;
- k = 1;
- }
- d = 0;
- suma = 0;
- srednia = 0;
- f4++;
- }
- k = 3;
- l = 3;
- x = 1;
- }
- }
- }
- }
- }
- }
- x = 0;
- pomc = pomc->next;
- }
- }
- void wypisz(int c1, int c2, int c3, int c4, int k1, int k2, int k3, int k4) // Wypisanie wlasnosci kart
- {
- string kolor1;
- string kolor2;
- string kolor3;
- string kolor4;
- if (k1 == 1)
- {
- kolor1 = "pik";
- }
- if (k1 == 2)
- {
- kolor1 = "kier";
- }
- if (k1 == 3)
- {
- kolor1 = "karo";
- }
- if (k2 == 1)
- {
- kolor2 = "pik";
- }
- if (k2 == 2)
- {
- kolor2 = "kier";
- }
- if (k2 == 3)
- {
- kolor2 = "karo";
- }
- if (k3 == 1)
- {
- kolor3 = "pik";
- }
- if (k3 == 2)
- {
- kolor3 = "kier";
- }
- if (k3 == 3)
- {
- kolor3 = "karo";
- }
- if (k4 == 1)
- {
- kolor4 = "pik";
- }
- if (k4 == 2)
- {
- kolor4 = "kier";
- }
- if (k4 == 3)
- {
- kolor4 = "karo";
- }
- cerr << endl << "Pierwsza karta to: " << c1 << '\t' << kolor1 << endl;
- cerr << endl << "Druga karta to : " << c2 << '\t' << kolor2 << endl;
- cerr << endl << "Trzecia karta to : " << c3 << '\t' << kolor3 << endl;
- cerr << endl << "Czwarta karta to : " << c4 << '\t' << kolor4 << endl;
- }
- void program(int f1, int f2, int f3, int f4) // Funkcja z zadania
- {
- double srednia = 0;
- double suma = 0;
- suma = f1 + f2 + f3 + f4;
- srednia = (suma) / 4;
- cerr << endl << "Srednia: " << srednia << endl;
- if (f1 > srednia)
- {
- cerr << endl << "Pierwsza karta ma wartosc wieksza od sredniej." << endl << endl;
- }
- if (f2 > srednia)
- {
- cerr << endl << "Druga karta ma wartosc wieksza od sredniej." << endl << endl;
- }
- if (f3 > srednia)
- {
- cerr << endl << "Trzecia karta ma wartosc wieksza od sredniej." << endl << endl;
- }
- if (f4 > srednia)
- {
- }cerr << endl << "Czwarta karta ma wartosc wieksza od sredniej." << endl << endl;
- }
- int main()
- {
- Image3CH Colour(1928, 1448);
- Image1CH Grayscale(1928, 1448);
- Image1CH Median(1928, 1448);
- Image1CH Binary(1928, 1448);
- Image1CH Binaryd1(1928, 1448);
- Image1CH Binaryd1e1(1928, 1448);
- Image1CH Binaryd1e2(1928, 1448);
- Image1CH Binaryd2e2(1928, 1448);
- Image1CH Binaryd2e3(1928, 1448);
- Image1CH Binaryd3e3(1928, 1448);
- Image1CH Binaryd4e3(1928, 1448);
- Image1CH BinaryMorfo(1928, 1448);
- int obraz = 1;
- cerr << "Wpisz liczbe, aby wczytac odpowiedni obraz" << endl << "1 - Idealny" << endl << "2 - Noised" << endl << "3 - Gradient" << endl << "4 - Blurred" << endl << endl;
- cin >> obraz;
- cerr << endl << endl;
- while (obraz != 1 && obraz != 2 && obraz != 3 && obraz != 4)
- {
- cerr << endl << "Nieprawidlowa liczba, wpisz ponownie:" << endl;
- cin >> obraz;
- cerr << endl;
- }
- if (obraz == 1)
- {
- Colour.LoadImage("img\\ideal.png", LPL_LOAD_FITTED);
- }
- if (obraz == 2)
- {
- Colour.LoadImage("img\\noised.png", LPL_LOAD_FITTED);
- }
- if (obraz == 3)
- {
- Colour.LoadImage("img\\gradient.png", LPL_LOAD_FITTED);
- }
- if (obraz == 4)
- {
- Colour.LoadImage("img\\blurred.png", LPL_LOAD_FITTED);
- }
- Colour.ShowImage("Kolor");
- rgbtogray(Colour, Grayscale);
- Grayscale.ShowImage("Szary");
- int rozmiar = 0;
- cerr << "Podaj rozmiar maski filtru medianowego :" << endl;
- cin >> rozmiar;
- while (rozmiar % 2 == 0)
- {
- cerr << "Rozmiar musi byc liczba nieparzysta! Podaj ponownie: " << endl;
- cin >> rozmiar;
- cerr << endl;
- }
- filtrmedianowy(Grayscale, Median, rozmiar);
- Median.ShowImage("Po_Medianowym");
- binaryzacja(Median, Binary);
- Binary.ShowImage("Binarny");
- dylatacja(Binary, Binaryd1);
- erozja(Binaryd1, Binaryd1e1);
- erozja(Binaryd1e1, Binaryd1e2);
- dylatacja(Binaryd1e2, Binaryd2e2);
- erozja(Binaryd2e2, Binaryd2e3);
- dylatacja(Binaryd2e3, Binaryd3e3);
- dylatacja(Binaryd3e3, Binaryd4e3);
- erozja(Binaryd4e3, BinaryMorfo);
- BinaryMorfo.ShowImage("Binarny_Po_Morfo");
- int **Tsc = new int *[BinaryMorfo.width()];
- for (int i = 0; i<BinaryMorfo.width(); i++)
- {
- Tsc[i] = new int[BinaryMorfo.height()];
- }
- int **Tsb = new int *[BinaryMorfo.width()];
- for (int i = 0; i<BinaryMorfo.width(); i++)
- {
- Tsb[i] = new int[BinaryMorfo.height()];
- }
- segmentacja(BinaryMorfo, Tsc, 0);
- segmentacja(BinaryMorfo, Tsb, 1);
- lista *glowabialy = new lista;
- lista *pombialy = glowabialy;
- glowabialy->next = 0;
- glowabialy->liczba = -1;
- lista *glowa1bialy = new lista;
- lista *pom1bialy = glowa1bialy;
- lista *pom2bialy = glowa1bialy;
- glowa1bialy->next = 0;
- glowa1bialy->liczba = -1;
- bool kolor = 1;
- segregacja(BinaryMorfo, Tsb, pombialy, pom1bialy, kolor);
- lista *glowaczarny = new lista;
- lista *pomczarny = glowaczarny;
- glowaczarny->next = 0;
- glowaczarny->liczba = -1;
- lista *glowa1czarny = new lista;
- lista *pom1czarny = glowa1czarny;
- lista *pom2czarny = glowa1czarny;
- glowa1czarny->next = 0;
- glowa1czarny->liczba = -1;
- kolor = 0;
- segregacja(BinaryMorfo, Tsc, pomczarny, pom1czarny, kolor);
- int figura1 = 0;
- int figura2 = 0;
- int figura3 = 0;
- int figura4 = 0;
- int figura1kolor; // 1-pik 2-kier 3-karo
- int figura2kolor;
- int figura3kolor;
- int figura4kolor;
- przynaleznosc(figura1, figura2, figura3, figura4, figura1kolor, figura2kolor, figura3kolor, figura4kolor, pom2czarny, pom2bialy, Tsc, Tsb, BinaryMorfo, Colour);
- wypisz(figura1, figura2, figura3, figura4, figura1kolor, figura2kolor, figura3kolor, figura4kolor);
- program(figura1, figura2, figura3, figura4);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement