Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.68 KB | None | 0 0
  1. public static void Roberts(Image<Bgr, byte> img, Image<Bgr, byte> imgCopy)
  2.         {
  3.             unsafe
  4.             {
  5.                 // direct access to the image memory(sequencial)
  6.                 // direcion top left -> bottom right
  7.                 MIplImage m = img.MIplImage;
  8.                 byte* dataPtr = (byte*)m.imageData.ToPointer(); // Pointer to the image
  9.                 MIplImage m1 = imgCopy.MIplImage;
  10.                 byte* dataPtr1 = (byte*)m1.imageData.ToPointer(); // Pointer to the image
  11.                 double blueD = 0, greenD = 0, redD = 0;
  12.  
  13.                 int width = img.Width;
  14.                 int height = img.Height;
  15.                 int nChan = m.nChannels; // number of channels - 3
  16.                 int widthS = m.widthStep;
  17.                 int padding = m.widthStep - m.nChannels * m.width; // alinhament bytes (padding)
  18.                 int x, y, e, f, h,i;
  19.  
  20.  
  21.  
  22.                 if (nChan == 3) // image in RGB
  23.                 {
  24.                     for (y = 0; y < height; y++) //percorrer a imagem pixel a pixel
  25.                     {
  26.                         for (x = 0; x < width; x++)
  27.                         {
  28.                             //Core
  29.                             if (x != 0 && x != (width - 1) && y != 0 && y != (height - 1))
  30.                             {
  31.                                 e = dataPtr1[0];
  32.                                 f = (dataPtr1 + nChan)[0];
  33.                                 h = (dataPtr1 + widthS)[0];
  34.                                 i = (dataPtr1 + nChan + widthS)[0];
  35.  
  36.                                 blueD = Math.Abs(e - i) + Math.Abs(f - h);
  37.  
  38.                                 if (blueD > 255)
  39.                                     blueD = 255;
  40.  
  41.                                 e = dataPtr1[1];
  42.                                 f = (dataPtr1 + nChan)[1];
  43.                                 h = (dataPtr1 + widthS)[1];
  44.                                 i = (dataPtr1 + nChan + widthS)[1];
  45.                                 greenD = Math.Abs(e - i) + Math.Abs(f - h);
  46.  
  47.                                 if (greenD > 255)
  48.                                     greenD = 255;
  49.  
  50.                                 e = dataPtr1[2];
  51.                                 f = (dataPtr1 + nChan)[2];
  52.                                 h = (dataPtr1 + widthS)[2];
  53.                                 i = (dataPtr1 + nChan + widthS)[2];
  54.  
  55.                                 redD = Math.Abs(e - i) + Math.Abs(f - h);
  56.  
  57.                                 if (redD > 255)
  58.                                     redD = 255;
  59.                             }
  60.                             else
  61.                             {
  62.                                 //Top Bar
  63.                                 if (x != 0 && x != (width - 1) && y == 0)
  64.                                 {
  65.                                     e = dataPtr1[0];
  66.                                     f = (dataPtr1 + nChan)[0];
  67.                                     h = (dataPtr1 + widthS)[0];
  68.                                     i = (dataPtr1 + nChan + widthS)[0];
  69.  
  70.                                     blueD = Math.Abs(e - i) + Math.Abs(f - h);
  71.  
  72.                                     if (blueD > 255)
  73.                                         blueD = 255;
  74.  
  75.                                     e = dataPtr1[1];
  76.                                     f = (dataPtr1 + nChan)[1];
  77.                                     h = (dataPtr1 + widthS)[1];
  78.                                     i = (dataPtr1 + nChan + widthS)[1];
  79.                                     greenD = Math.Abs(e - i) + Math.Abs(f - h);
  80.  
  81.                                     if (greenD > 255)
  82.                                         greenD = 255;
  83.  
  84.                                     e = dataPtr1[2];
  85.                                     f = (dataPtr1 + nChan)[2];
  86.                                     h = (dataPtr1 + widthS)[2];
  87.                                     i = (dataPtr1 + nChan + widthS)[2];
  88.  
  89.                                     redD = Math.Abs(e - i) + Math.Abs(f - h);
  90.  
  91.                                     if (redD > 255)
  92.                                         redD = 255;
  93.                                 }
  94.                                 else
  95.                                 {
  96.                                     //Bottam Bar
  97.                                     if (x != 0 && x != (width - 1) && y == (height - 1))
  98.                                     {
  99.                                         e = dataPtr1[0];
  100.                                         f = (dataPtr1 + nChan)[0];
  101.                                         h = dataPtr1[0];
  102.                                         i = (dataPtr1 + nChan)[0];
  103.  
  104.                                         blueD = Math.Abs(e - i) + Math.Abs(f - h);
  105.  
  106.                                         if (blueD > 255)
  107.                                             blueD = 255;
  108.  
  109.                                         e = dataPtr1[1];
  110.                                         f = (dataPtr1 + nChan)[1];
  111.                                         h = dataPtr1[1];
  112.                                         i = (dataPtr1 + nChan)[1];
  113.  
  114.                                         greenD = Math.Abs(e - i) + Math.Abs(f - h);
  115.  
  116.                                         if (greenD > 255)
  117.                                             greenD = 255;
  118.  
  119.                                         e = dataPtr1[2];
  120.                                         f = (dataPtr1 + nChan)[2];
  121.                                         h = dataPtr1[2];
  122.                                         i = (dataPtr1 + nChan)[2];
  123.  
  124.                                         redD = Math.Abs(e - i) + Math.Abs(f - h);
  125.  
  126.                                         if (redD > 255)
  127.                                             redD = 255;
  128.                                     }
  129.                                     else
  130.                                     {
  131.                                         //Left Colum
  132.                                         if (x == 0 && y != 0 && y != (height - 1))
  133.                                         {
  134.                                             e = dataPtr1[0];
  135.                                             f = (dataPtr1 + nChan)[0];
  136.                                             h = (dataPtr1 + widthS)[0];
  137.                                             i = (dataPtr1 + nChan + widthS)[0];
  138.  
  139.                                             blueD = Math.Abs(e - i) + Math.Abs(f - h);
  140.  
  141.                                             if (blueD > 255)
  142.                                                 blueD = 255;
  143.  
  144.                                             e = dataPtr1[1];
  145.                                             f = (dataPtr1 + nChan)[1];
  146.                                             h = (dataPtr1 + widthS)[1];
  147.                                             i = (dataPtr1 + nChan + widthS)[1];
  148.                                             greenD = Math.Abs(e - i) + Math.Abs(f - h);
  149.  
  150.                                             if (greenD > 255)
  151.                                                 greenD = 255;
  152.  
  153.                                             e = dataPtr1[2];
  154.                                             f = (dataPtr1 + nChan)[2];
  155.                                             h = (dataPtr1 + widthS)[2];
  156.                                             i = (dataPtr1 + nChan + widthS)[2];
  157.  
  158.                                             redD = Math.Abs(e - i) + Math.Abs(f - h);
  159.  
  160.                                             if (redD > 255)
  161.                                                 redD = 255;
  162.                                         }
  163.                                         else
  164.                                         {
  165.                                             //Right Colum
  166.                                             if (x == (width - 1) && y != 0 && y != (height - 1))
  167.                                             {
  168.                                                 e = dataPtr1[0];
  169.                                                 f = dataPtr1[0];
  170.                                                 h = (dataPtr1 + widthS)[0];
  171.                                                 i = (dataPtr1 + widthS)[0];
  172.  
  173.                                                 blueD = Math.Abs(e - i) + Math.Abs(f - h);
  174.  
  175.                                                 if (blueD > 255)
  176.                                                     blueD = 255;
  177.  
  178.                                                 e = dataPtr1[1];
  179.                                                 f = dataPtr1[1];
  180.                                                 h = (dataPtr1 + widthS)[1];
  181.                                                 i = (dataPtr1 + widthS)[1];
  182.                                                 greenD = Math.Abs(e - i) + Math.Abs(f - h);
  183.  
  184.                                                 if (greenD > 255)
  185.                                                     greenD = 255;
  186.  
  187.                                                 e = dataPtr1[2];
  188.                                                 f = dataPtr1[2];
  189.                                                 h = (dataPtr1 + widthS)[2];
  190.                                                 i = (dataPtr1 + widthS)[2];
  191.  
  192.                                                 redD = Math.Abs(e - i) + Math.Abs(f - h);
  193.  
  194.                                                 if (redD > 255)
  195.                                                     redD = 255;
  196.                                             }
  197.                                             else
  198.                                             {
  199.                                                 //Top Left
  200.                                                 if (x == 0 && y == 0)
  201.                                                 {
  202.                                                     e = dataPtr1[0];
  203.                                                     f = (dataPtr1 + nChan)[0];
  204.                                                     h = (dataPtr1 + widthS)[0];
  205.                                                     i = (dataPtr1 + nChan + widthS)[0];
  206.  
  207.                                                     blueD = Math.Abs(e - i) + Math.Abs(f - h);
  208.  
  209.                                                     if (blueD > 255)
  210.                                                         blueD = 255;
  211.  
  212.                                                     e = dataPtr1[1];
  213.                                                     f = (dataPtr1 + nChan)[1];
  214.                                                     h = (dataPtr1 + widthS)[1];
  215.                                                     i = (dataPtr1 + nChan + widthS)[1];
  216.                                                     greenD = Math.Abs(e - i) + Math.Abs(f - h);
  217.  
  218.                                                     if (greenD > 255)
  219.                                                         greenD = 255;
  220.  
  221.                                                     e = dataPtr1[2];
  222.                                                     f = (dataPtr1 + nChan)[2];
  223.                                                     h = (dataPtr1 + widthS)[2];
  224.                                                     i = (dataPtr1 + nChan + widthS)[2];
  225.  
  226.                                                     redD = Math.Abs(e - i) + Math.Abs(f - h);
  227.  
  228.                                                     if (redD > 255)
  229.                                                         redD = 255;
  230.                                                 }
  231.                                                 else
  232.                                                 {
  233.                                                     //Top Right
  234.                                                     if (x == (width - 1) && y == 0)
  235.                                                     {
  236.                                                         e = dataPtr1[0];
  237.                                                         f = dataPtr1[0];
  238.                                                         h = (dataPtr1 + widthS)[0];
  239.                                                         i = (dataPtr1 + widthS)[0];
  240.  
  241.                                                         blueD = Math.Abs(e - i) + Math.Abs(f - h);
  242.  
  243.                                                         if (blueD > 255)
  244.                                                             blueD = 255;
  245.  
  246.                                                         e = dataPtr1[1];
  247.                                                         f = dataPtr1[1];
  248.                                                         h = (dataPtr1 + widthS)[1];
  249.                                                         i = (dataPtr1 + widthS)[1];
  250.  
  251.                                                         greenD = Math.Abs(e - i) + Math.Abs(f - h);
  252.  
  253.                                                         if (greenD > 255)
  254.                                                             greenD = 255;
  255.  
  256.                                                         e = dataPtr1[2];
  257.                                                         f = dataPtr1[2];
  258.                                                         h = (dataPtr1 + widthS)[2];
  259.                                                         i = (dataPtr1 + widthS)[2];
  260.  
  261.                                                         redD = Math.Abs(e - i) + Math.Abs(f - h);
  262.  
  263.                                                         if (redD > 255)
  264.                                                             redD = 255;
  265.                                                     }
  266.                                                     else
  267.                                                     {
  268.                                                         //Bottom Left
  269.                                                         if (x == 0 && y == (height - 1))
  270.                                                         {
  271.                                                             e = dataPtr1[0];
  272.                                                             f = (dataPtr1 + nChan)[0];
  273.                                                             h = dataPtr1[0];
  274.                                                             i = (dataPtr1 + nChan)[0];
  275.  
  276.                                                             blueD = Math.Abs(e - i) + Math.Abs(f - h);
  277.  
  278.                                                             if (blueD > 255)
  279.                                                                 blueD = 255;
  280.  
  281.                                                             e = dataPtr1[1];
  282.                                                             f = (dataPtr1 + nChan)[1];
  283.                                                             h = dataPtr1[1];
  284.                                                             i = (dataPtr1 + nChan)[1];
  285.  
  286.                                                             greenD = Math.Abs(e - i) + Math.Abs(f - h);
  287.  
  288.                                                             if (greenD > 255)
  289.                                                                 greenD = 255;
  290.  
  291.                                                             e = dataPtr1[2];
  292.                                                             f = (dataPtr1 + nChan)[2];
  293.                                                             h = dataPtr1[2];
  294.                                                             i = (dataPtr1 + nChan)[2];
  295.  
  296.                                                             redD = Math.Abs(e - i) + Math.Abs(f - h);
  297.  
  298.                                                             if (redD > 255)
  299.                                                                 redD = 255;
  300.                                                         }
  301.                                                         else
  302.                                                         {
  303.                                                             //Bottam Right
  304.                                                             if (x == (width - 1) && y == (height - 1))
  305.                                                             {
  306.                                                                 e = dataPtr1[0];
  307.                                                                 f = dataPtr1[0];
  308.                                                                 h = dataPtr1[0];
  309.                                                                 i = dataPtr1[0];
  310.  
  311.                                                                 blueD = Math.Abs(e - i) + Math.Abs(f - h);
  312.  
  313.                                                                 if (blueD > 255)
  314.                                                                     blueD = 255;
  315.  
  316.                                                                 e = dataPtr1[1];
  317.                                                                 f = dataPtr1[1];
  318.                                                                 h = dataPtr1[1];
  319.                                                                 i = dataPtr1[1];
  320.  
  321.                                                                 greenD = Math.Abs(e - i) + Math.Abs(f - h);
  322.  
  323.                                                                 if (greenD > 255)
  324.                                                                     greenD = 255;
  325.  
  326.                                                                 e = dataPtr1[2];
  327.                                                                 f = dataPtr1[2];
  328.                                                                 h = dataPtr1[2];
  329.                                                                 i = dataPtr1[2];
  330.  
  331.                                                                 redD = Math.Abs(e - i) + Math.Abs(f - h);
  332.  
  333.                                                                 if (redD > 255)
  334.                                                                     redD = 255;
  335.                                                             }
  336.                                                         }
  337.                                                     }
  338.                                                 }
  339.                                             }
  340.                                         }
  341.                                     }
  342.                                 }
  343.                             }
  344.  
  345.                             dataPtr[0] = (byte)Math.Round(blueD);
  346.                             dataPtr[1] = (byte)Math.Round(greenD);
  347.                             dataPtr[2] = (byte)Math.Round(redD);
  348.  
  349.                             // advance the pointer to the next pixel
  350.                             dataPtr += nChan;
  351.                             dataPtr1 += nChan;
  352.                         }
  353.  
  354.                         //at the end of the line advance the pointer by the aligment bytes (padding)
  355.                         dataPtr += padding;
  356.                         dataPtr1 += padding;
  357.                     }
  358.                 }
  359.             }
  360.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement