Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.16 KB | None | 0 0
  1. #include "lipa.h"
  2. #include <iostream>
  3.  
  4. //LipaLib - Learning Image Processing Autonomic Library
  5.  
  6. // =========== FUNCTION EXAMPLE =================//
  7. void rgbTogray(Image3CH& rgbImg, Image1CH& grayImg) // arguments with & because we want to save changes to images after performing funtion
  8. {
  9. //Check if image sizes are equal
  10. if (rgbImg.width() == grayImg.width() && rgbImg.height() == grayImg.height())
  11. {
  12. for (int i = 0; i < rgbImg.width(); i++) //iterate by rows
  13. for (int j = 0; j < rgbImg.height(); j++) //iterate by cols
  14. {
  15. 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)
  16. }
  17. }
  18. else
  19. {
  20. std::cerr << "Image sizes mismatch" << std::endl; //print error
  21. return;
  22. }
  23. }
  24. // ==============================================//
  25.  
  26. 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.
  27. {
  28.  
  29. if (in.width() == out.width() && in.height() == out.height())
  30. {
  31.  
  32. for (int i = 0; i < in.width(); i++) //iterate by rows
  33. for (int j = 0; j < in.height(); j++) //iterate by cols
  34. {
  35. if (in(i, j).I() > treshold) // Gdy wartość intensywności piksela jest powyżej progu, przypisuje wartość 1 - odpowiada koloru białemu.
  36. {
  37. out(i, j).I() = 1;
  38. }
  39. else
  40. out(i, j).I() = 0; // Gdy wartość intensywności piksela jest poniżej progu, przypisuje wartość 0 - odpowiada koloru czarnemu.
  41. }
  42. }
  43. else
  44. {
  45. std::cerr << "Image sizes mismatch" << std::endl; //print error
  46. return;
  47. }
  48. }
  49.  
  50. 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).
  51. {
  52. //Tablica dynamiczna przechowująca etykiety.
  53. double nr_etykiety = 1;
  54. int **Etykiety = new int*[in.width()];
  55.  
  56. for (int i = 0; i < in.width(); i++)
  57. {
  58. Etykiety[i] = new int[in.height()];
  59. }
  60.  
  61. //Przypisanie każdemu z pikseli obrazu etykiety oraz wpisanie jej do tablicy dynamicznej.
  62. for (int i = 0; i < in.width(); i++)
  63. for (int j = 0; j < in.height(); j++)
  64. {
  65. if (in(i, j).I() == parametr)
  66. {
  67. Etykiety[i][j] = nr_etykiety;
  68. nr_etykiety++;
  69. }
  70. else
  71. {
  72. Etykiety[i][j] = 0;
  73. }
  74. }
  75.  
  76. bool first1 = true;
  77. bool first2 = true;
  78. int z = 0;
  79.  
  80.  
  81.  
  82. for(int i=0; i<in.width();i++)
  83. for (int j = 0; j < in.height(); j++)
  84. {
  85. if (i == 0 || j == 0 || i == in.width() - 1 || j == in.height() - 1)
  86. {
  87. Etykiety[i][j] = 1;
  88. }
  89. }
  90.  
  91. while(first1 == true || first2 == true )
  92. {
  93.  
  94. //Segmentacja Top-down. Ustalenie wartości etykiet każdego z pikseli na podstawie sąsiedztwa.
  95. first1 = false;
  96. for (int i = 1; i < (in.width() - 1); i++)
  97. {
  98. for (int j = 1; j < (in.height() - 1); j++)
  99. {
  100. int min = in.width()*in.height();
  101.  
  102.  
  103. if (Etykiety[i][j] > 0) //Sprawdzenie etykiet, gdyż elementy z etykietą 0 nie są poddane segmentacji .
  104. {
  105.  
  106. for (int k = 0; k < 3; k++) // 3 odpowiada rozmiarowi otoczenia jakie jest rozpatrywane.
  107. {
  108. for (int l = 0; l < 3; l++)
  109. {
  110. if (Etykiety[i + k - 1][j + l - 1] > 0)//Sprawdzenie czy sąsiad nie jest 0.
  111. {
  112.  
  113. if (Etykiety[i + k - 1][j + l - 1] < min) //Sprawdzenie czy kolejne etykiety sąsiadów są mniejsze od minimalnego.
  114. {
  115. min = Etykiety[i + k - 1][j + l - 1]; //Uaktulnienie najmniejszej wartości etykiety sąsiadów.
  116. }
  117.  
  118.  
  119. }
  120. }
  121. }
  122. if (Etykiety[i][j] != min)
  123. {
  124. Etykiety[i][j] = min;
  125. first1 = true;
  126. }
  127. }
  128.  
  129. }
  130. }
  131.  
  132.  
  133. //Segmentacja Bottom-up. Ustalenie wartości etykiet każdego z pikseli na podstawie sąsiedztwa.
  134. first2 = false;
  135. for (int i = (in.width() - 2); i > 0; i--)
  136. {
  137. for (int j = (in.height() - 2); j > 0; j--)
  138. {
  139.  
  140. int min = in.width()*in.height();
  141. if (Etykiety[i][j] > 0)
  142. {
  143.  
  144. for (int k = 2; k > -1; k--)
  145. {
  146. for (int l = 2; l > -1; l--)
  147. {
  148. if (Etykiety[i + k - 1][j + l - 1] > 0)
  149. {
  150.  
  151. if (Etykiety[i + k - 1][j + l - 1] < min) //Sprawdzenie czy kolejne etykiety sąsiadów są mniejsze od minimalnego.
  152. {
  153. min = Etykiety[i + k - 1][j + l - 1];
  154. }
  155.  
  156. }
  157. }
  158. }
  159. if (Etykiety[i][j] != min)
  160. {
  161. Etykiety[i][j] = min;
  162. z++;
  163. first2 = true;
  164. }
  165.  
  166. }
  167. }
  168.  
  169.  
  170. }
  171.  
  172. }
  173.  
  174. //Wpisanie etykiet do obrazu wyjściowego.
  175. for (int i = 0; i < in.width(); i++)
  176. for (int j = 0; j < in.height(); j++)
  177. {
  178. out(i, j).I() = Etykiety[i][j];
  179. }
  180.  
  181. //Usuwanie tablicy
  182. for (int i = 0; i < in.width(); i++)
  183. {
  184. delete[] Etykiety[i];
  185. }
  186. delete[] Etykiety;
  187. }
  188.  
  189.  
  190. void dylatacja(Image1CH& in, Image1CH& out)
  191. {
  192.  
  193. //dylatacja
  194. for (int i = 0; i < (in.width() - 1); i++)
  195. {
  196. for (int j = 0; j < (in.height() - 1); j++)
  197. {
  198. if (i < 1 || i>in.width() - 1 - 1 || j<1 || j>in.height() - 1 - 1)
  199. {
  200. out(i, j).I() = in(i, j).I();
  201. }
  202. else
  203. {
  204. if (in(i, j).I() == 1)
  205. {
  206. for (int k = 0; k < 3; k++)
  207. {
  208. for (int l = 0; l < 3; l++)
  209. {
  210. out((i + k - 1), (j + l - 1)).I() = 1;
  211.  
  212. }
  213. }
  214. }
  215.  
  216. }
  217. }
  218. }
  219. }
  220. void erozja(Image1CH& in, Image1CH& out)
  221. {
  222. //erozja
  223. for (int i = 0; i < (in.width() - 1); i++)
  224. {
  225. for (int j = 0; j < (in.height() - 1); j++)
  226. {
  227. if (i < 1 || i>in.width() - 1 - 1 || j<1 || j>in.height() - 1 - 1)
  228. {
  229. out(i, j).I() = in(i, j).I();
  230. }
  231. else
  232. {
  233. double min = 1;
  234. for (int k = 0; k < 3; k++)
  235. {
  236. for (int l = 0; l < 3; l++)
  237. {
  238. if (in((i + k - 1), (j + l - 1)).I() < min)
  239. {
  240. min = in((i + k - 1), (j + l - 1)).I();
  241. }
  242. }
  243. }
  244. out(i, j).I() = min;
  245. }
  246. }
  247. }
  248. }
  249.  
  250. int liczba(Image1CH& in) // Funkcja licząca ilość segmentów w obrazie.
  251. {
  252. //Stworzenie nowej tablicy dynamicznej przechowującej nowe, posegregowane etykiety.
  253. int size = 5000;
  254. int *Segment = new int[size];
  255. Segment[0] = 0;
  256.  
  257. int licz = 0;
  258. int powtorzenie;
  259.  
  260. for (int i = 0; i < in.width(); i++)
  261. for (int j = 0; j < in.height(); j++)
  262. {
  263. powtorzenie = 0;
  264.  
  265. if (in(i, j).I() > 0)
  266. {
  267. if (licz == 0)
  268. {
  269. Segment[licz] = in(i, j).I();
  270. licz++;
  271. }
  272. for (int m = 0; m < licz; m++)
  273. {
  274. if (Segment[m] == in(i, j).I())
  275. {
  276. powtorzenie++;
  277. }
  278. }
  279. if (powtorzenie == 0)
  280. {
  281. Segment[licz] = in(i, j).I();
  282. licz++;
  283. }
  284. }
  285.  
  286. }
  287. delete[] Segment;
  288.  
  289. return licz-2;
  290. }
  291.  
  292. void wydzielenie(Image1CH& in1, Image1CH& in2, Image3CH& out)
  293. {
  294.  
  295.  
  296. for (int i = 0; i < in2.width(); i++)
  297. for (int j = 0; j < in2.height(); j++)
  298. {
  299.  
  300. if (in2(i, j).I() == 1)
  301. {
  302. out(i, j).R() = 0;
  303. out(i, j).G() = 0;
  304. out(i, j).B() = 0;
  305. }
  306. else if (in2(i, j).I() > 1)
  307. {
  308.  
  309. out(i, j).R() = in2(i, j).I();
  310.  
  311. }
  312.  
  313. else if (in1(i, j).I() > 0)
  314. {
  315. for (int k = 0; k < 3; k++)
  316. for (int l = 0; l < 3; l++)
  317. {
  318. if (in2(k, l).I() > 0)
  319. {
  320.  
  321. out(i, j).B() = in1(k, l).I();
  322. out(i, j).G() = in2(k, l).I();
  323.  
  324.  
  325. }
  326. }
  327. }
  328.  
  329. }
  330.  
  331.  
  332.  
  333.  
  334. }
  335.  
  336. void pola(Image3CH& in, int*& pole)
  337. {
  338. int z = 0;
  339. int powtorzenie = 0;
  340. int size = 50;
  341. int *Tab = new int[size];
  342. Tab[0] = 0;
  343.  
  344.  
  345. for (int i = 0; i < in.width(); i++)
  346. for (int j = 0; j < in.height(); j++)
  347. {
  348. powtorzenie = 0;
  349.  
  350. if (in(i, j).B() > 0 && in(i, j).G() > 0)
  351. {
  352. if (z == 0)
  353. {
  354. pole[z] = 1;
  355. Tab[z] = in(i, j).B();
  356. z++;
  357.  
  358. }
  359. for (int k = 0; k < z; k++)
  360. {
  361. if (Tab[k] == in(i, j).B())
  362. {
  363. pole[k]++;
  364. powtorzenie++;
  365. }
  366. }
  367. if (powtorzenie == 0)
  368. {
  369. pole[z] = 1;
  370. Tab[z] = in(i, j).B();
  371. z++;
  372. }
  373. }
  374. }
  375.  
  376.  
  377.  
  378. delete[] Tab;
  379. }
  380.  
  381. //void unsharpMask(Image3CH &in, Image3CH &out)
  382. //{
  383. // double kernel[5][5] = { 1, 1, 1, 1, 1,
  384. // 1, 2, 4, 2, 1,
  385. // 2, 4, 8, 4, 2,
  386. // 1, 2, 4, 2, 1,
  387. // 1, 1, 1, 1, 1 };
  388. // double accumulatorR = 0;
  389. // double accumulatorG = 0;
  390. // double accumulatorB = 0;
  391. //
  392. // if (in.width() == out.width() && in.height() == out.height())
  393. // {
  394. // for (int i = 2; i < in.width() - 2; i++) //iterate by rows
  395. // for (int j = 2; j < in.height() - 2; j++) //iterate by cols
  396. // {
  397. // accumulatorR = 0;
  398. // accumulatorG = 0;
  399. // accumulatorB = 0;
  400. // for (int k = 0; k < 5; k++) //iterate by rows
  401. // for (int l = 0; l < 5; l++) //iterate by cols
  402. // {
  403. // accumulatorR += in(i + k - 2, j + l - 2).R() * kernel[k][l];
  404. // accumulatorG += in(i + k - 2, j + l - 2).G() * kernel[k][l];
  405. // accumulatorB += in(i + k - 2, j + l - 2).B() * kernel[k][l];
  406. // }
  407. // out(i, j).R() = 0.5*(in(i,j).R() - (accumulatorR / 52))+in(i,j).R();
  408. // out(i, j).G() = 0.5*(in(i, j).G() - (accumulatorG / 52)) + in(i, j).G();
  409. // out(i, j).B() = 0.5*(in(i, j).B() - (accumulatorB / 52)) + in(i, j).B();
  410. //
  411. // }
  412. // }
  413. // else
  414. // {
  415. // std::cerr << "Image sizes mismatch" << std::endl; //print error
  416. // return;
  417. // }
  418. //
  419. //}
  420.  
  421. void medianMask(Image1CH &in, Image1CH &out)
  422. {
  423. double kernel[3][3] = { 1, 1, 1,
  424. 1, 1, 1,
  425. 1, 1, 1 };
  426.  
  427. int min = 1;
  428. int temp;
  429. int *Tab = new int[9];
  430. if (in.width() == out.width() && in.height() == out.height())
  431. {
  432. for (int i = 1; i < in.width() - 1; i++) //iterate by rows
  433. for (int j = 1; j < in.height() - 1; j++) //iterate by cols
  434. {
  435. for (int k = 0; k < 3; k++) //iterate by rows
  436. for (int l = 0; l < 3; l++) //iterate by cols
  437. {
  438. if (in(i + k - 1, j + l - 1).I() < min)
  439. {
  440. min = in(i + k - 1, j + l - 1).I();
  441. Tab[k*l] = min;
  442.  
  443. }
  444.  
  445. // Bubble Sorting
  446.  
  447. if (Tab[l] > Tab[l +1])
  448. {
  449. temp = Tab[l];
  450. Tab[l] = Tab[l + 1];
  451. Tab[l + 1] = temp;
  452. }
  453. }
  454. out(i, j).I() = Tab[4];
  455.  
  456. }
  457. }
  458. else
  459. {
  460. std::cerr << "Image sizes mismatch" << std::endl; //print error
  461. return;
  462. }
  463.  
  464. }
  465.  
  466.  
  467.  
  468.  
  469. int main()
  470. {
  471. int liczba_segmentow1 = 0;
  472. int liczba_segmentow2 = 0;
  473.  
  474. Image3CH ColourImage(1928, 1448); // Create new image Image3CH - three channels image (width,height)
  475. Image1CH GrayscaleImage(ColourImage.width(), ColourImage.height());
  476. Image1CH Binarny(ColourImage.width(), ColourImage.height());
  477.  
  478. Image1CH Erozja(ColourImage.width(), ColourImage.height());
  479. Image1CH Dylatacja(ColourImage.width(), ColourImage.height());
  480. Image1CH Zamkniety(ColourImage.width(), ColourImage.height());
  481.  
  482. Image1CH Segmenty1(ColourImage.width(), ColourImage.height());
  483. Image1CH Segmenty2(ColourImage.width(), ColourImage.height());
  484. Image3CH Segmenty3(ColourImage.width(), ColourImage.height());
  485.  
  486. Image1CH Median(ColourImage.width(), ColourImage.height());
  487.  
  488.  
  489.  
  490. //ColourImage.LoadImage("img\\ideal.png", LPL_LOAD_FITTED);
  491. //ColourImage.LoadImage("img\\blurred.png", LPL_LOAD_FITTED);
  492. //ColourImage.LoadImage("img\\gradient2.png", LPL_LOAD_FITTED);
  493. ColourImage.LoadImage("img\\noised.png", LPL_LOAD_FITTED);
  494.  
  495.  
  496.  
  497. int *Pole = new int[50];
  498. Pole[0] = 0;
  499.  
  500.  
  501.  
  502.  
  503. rgbTogray(ColourImage, GrayscaleImage);
  504. medianMask(GrayscaleImage, Median);
  505. binaryzacja(GrayscaleImage, Binarny, 0.4);
  506.  
  507. erozja(Binarny, Dylatacja);
  508. dylatacja(Dylatacja, Zamkniety);
  509.  
  510. segmentacja(Zamkniety, Segmenty2, 0);
  511. segmentacja(Zamkniety, Segmenty1, 1);
  512.  
  513. liczba_segmentow1 = liczba(Segmenty1);
  514. liczba_segmentow2 = liczba(Segmenty2);
  515.  
  516. std::cout << " Liczba kart: " << liczba_segmentow1 << " ";
  517. std::cout <<" Liczba wszystkich znakow: "<< liczba_segmentow2 << " ";
  518.  
  519. wydzielenie(Segmenty1, Segmenty2, Segmenty3);
  520. pola(Segmenty3, Pole);
  521.  
  522. for (int i = 0; i < 50; i++)
  523. {
  524. std::cout << Pole[i] << " ";
  525. }
  526.  
  527.  
  528.  
  529. //Unsharp.ShowImage("Obraz po filtracji");
  530. ColourImage.ShowImage("Obraz wejsciowy"); //Show image
  531. Binarny.ShowImage("Obraz binarny");
  532.  
  533.  
  534. Segmenty1.ShowImage("Obraz posegmentowany");
  535. Segmenty2.ShowImage("Obraz posegmentowany");
  536. Segmenty3.ShowImage("Obraz posegmentowany");
  537.  
  538.  
  539.  
  540. delete[] Pole;
  541.  
  542. return 0;
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement