title: How to crop the image on opencv and dlib on ubuntu
body: <p>I want to do these on the opencv and dlib using c++ on ubuntu. </p>
  
  <ol>
  <li>Detect the human face using dlib.(I did it already.)</li>
  <li>crop the image of only around mouth part. </li>
  </ol>
  
  <p>here is my code. it is base on dlib sample code. </p>
  
  <pre><code>#include &lt;dlib/image_processing/frontal_face_detector.h&gt;
  #include &lt;dlib/image_processing/render_face_detections.h&gt;
  #include &lt;dlib/image_processing.h&gt;
  #include &lt;dlib/gui_widgets.h&gt;
  #include &lt;dlib/image_io.h&gt;
  #include &lt;iostream&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;highgui.h&gt;
  
  
  using namespace dlib;
  using namespace std;
  
  // ----------------------------------------------------------------------------------------
  
  int main(int argc, char** argv)
  {  
      try
      {
          // This example takes in a shape model file and then a list of images to
          // process.  We will take these filenames in as command line arguments.
          // Dlib comes with example images in the examples/faces folder so give
          // those as arguments to this program.
          if (argc == 1)
          {
              cout &lt;&lt; "Call this program like this:" &lt;&lt; endl;
              cout &lt;&lt; "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" &lt;&lt; endl;
              cout &lt;&lt; "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
              cout &lt;&lt; "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" &lt;&lt; endl;
              return 0;
          }
  
          // We need a face detector.  We will use this to get bounding boxes for
          // each face in an image.
          frontal_face_detector detector = get_frontal_face_detector();
          // And we also need a shape_predictor.  This is the tool that will predict face
          // landmark positions given an image and face bounding box.  Here we are just
          // loading the model from the shape_predictor_68_face_landmarks.dat file you gave
          // as a command line argument.
          shape_predictor sp;
          deserialize(argv[1]) &gt;&gt; sp;
      cv::Mat cimg = cv::imread(argv[1]);
  
  
          image_window win, win_faces;
          // Loop over all the images provided on the command line.
          for (int i = 2; i &lt; argc; ++i)
          {
              cout &lt;&lt; "processing image " &lt;&lt; argv[i] &lt;&lt; endl;
              array2d&lt;rgb_pixel&gt; img;
              load_image(img, argv[i]);
  /*
              // Make the image larger so we can detect small faces.
              pyramid_up(img);
  */
              // Now tell the face detector to give us a list of bounding boxes
              // around all the faces in the image.
              std::vector&lt;rectangle&gt; dets = detector(img);
              cout &lt;&lt; "Number of faces detected: " &lt;&lt; dets.size() &lt;&lt; endl;
  
              // Now we will go ask the shape_predictor to tell us the pose of
              // each face we detected.
              std::vector&lt;full_object_detection&gt; shapes;
              for (unsigned long j = 0; j &lt; dets.size(); ++j)
              {
                  full_object_detection shape = sp(img, dets[j]);
                  cout &lt;&lt; "number of parts: "&lt;&lt; shape.num_parts() &lt;&lt; endl;
                  cout &lt;&lt; "pixel position of first part:  " &lt;&lt; shape.part(0) &lt;&lt; endl;
                  cout &lt;&lt; "pixel position of second part: " &lt;&lt; shape.part(1) &lt;&lt; endl;
                  // You get the idea, you can get all the face part locations if
                  // you want them.  Here we just store them in shapes so we can
                  // put them on the screen.
                  shapes.push_back(shape);
              }
  
          // Crop the original image to the defined ROI */
          cv::Rect roi;
          roi.x = 0;
          roi.y = 0;
          roi.width = 200;
          roi.height = 200;
  
          cv::Mat crop = cimg(roi);
          cv::imshow("crop", crop);
  
              // Now let's view our face poses on the screen.
  /*
              win.clear_overlay();
              win.set_image(img);
              win.add_overlay(render_face_detections(shapes));
  
              // We can also extract copies of each face that are cropped, rotated upright,
              // and scaled to a standard size as shown here:
              //dlib::array&lt;array2d&lt;rgb_pixel&gt; &gt; face_chips;
              //extract_image_chips(img, get_face_chip_details(shapes), face_chips);
              //win_faces.set_image(tile_images(face_chips));
  */
  
              cout &lt;&lt; "Hit enter to process the next image..." &lt;&lt; endl;
              cin.get();
          }
      }
      catch (exception&amp; e)
      {
          cout &lt;&lt; "\nexception thrown!" &lt;&lt; endl;
          cout &lt;&lt; e.what() &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>but it gives me this error.</p>
  
  <pre><code>OpenCV Error: Assertion failed (0 &lt;= roi.x &amp;&amp; 0 &lt;= roi.width &amp;&amp; roi.x + roi.width &lt;= m.cols &amp;&amp; 0 &lt;= roi.y &amp;&amp; 0 &lt;= roi.height &amp;&amp; roi.y + roi.height &lt;= m.rows) in Mat, file /home/bigadmin/opencv-3.1.0/modules/core/src/matrix.cpp, line 508
  
  exception thrown!
  /home/bigadmin/opencv-3.1.0/modules/core/src/matrix.cpp:508: error: (-215) 0 &lt;= roi.x &amp;&amp; 0 &lt;= roi.width &amp;&amp; roi.x + roi.width &lt;= m.cols &amp;&amp; 0 &lt;= roi.y &amp;&amp; 0 &lt;= roi.height &amp;&amp; roi.y + roi.height &lt;= m.rows in function Mat
  </code></pre>
  
  <p>please teach me the solution to solve this issue.</p>
  
  <p>thanks.</p>