Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.56 KB | None | 0 0
  1. #include "lipa.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. struct lista
  10. {
  11. int liczba;
  12. lista *next;
  13. };
  14.  
  15. void rgbtogray(Image3CH& rgbImg, Image1CH& grayImg) // Zamiana obrazu z kolorowego na monochromatyczny (skala szarości)
  16. {
  17.  
  18. if (rgbImg.width() == grayImg.width() && rgbImg.height() == grayImg.height())
  19. {
  20. for (int i = 0; i < rgbImg.width(); i++)
  21. {
  22. for (int j = 0; j < rgbImg.height(); j++)
  23. {
  24. grayImg(i, j).Intensity() = (rgbImg(i, j).Red() + rgbImg(i, j).Green() + rgbImg(i, j).Blue()) / 3;
  25. }
  26. }
  27. }
  28. else
  29. {
  30. cerr << "Rozne rozmiary obrazow!" << endl;
  31. return;
  32. }
  33. }
  34.  
  35. void filtrmedianowy(Image1CH& in, Image1CH& out, int rozmiar)
  36. {
  37. double *T = new double[rozmiar*rozmiar];
  38. int indeks = 0;
  39. int promien = (rozmiar - 1) / 2;
  40. double min = 1;
  41. double pom = 0;
  42. int wsk = 0;
  43.  
  44. for (int i = promien; i < in.width()-promien; i++)
  45. {
  46. for (int j = promien; j < in.height()-promien; j++)
  47. {
  48. for (int p = -promien; p < promien + 1; p++)
  49. {
  50. for (int q = -promien; q < promien +1; q++)
  51. {
  52. T[indeks] = in(i + p, j + q).Intensity();
  53. indeks++;
  54. }
  55. }
  56.  
  57. indeks = 0;
  58.  
  59. for (int k = 0; k < rozmiar*rozmiar; k++)
  60. {
  61. pom = T[k];
  62. for (int l = k; l < rozmiar*rozmiar; l++)
  63. {
  64. if (T[l] <= min)
  65. {
  66. min = T[l];
  67. wsk = l;
  68. }
  69. }
  70. T[k] = T[wsk];
  71. T[wsk] = pom;
  72. min = 1;
  73. }
  74.  
  75. out(i, j).Intensity() = T[((rozmiar*rozmiar)-1)/2];
  76.  
  77. }
  78. }
  79. delete[] T;
  80. }
  81.  
  82. void binaryzacja(Image1CH& in, Image1CH& out)
  83. {
  84. double p = 0;
  85. double pp = 0;
  86. double min1 = 1;
  87. double min2 = 1;
  88. double max = 0;
  89.  
  90. for (int i = 0; i < in.width(); i++)
  91. {
  92. for (int j = 0; j < in.height(); j++)
  93. {
  94. if (in(i, j).Intensity() > max)
  95. {
  96. max = in(i, j).Intensity();
  97. }
  98. if (in(i, j).Intensity() < min1)
  99. {
  100. min1 = in(i, j).Intensity();
  101. }
  102. }
  103. }
  104.  
  105. p = (max - min1) / 2;
  106.  
  107. for (int i = 1; i < in.width()-1; i++)
  108. {
  109. for (int j = 1; j < in.height()-1; j++)
  110. {
  111. for (int k = -1; k < 2; k++)
  112. {
  113. for (int l = -1; l < 2; l++)
  114. {
  115. if (in(i + k, j + l).Intensity() < min2)
  116. {
  117. min2 = in(i + k, j + l).Intensity();
  118. }
  119. }
  120. }
  121. pp = p - min2/4;
  122. if (in(i, j).Intensity() > pp)
  123. {
  124. out(i, j).Intensity() = 1;
  125. }
  126. else
  127. {
  128. out(i, j).Intensity() = 0;
  129. }
  130. min2 = 1;
  131. }
  132. }
  133. for (int a = 0; a < in.width(); a++)
  134. {
  135. out(a, 0).Intensity() = 0;
  136. out(a, in.height() - 1).Intensity() = 0;
  137. }
  138. for (int b = 0; b < in.height(); b++)
  139. {
  140. out(0, b).Intensity() = 0;
  141. out(in.width() -1, b).Intensity() = 0;
  142. }
  143. }
  144.  
  145. void dylatacja(Image1CH& in, Image1CH& out)
  146. {
  147. bool zmiana = false;
  148. for (int i = 1; i < in.width()-1; i++)
  149. {
  150. for (int j = 1; j < in.height()-1; j++)
  151. {
  152. if (in(i, j).Intensity() == 1)
  153. {
  154. for (int p = -1; p < 2; p++)
  155. {
  156. for (int q = -1; q < 2; q++)
  157. {
  158. if (in(i + p, j + q).Intensity() == 0)
  159. {
  160. zmiana = true;
  161. }
  162. }
  163. }
  164. }
  165. if (zmiana == true)
  166. {
  167. out(i, j).Intensity() = 0;
  168. }
  169. else
  170. {
  171. out(i, j).Intensity() = in(i, j).Intensity();
  172. }
  173. zmiana = false;
  174. }
  175. }
  176. }
  177.  
  178. void erozja(Image1CH& in, Image1CH& out)
  179. {
  180. bool zmiana = false;
  181. for (int i = 1; i < in.width() - 1; i++)
  182. {
  183. for (int j = 1; j < in.height() - 1; j++)
  184. {
  185. if (in(i, j).Intensity() == 0)
  186. {
  187. for (int p = -1; p < 2; p++)
  188. {
  189. for (int q = -1; q < 2; q++)
  190. {
  191. if (in(i + p, j + q).Intensity() == 1)
  192. {
  193. zmiana = true;
  194. }
  195. }
  196. }
  197. }
  198. if (zmiana == true)
  199. {
  200. out(i, j).Intensity() = 1;
  201. }
  202. else
  203. {
  204. out(i, j).Intensity() = in(i, j).Intensity();
  205. }
  206. zmiana = false;
  207. }
  208. }
  209. }
  210.  
  211. void segmentacja(Image1CH& in, int** out, bool tryb)
  212. {
  213. int a = 1;
  214. double szukana = 0;
  215.  
  216. if (tryb == 1)
  217. {
  218. szukana = 1;
  219. }
  220. if (tryb == 0)
  221. {
  222. szukana = 0;
  223. }
  224. for (int i = 0; i < in.width(); i++)
  225. {
  226. for (int j = 0; j < in.height(); j++)
  227. {
  228. if (in(i, j).Intensity() == szukana)
  229. {
  230. out[i][j] = a;
  231. a++;
  232. }
  233. else
  234. {
  235. out[i][j] = 0;
  236. }
  237. }
  238. }
  239. /*
  240. for (int g = 0; g < in.width(); g++)
  241. {
  242. for (int h = 0; h < in.height(); h++)
  243. {
  244. cout << out[g][h] << '\t';
  245. }
  246. cout << endl;
  247. }
  248. system("pause");
  249. */
  250. int min = in.width()*in.height();
  251.  
  252. for (int i = 1; i < in.width()-1; i++)
  253. {
  254. for (int j = 1; j < in.height()-1; j++)
  255. {
  256. for (int p = -1; p < 2; p++)
  257. {
  258. for (int q = -1; q < 2; q++)
  259. {
  260. if (out[i + p][j + q] < min && out[i + p][j + q] != 0)
  261. {
  262. min = out[i + p][ j + q];
  263. }
  264. }
  265. }
  266. for (int k = -1; k < 2; k++)
  267. {
  268. for (int l = -1; l < 2; l++)
  269. {
  270. if (out[i + k][j + l] != 0)
  271. {
  272. out[i + k][j + l] = min;
  273. }
  274. }
  275. }
  276. min = in.width()*in.height();
  277. }
  278. }
  279.  
  280. min = in.width()*in.height();
  281.  
  282. for (int i = in.width() - 2; i > 0; i--)
  283. {
  284. for (int j = in.height() - 2; j > 0; j--)
  285. {
  286. for (int p = 1; p > -2; p--)
  287. {
  288. for (int q = 1; q > -2; q--)
  289. {
  290. if (out[i + p][j + q] < min && out[i + p][j + q] != 0)
  291. {
  292. min = out[i + p][j + q];
  293. }
  294. }
  295. }
  296.  
  297. for (int k = 1; k > -2; k--)
  298. {
  299. for (int l = 1; l > -2; l--)
  300. {
  301. if (out[i + k][j + l] != 0)
  302. {
  303. out[i + k][j + l] = min;
  304. }
  305. }
  306. }
  307.  
  308. min = in.width()*in.height();
  309.  
  310.  
  311. }
  312. }
  313.  
  314. min = in.width()*in.height();
  315.  
  316. for (int i = 1; i < in.width() - 1; i++)
  317. {
  318. for (int j = 1; j < in.height() - 1; j++)
  319. {
  320. for (int p = -1; p < 2; p++)
  321. {
  322. for (int q = -1; q < 2; q++)
  323. {
  324. if (out[i + p][j + q] < min && out[i + p][j + q] != 0)
  325. {
  326. min = out[i + p][j + q];
  327. }
  328. }
  329. }
  330. for (int k = -1; k < 2; k++)
  331. {
  332. for (int l = -1; l < 2; l++)
  333. {
  334. if (out[i + k][j + l] != 0)
  335. {
  336. out[i + k][j + l] = min;
  337. }
  338. }
  339. }
  340. min = in.width()*in.height();
  341. }
  342. }
  343.  
  344.  
  345. }
  346.  
  347. void segregacja(Image1CH& in, int** T, lista *g, lista *g1, bool kol)
  348. {
  349. lista *p = g;
  350. lista *p1 = g;
  351. lista *p2 = g;
  352. lista *p3 = g;
  353.  
  354. lista *pomo1 = g1;
  355. lista *pomo2 = g1;
  356. lista *pomo3 = g1;
  357.  
  358. bool x = 0;
  359. bool y = 0;
  360. int licznik = 0;
  361. int licznik1 = 0;
  362. int minimum =1310;
  363. int aku = 0;
  364.  
  365. for (int i = 0; i < in.width(); i++)
  366. {
  367. for (int j = 0; j < in.height(); j++)
  368. {
  369. do
  370. {
  371. if (T[i][j] == p->liczba)
  372. {
  373. x = 1;
  374. }
  375. p = p->next;
  376. } while (p != 0);
  377. p = g;
  378. if (x == 0)
  379. {
  380. p1->liczba = T[i][j];
  381. p1->next = new lista;
  382. p1 = p1->next;
  383. p1->liczba = 0;
  384. p1->next = 0;
  385. }
  386. x = 0;
  387. }
  388. }
  389. while (p2 != 0)
  390. {
  391. if (p2->liczba > 0 && p2->liczba != 0)
  392. {
  393. licznik++;
  394. cerr << p2->liczba << '\t';
  395. }
  396. p2 = p2->next;
  397. }
  398. if (kol == 1)
  399. {
  400. cerr << endl << "Wykryto " << licznik << " bialych elementow" << endl;
  401. }
  402. if (kol == 0)
  403. {
  404. cerr << endl << "Wykryto " << licznik << " czarnych elementow" << endl;
  405. }
  406.  
  407.  
  408. while (p3 != 0)
  409. {
  410. if (p3->liczba > 0)
  411. {
  412. for (int u = 0; u < in.width(); u++)
  413. {
  414. for (int v = 0; v < in.height(); v++)
  415. {
  416. if (T[u][v] == p3->liczba)
  417. {
  418. aku++;
  419. }
  420. }
  421. }
  422. if (aku < minimum)
  423. {
  424. for (int k = 0; k < in.width(); k++)
  425. {
  426. for (int l = 0; l < in.height(); l++)
  427. {
  428. if (T[k][l] == p3->liczba)
  429. {
  430. T[k][l] = 0;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. aku = 0;
  437. p3 = p3->next;
  438. }
  439.  
  440.  
  441. for (int n = 0; n < in.width(); n++)
  442. {
  443. for (int m = 0; m < in.height(); m++)
  444. {
  445. do
  446. {
  447. if (T[n][m] == pomo1->liczba)
  448. {
  449. y = 1;
  450. }
  451. pomo1 = pomo1->next;
  452. } while (pomo1 != 0);
  453.  
  454. pomo1= g1;
  455. if (y == 0)
  456. {
  457. pomo2->liczba = T[n][m];
  458. pomo2->next = new lista;
  459. pomo2 = pomo2->next;
  460. pomo2->liczba = 0;
  461. pomo2->next = 0;
  462. }
  463. y = 0;
  464. }
  465. }
  466. while (pomo3 != 0)
  467. {
  468. if (pomo3->liczba > 0 && pomo3->liczba != 0)
  469. {
  470. licznik1++;
  471. cerr << pomo3->liczba << '\t';
  472. }
  473. pomo3 = pomo3->next;
  474. }
  475. cerr << endl;
  476. if (kol == 1)
  477. {
  478. cerr << endl << "Wykryto " << licznik1 << " docelowych bialych elementow" << endl << endl;
  479. }
  480. if (kol == 0)
  481. {
  482. cerr << endl << "Wykryto " << licznik1 << " docelowych czarnych elementow" << endl << endl;
  483. }
  484.  
  485. }
  486. void przynaleznosc(int& f1, int& f2, int&f3, int&f4, lista *gc, lista *gb, int** Tc, int** Tb, Image1CH& in)
  487. {
  488. lista *pomc = gc;
  489. lista *pomb = gb;
  490. int fig = 0;
  491. bool x = 0;
  492.  
  493. while (pomc != 0)
  494. {
  495.  
  496. for (int i = 0; i < in.width(); i++)
  497. {
  498. for (int j = 0; j < in.height(); j++)
  499. {
  500. if (Tc[i][j] == pomc->liczba)
  501. {
  502. j = in.height();
  503. i = in.width();
  504.  
  505. if (Tc[i-1][j] == 0)
  506. {
  507. while (pomb != 0 && x == 0)
  508. {
  509. fig++;
  510. if (pomb->liczba == Tb[i - 1][j])
  511. {
  512. x = 1;
  513. }
  514. pomb = pomb->next;
  515. }
  516. }
  517.  
  518. if (Tc[i][j-1] == 0 && x ==0)
  519. {
  520. while (pomb != 0 && x == 0)
  521. {
  522. fig++;
  523. if (pomb->liczba == Tb[i][j-1])
  524. {
  525. x = 1;
  526. }
  527. pomb = pomb->next;
  528. }
  529. }
  530.  
  531. if (Tc[i + 1][j] == 0 && x == 0)
  532. {
  533. while (pomb != 0 && x == 0)
  534. {
  535. fig++;
  536. if (pomb->liczba == Tb[i + 1][j])
  537. {
  538. x = 1;
  539. }
  540. pomb = pomb->next;
  541. }
  542. }
  543.  
  544. if (Tc[i][j+1] == 0 && x == 0)
  545. {
  546. while (pomb != 0 && x == 0)
  547. {
  548. fig++;
  549. if (pomb->liczba == Tb[i][j+1])
  550. {
  551. x = 1;
  552. }
  553. pomb = pomb->next;
  554. }
  555. }
  556.  
  557. }
  558. }
  559. }
  560.  
  561. pomc = pomc->next;
  562. x = 0;
  563.  
  564. if (fig == 1)
  565. {
  566. f1++;
  567. }
  568. else if (fig == 2)
  569. {
  570. f2++;
  571. }
  572. else if (fig == 3)
  573. {
  574. f3++;
  575. }
  576. else if (fig == 4)
  577. {
  578. f4++;
  579. }
  580.  
  581. fig = 0;
  582. }
  583. }
  584.  
  585. void wypisz(int k1, int k2, int k3, int k4)
  586. {
  587. cerr << endl << "Pierwsza karta to :" << k1 << endl;
  588. cerr << endl << "Druga karta to :" << k2 << endl;
  589. cerr << endl << "Trzecia karta to :" << k3 << endl;
  590. cerr << endl << "Czwarta karta to :" << k4 << endl;
  591. }
  592.  
  593.  
  594. int main()
  595. {
  596. Image3CH Colour(1928, 1448);
  597.  
  598. Image1CH Grayscale(1928, 1448);
  599. Image1CH Median(1928, 1448);
  600. Image1CH Binary(1928, 1448);
  601. Image1CH Binaryd1(1928, 1448);
  602. Image1CH Binaryd1e1(1928, 1448);
  603. Image1CH Binaryd1e2(1928, 1448);
  604. Image1CH Binaryd2e2(1928, 1448);
  605. Image1CH Binaryd2e3(1928, 1448);
  606. Image1CH Binaryd3e3(1928, 1448);
  607. Image1CH Binaryd4e3(1928, 1448);
  608. Image1CH BinaryMorfo(1928, 1448);
  609.  
  610. Colour.LoadImage("img\\ideal.png", LPL_LOAD_FITTED);
  611.  
  612. Colour.ShowImage("Kolor");
  613.  
  614. rgbtogray(Colour, Grayscale);
  615.  
  616. Grayscale.ShowImage("Szary");
  617.  
  618. int rozmiar = 0;
  619. cerr << "Podaj rozmiar maski filtru medianowego :" << endl;
  620. cin >> rozmiar;
  621. while (rozmiar % 2 == 0)
  622. {
  623. cerr << "Rozmiar musi byc liczba nieparzysta! Podaj ponownie: " << endl;
  624. cin >> rozmiar;
  625. }
  626.  
  627. filtrmedianowy(Grayscale, Median, rozmiar);
  628.  
  629. Median.ShowImage("Po_Medianowym");
  630.  
  631. binaryzacja(Median, Binary);
  632.  
  633. Binary.ShowImage("Binarny");
  634.  
  635. dylatacja(Binary, Binaryd1);
  636. erozja(Binaryd1, Binaryd1e1);
  637. erozja(Binaryd1e1, Binaryd1e2);
  638. dylatacja(Binaryd1e2, Binaryd2e2);
  639.  
  640. erozja(Binaryd2e2, Binaryd2e3);
  641. dylatacja(Binaryd2e3, Binaryd3e3);
  642. dylatacja(Binaryd3e3, Binaryd4e3);
  643. erozja(Binaryd4e3, BinaryMorfo);
  644.  
  645. BinaryMorfo.ShowImage("Binarny_Po_Morfo");
  646.  
  647.  
  648. int **Tsc = new int *[BinaryMorfo.width()];
  649.  
  650. for (int i = 0; i<BinaryMorfo.width(); i++)
  651. {
  652. Tsc[i] = new int[BinaryMorfo.height()];
  653. }
  654.  
  655.  
  656.  
  657. int **Tsb = new int *[BinaryMorfo.width()];
  658.  
  659. for (int i = 0; i<BinaryMorfo.width(); i++)
  660. {
  661. Tsb[i] = new int[BinaryMorfo.height()];
  662. }
  663.  
  664.  
  665. segmentacja(BinaryMorfo, Tsc, 0);
  666. segmentacja(BinaryMorfo, Tsb, 1);
  667.  
  668.  
  669. lista *glowabialy = new lista;
  670. lista *pombialy = glowabialy;
  671. glowabialy->next = 0;
  672. glowabialy->liczba = -1;
  673.  
  674. lista *glowa1bialy = new lista;
  675. lista *pom1bialy = glowa1bialy;
  676. glowa1bialy->next = 0;
  677. glowa1bialy->liczba = -1;
  678.  
  679. bool kolor = 1;
  680.  
  681. segregacja(BinaryMorfo, Tsb, pombialy, pom1bialy, kolor);
  682.  
  683. lista *glowaczarny = new lista;
  684. lista *pomczarny = glowaczarny;
  685. glowaczarny->next = 0;
  686. glowaczarny->liczba = -1;
  687.  
  688. lista *glowa1czarny = new lista;
  689. lista *pom1czarny = glowa1czarny;
  690. glowa1czarny->next = 0;
  691. glowa1czarny->liczba = -1;
  692.  
  693. kolor = 0;
  694.  
  695. segregacja(BinaryMorfo, Tsc, pomczarny, pom1czarny, kolor);
  696.  
  697. int figura1 = 0;
  698. int figura2 = 0;
  699. int figura3 = 0;
  700. int figura4 = 0;
  701.  
  702. przynaleznosc(figura1, figura2, figura3, figura4, pom1czarny, pom1bialy, Tsc, Tsb, BinaryMorfo);
  703.  
  704. wypisz(figura1, figura2, figura3, figura4);
  705.  
  706.  
  707. system("pause");
  708.  
  709. return 0;
  710. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement