title: How to efficiently merge two overlapping contours into one big contour?
body: <p>I have a huge image ( about 63000 x 63000 pixels = 3969 Megapixels )
  what i have done so far is i decided to make "tiles" of  (1024 x 1024) and do my calculations based on these tiles, resulting in an 62 x 62 image tile grid!
  (this works out very well and has the advantage of making the image viewable with zoom-in and zoom out, only viewn tiles are downsized for example)
  But what i need now are the contours from the huge image!</p>
  
  <ul>
  <li>i use the OpenCV function "findContours" to detect contours on each
  one of the tiles.</li>
  <li>i have added some overlap in the tiles so i get
  overlapping contours ( 1 pixel overlap )</li>
  <li>i used the offset parameter
  of "findContours" to shift the contours to the right position<br>
  into the "virtual total image"</li>
  </ul>
  
  <p>Here are some screenshot's i made from a demo application
  <img src="https://i.stack.imgur.com/Lj3BF.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/K14cM.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/3f0mP.png" alt="enter image description here"></p>
  
  <p>What I want is this:
  <img src="https://i.stack.imgur.com/j4afb.png" alt="enter image description here"></p>
  
  <p>Now my questions: </p>
  
  <ol>
  <li>is it possible to stitch the contours, my worst case is a contour which covers the total image... is there some library that can do this?</li>
  <li>is there a library which works on a compressed version of the total image ( like rle for example )</li>
  <li>is there a way to make opencv findcontours work on 1 bit binary images ?</li>
  </ol>
  
  <p>Here's the code used by findcontours:</p>
  
  <pre><code>    // Surf2DTiledData ...a gobject based class used for 2d tile management and viewing.. 
  Surf2DTiledData* td = (Surf2DTiledData*)in_td;
  
  int nr_hor_tiles = surf2_d_tiled_data_get_nr_hor_tiles(td);
  int nr_ver_tiles = surf2_d_tiled_data_get_nr_ver_tiles(td);
  int tile_size_x = surf2_d_tiled_data_get_tile_width(td);
  int tile_size_y = surf2_d_tiled_data_get_tile_height(td);
  
  contouring_data_obj = surf2_d_tiled_data_get_ContouringData(td);
  p_contours = contouring_data_obj-&gt;p_contours;
  p_border_contours = contouring_data_obj-&gt;p_border_contours;
  
  g_return_if_fail(p_border_contours != NULL);
  g_return_if_fail(p_contours != NULL);
  
  
  for (y = 0; y &lt; nr_ver_tiles; y++){
      int x;
      for (x = 0; x &lt; nr_hor_tiles; x++){
          int idx = x + y*nr_hor_tiles;
          CvMemStorage *mem = contouring_data_obj-&gt;contour_storage[idx];
          CvMat _src;
          CvSeq *contours = NULL;
          uchar* dataBuffer = (uchar*)p_data[x][y];
  
          // the idea is to have some extra space available for the overlap
          // detection of contours!
          // the extra space is needed for the algorithm to check for
          // overlaps of contours later on!
  #define VIRT_BORDER_EXTEND 2
          int virtual_x = x * tile_size_x - VIRT_BORDER_EXTEND;
          int virtual_y = y * tile_size_y - VIRT_BORDER_EXTEND;
  
          int virtual_width = tile_size_x + VIRT_BORDER_EXTEND * 2;
          int virtual_height = tile_size_y + VIRT_BORDER_EXTEND * 2;
          int x_off = -VIRT_BORDER_EXTEND;
          int y_off = -VIRT_BORDER_EXTEND;
          if (virtual_x &lt; 0) {
              virtual_width += virtual_x;
              virtual_x = 0;
              x_off = 0;
          }
  
          if (virtual_y &lt; 0) {
              virtual_height += virtual_y;
              virtual_y = 0;
              y_off = 0;
          }
  
          if ((virtual_x + virtual_width) &gt; (nr_hor_tiles*tile_size_x)) {
              virtual_width = nr_hor_tiles*tile_size_x - virtual_x;
          }
  
          if ((virtual_y + virtual_height) &gt; (nr_ver_tiles*tile_size_y)) {
              virtual_height = nr_ver_tiles*tile_size_y - virtual_y;
          }
  
          CvMat* _roi_mat = get_roi_mat(td,
                             virtual_x, virtual_y,
                             virtual_width, virtual_height);
  
          // Use either this:
          //mem = cvCreateMemStorage(0);
  
          if (_roi_mat){
              // CV_LINK_RUNS =&gt; different algorithm!!!!
              int tile_off_x = tile_size_x * x;
              int tile_off_y = tile_size_y * y;
  
              CvPoint contour_shift = cvPoint(x_off + tile_off_x, y_off + tile_off_y);
  
              int n = cvFindContours(_roi_mat, mem, &amp;contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, contour_shift);
  
              cvReleaseMat(&amp;_roi_mat);
  
              p_contours[x][y] = contours;
          }
  
          //cvReleaseMemStorage(&amp;mem);
      }
  }
  </code></pre>
  
  <p>later i used opengl to make textures out of the tiles and for every tile there is a quad !
  the opencv contours are not drawn as this could be too slow for now, but i draw their bounding boxes... which are drawn in opengl too..</p>