title: opencv - shrink objects to pixels
body: <p>I am processing such an image as shown in Fig.1, which is composed of an array of points and required to convert to Fig. 2.  </p>
  
  <p><em>Fig.1 original image</em></p>
  
  <p><img src="https://i.stack.imgur.com/5vlau.png" alt="Fig.1 original image"><br>
  <em>Fig.2 wanted image</em></p>
  
  <p><img src="https://i.stack.imgur.com/lv6rc.png" alt="Fig.2 wanted image"></p>
  
  <p>In order to finish the conversion, firstly I detect the <code>edge</code> of every point and then operate <code>dilation</code>. The result is satisfactory after choosing the proper parameters, seen in Fig. 3.</p>
  
  <p><em>Fig.3 image after dilation</em></p>
  
  <p><img src="https://i.stack.imgur.com/5QfN3.png" alt="Fig.3 image after dilation"></p>
  
  <p>I processed the same image before in MATLAB. When it comes to shrink objects (in Fig.3) to pixels, function <code>bwmorph(Img,'shrink',Inf)</code> works and the result is exactly where Fig. 2 comes from. So how to get the same wanted image in opencv? It seems that there is no similar <code>shrink</code> function.</p>
  
  <p>Here is my code of finding edge and dilation operation:</p>
  
  <pre><code>#include "opencv2/imgproc/imgproc.hpp"
  #include "opencv2/highgui/highgui.hpp"
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  using namespace cv;
  
  // Global variables
  Mat src, dilation_dst;
  int dilation_size = 2;
  
  int main(int argc, char *argv[])
  {
      IplImage* img = cvLoadImage("c:\\001a.bmp", 0);                 // 001a.bmp is Fig.1                              
  
      // Perform canny edge detection
      cvCanny(img, img, 33, 100, 3);
  
      // IplImage to Mat
      Mat imgMat(img);
      src = img;
  
      // Create windows
      namedWindow("Dilation Demo", CV_WINDOW_AUTOSIZE);
  
      Mat element = getStructuringElement(2,                          // dilation_type = MORPH_ELLIPSE
                    Size(2*dilation_size + 1, 2*dilation_size + 1),
                    Point(dilation_size, dilation_size));
  
      // Apply the dilation operation
      dilate(src, dilation_dst, element);
      imwrite("c:\\001a_dilate.bmp", dilation_dst);
      imshow("Dilation Demo", dilation_dst);
  
      waitKey(0);
      return 0;
  }
  </code></pre>