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)
- {
- 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).Red() + rgbImg(i, j).Green() + rgbImg(i, j).Blue()) / 3;
- }
- }
- }
- else
- {
- cerr << "Rozne rozmiary obrazow!" << endl;
- return;
- }
- }
- void filtrmedianowy(Image1CH& in, Image1CH& out, int rozmiar)
- {
- 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)
- {
- double p = 0;
- double pp = 0;
- double min1 = 1;
- double min2 = 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;
- for (int i = 1; i < in.width()-1; i++)
- {
- for (int j = 1; j < in.height()-1; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- for (int l = -1; l < 2; l++)
- {
- if (in(i + k, j + l).Intensity() < min2)
- {
- min2 = in(i + k, j + l).Intensity();
- }
- }
- }
- pp = p - min2/4;
- if (in(i, j).Intensity() > pp)
- {
- out(i, j).Intensity() = 1;
- }
- else
- {
- out(i, j).Intensity() = 0;
- }
- min2 = 1;
- }
- }
- for (int a = 0; a < in.width(); a++)
- {
- out(a, 0).Intensity() = 0;
- out(a, in.height() - 1).Intensity() = 0;
- }
- for (int b = 0; b < in.height(); b++)
- {
- out(0, b).Intensity() = 0;
- out(in.width() -1, 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)
- {
- 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;
- }
- }
- }
- /*
- for (int g = 0; g < in.width(); g++)
- {
- for (int h = 0; h < in.height(); h++)
- {
- cout << out[g][h] << '\t';
- }
- cout << endl;
- }
- system("pause");
- */
- 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)
- {
- 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 =1310;
- 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)
- {
- 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 << "Wykryto " << licznik1 << " docelowych bialych elementow" << endl << endl;
- }
- if (kol == 0)
- {
- cerr << endl << "Wykryto " << licznik1 << " docelowych czarnych elementow" << endl << endl;
- }
- }
- void przynaleznosc(int& f1, int& f2, int&f3, int&f4, lista *gc, lista *gb, int** Tc, int** Tb, Image1CH& in)
- {
- lista *pomc = gc;
- lista *pomb = gb;
- int fig = 0;
- bool x = 0;
- while (pomc != 0)
- {
- for (int i = 0; i < in.width(); i++)
- {
- for (int j = 0; j < in.height(); j++)
- {
- if (Tc[i][j] == pomc->liczba)
- {
- j = in.height();
- i = in.width();
- if (Tc[i-1][j] == 0)
- {
- while (pomb != 0 && x == 0)
- {
- fig++;
- if (pomb->liczba == Tb[i - 1][j])
- {
- x = 1;
- }
- pomb = pomb->next;
- }
- }
- if (Tc[i][j-1] == 0 && x ==0)
- {
- while (pomb != 0 && x == 0)
- {
- fig++;
- if (pomb->liczba == Tb[i][j-1])
- {
- x = 1;
- }
- pomb = pomb->next;
- }
- }
- if (Tc[i + 1][j] == 0 && x == 0)
- {
- while (pomb != 0 && x == 0)
- {
- fig++;
- if (pomb->liczba == Tb[i + 1][j])
- {
- x = 1;
- }
- pomb = pomb->next;
- }
- }
- if (Tc[i][j+1] == 0 && x == 0)
- {
- while (pomb != 0 && x == 0)
- {
- fig++;
- if (pomb->liczba == Tb[i][j+1])
- {
- x = 1;
- }
- pomb = pomb->next;
- }
- }
- }
- }
- }
- pomc = pomc->next;
- x = 0;
- if (fig == 1)
- {
- f1++;
- }
- else if (fig == 2)
- {
- f2++;
- }
- else if (fig == 3)
- {
- f3++;
- }
- else if (fig == 4)
- {
- f4++;
- }
- fig = 0;
- }
- }
- void wypisz(int k1, int k2, int k3, int k4)
- {
- cerr << endl << "Pierwsza karta to :" << k1 << endl;
- cerr << endl << "Druga karta to :" << k2 << endl;
- cerr << endl << "Trzecia karta to :" << k3 << endl;
- cerr << endl << "Czwarta karta to :" << k4 << 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);
- Colour.LoadImage("img\\ideal.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;
- }
- 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;
- 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;
- 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;
- przynaleznosc(figura1, figura2, figura3, figura4, pom1czarny, pom1bialy, Tsc, Tsb, BinaryMorfo);
- wypisz(figura1, figura2, figura3, figura4);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement