Advertisement
Guest User

Pavlidis algorithm

a guest
Nov 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1.     enum PoleWzorca {
  2.         P, _0, _1, X, Y
  3.     };
  4.  
  5.     struct Wzorzec {
  6.         PoleWzorca type[3][3];
  7.     };
  8.  
  9.     Wzorzec wzorce[6] = {
  10.         {
  11.             {
  12.                 {X, X, X},
  13.                 {_0, P, _0},
  14.                 {Y, Y, Y}
  15.             }
  16.         },
  17.         {
  18.             {
  19.                 {X, _0, Y},
  20.                 {X, P, Y},
  21.                 {X, _0, Y}
  22.             }
  23.         },
  24.         {
  25.             {
  26.                 {X, X, X},
  27.                 {_0, P, X},
  28.                 {_1, _0, X}
  29.             }
  30.         },
  31.         {
  32.             {
  33.                 {X, X, X},
  34.                 {X, P, _0},
  35.                 {X, _0, _1}
  36.             }
  37.         },
  38.         {
  39.             {
  40.                 {X, _0, _1},
  41.                 {X, P, _0},
  42.                 {X, X, X}
  43.             }
  44.         },
  45.         {
  46.             {
  47.                 {_1, _0, X},
  48.                 {_0, P, X},
  49.                 {X, X, X}
  50.             }
  51.         }
  52.     };
  53.  
  54.  
  55.  
  56.  
  57.  
  58. struct PointOffset {
  59.     int x, y;
  60. } ptsRound[3][3] =
  61. {
  62.     {{-1, -1}, {0, -1}, {1, -1}},
  63.     {{-1, 0}, {0, 0}, {1, 0}},
  64.     {{-1, 1}, {0, 1}, {1, 1}}
  65. };
  66.  
  67.  
  68. #define __p0 255
  69. #define __p1 0
  70. #define __p2 160
  71. #define __p3 64
  72.  
  73.  
  74.  
  75. bool BMPHandler::CheckWzorzec(int X, int Y, Wzorzec wz)
  76. {
  77.     bool
  78.         x_present = false,
  79.         y_present = false,
  80.         x_satisfied = false,
  81.         y_satisfied = false;
  82.  
  83.     for (int x = -1; x < 2; x++) {
  84.         for (int y = -1; y < 2; y++) {
  85.  
  86.             PoleWzorca currType = wz.type[y + 1][x + 1];
  87.  
  88.             switch (currType) {
  89.             case BMPHandler::PoleWzorca::X:
  90.                 x_present = true;
  91.                 if (GetPixel8(X + x, Y + y) != __p0) x_satisfied = true;
  92.                 break;
  93.             case BMPHandler::PoleWzorca::Y:
  94.                 y_present = true;
  95.                 if (GetPixel8(X + x, Y + y) != __p0) y_satisfied = true;
  96.                 break;
  97.             case BMPHandler::PoleWzorca::_0:
  98.                 if (GetPixel8(X + x, Y + y) != __p0) return false;
  99.                 break;
  100.             case BMPHandler::PoleWzorca::_1:
  101.                 if (GetPixel8(X + x, Y + y) == __p0) return false;
  102.                 break;
  103.             case BMPHandler::PoleWzorca::P:
  104.                 break;
  105.             }
  106.         }
  107.     }
  108.  
  109.     if (x_present && !x_satisfied) return false;
  110.     if (y_present && !y_satisfied) return false;
  111.     return true;
  112. }
  113.  
  114.  
  115.  
  116. void BMPHandler::Pavlids()
  117. {
  118.     Threshold(127);
  119.  
  120.     PointOffset pts[4] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
  121.     int imgWidth = ptrInfoHeader->biWidth;
  122.     int imgHeight = ptrInfoHeader->biHeight;
  123.  
  124.     bool remain = true;
  125.  
  126.     while (remain == true)
  127.     {
  128.         remain = false;
  129.  
  130.         // For j := 1, 3, 5, 7
  131.         for (int j = 0; j < 4; j++)
  132.         {
  133.             // For wszystkie piksele obrazu
  134.             for (int x = 0; x < imgWidth; x++)
  135.             {
  136.                 for (int y = 0; y < imgHeight; y++)
  137.                 {
  138.  
  139.                     // If p = 1 and j-ty sasiad = 0
  140.                     if (GetPixel8(x, y) == __p1 && GetPixel8(x + pts[j].x, y + pts[j].y) == __p0)
  141.                     {
  142.                         bool skel = false;
  143.  
  144.                         // For wszystkie wzorce W
  145.                         for (int wzNum = 0; wzNum < 6; wzNum++)
  146.                         {
  147.                             if (CheckWzorzec(x, y, wzorce[wzNum])) {
  148.                                 skel = true;
  149.                                 break;
  150.                             }
  151.                         }
  152.  
  153.  
  154.                         // If skel = true then p := 2
  155.                         if (skel) {
  156.                             SetPixel8(x, y, __p2);
  157.                         }
  158.                         else {
  159.                             SetPixel8(x, y, __p3);
  160.                             remain = true;
  161.                         }
  162.                     }
  163.                 }
  164.             }
  165.  
  166.             // For wszystkie piksele obrazu jeżeli p == 3 to usun p (p == 0);
  167.             for (int x = 0; x < imgWidth; x++)
  168.             {
  169.                 for (int y = 0; y < imgHeight; y++)
  170.                 {
  171.                     if (GetPixel8(x, y) == __p3) SetPixel8(x, y, __p0);
  172.                 }
  173.             }
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement