title: Boundry detect paper sheet opencv
body: <p>I am new in openCV, I already detect edge of paper sheet but my result image is blurred after draw lines on edge, How I can draw lines on edges of paper sheet so my image quality remain unaffected.</p>
  
  <p>what I am Missing..</p>
  
  <p>My code is below.</p>
  
  <p>Many thanks.</p>
  
  <p><img src="https://i.stack.imgur.com/uwS2s.png" alt="enter image description here"></p>
  
  <pre><code>-(void)forOpenCV
  {
     if( imageView.image != nil )
     {
  
        cv::Mat greyMat=[self cvMatFromUIImage:imageView.image];
        vector&lt;vector&lt;cv::Point&gt; &gt; squares;
  
        cv::Mat img= [self debugSquares: squares: greyMat ];
  
  
        imageView.image =[self UIImageFromCVMat: img];
  
     }
  
  }
  
  
  - (cv::Mat) debugSquares: (std::vector&lt;std::vector&lt;cv::Point&gt; &gt;) squares : (cv::Mat &amp;)image
  {
  NSLog(@"%lu",squares.size());
  
  // blur will enhance edge detection
  
  Mat blurred(image);
  medianBlur(image, blurred, 9);
  
  Mat gray0(image.size(), CV_8U), gray;
  vector&lt;vector&lt;cv::Point&gt; &gt; contours;
  
  // find squares in every color plane of the image
  for (int c = 0; c &lt; 3; c++)
  {
      int ch[] = {c, 0};
      mixChannels(&amp;image, 1, &amp;gray0, 1, ch, 1);
  
      // try several threshold levels
      const int threshold_level = 2;
      for (int l = 0; l &lt; threshold_level; l++)
      {
          // Use Canny instead of zero threshold level!
          // Canny helps to catch squares with gradient shading
          if (l == 0)
          {
              Canny(gray0, gray, 10, 20, 3); //
  
              // Dilate helps to remove potential holes between edge segments
              dilate(gray, gray, Mat(), cv::Point(-1,-1));
          }
          else
          {
              gray = gray0 &gt;= (l+1) * 255 / threshold_level;
          }
  
          // Find contours and store them in a list
          findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
  
          // Test contours
          vector&lt;cv::Point&gt; approx;
          for (size_t i = 0; i &lt; contours.size(); i++)
          {
              // approximate contour with accuracy proportional
              // to the contour perimeter
              approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
  
              // Note: absolute value of an area is used because
              // area may be positive or negative - in accordance with the
              // contour orientation
              if (approx.size() == 4 &amp;&amp;
                  fabs(contourArea(Mat(approx))) &gt; 1000 &amp;&amp;
                  isContourConvex(Mat(approx)))
              {
                  double maxCosine = 0;
  
                  for (int j = 2; j &lt; 5; j++)
                  {
                      double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                      maxCosine = MAX(maxCosine, cosine);
                  }
  
                  if (maxCosine &lt; 0.3)
                      squares.push_back(approx);
              }
          }
      }
  }
  
  NSLog(@"%lu",squares.size());
  
  
  for( size_t i = 0; i &lt; squares.size(); i++ )
  {
  
  
      cv:: Rect rectangle = boundingRect(Mat(squares[i]));
      if(i==squares.size()-1)////Detecting Rectangle here
      {
          const cv::Point* p = &amp;squares[i][0];
  
  
          int n = (int)squares[i].size();
  
           NSLog(@"%d",n);
  
  
  
          line(image, cv::Point(507,418), cv::Point(507+1776,418+1372), Scalar(255,0,0),2,8);
  
          polylines(image, &amp;p, &amp;n, 1, true, Scalar(255,255,0), 5, CV_AA);
  
  
  
          fx1=rectangle.x;
          fy1=rectangle.y;
          fx2=rectangle.x+rectangle.width;
          fy2=rectangle.y+rectangle.height;
  
  
          line(image, cv::Point(fx1,fy1), cv::Point(fx2,fy2), Scalar(0,0,255),2,8);
  
  
      }
  
  
  
  }
  
  
  return image;
  }
  </code></pre>