Image loses quality with cv2.warpPerspective
<p>I am working with OpenCV 3.1 and with Python. </p>

<p>My problem comes when I try to deskew (fix the tilt of) an image with text. I am using <code>cv2.warpPerspective</code> to make it possible, but the image loses a lot of quality. You can see here the original part of the image:</p>

<p><a href="http://i.stack.imgur.com/V9OZn.png" rel="nofollow"><img src="http://i.stack.imgur.com/V9OZn.png" alt="image without smooth"></a></p>

<p>and then, here, the "rotated" image:</p>

<p><a href="http://i.stack.imgur.com/v8cHe.png" rel="nofollow"><img src="http://i.stack.imgur.com/v8cHe.png" alt="image with smooth"></a></p>

<p>it is like <em>smoothed</em>. </p>

<p>I was using morphological transformation like:</p>

<pre><code>kernel = np.ones((2, 2), np.uint8)
blur_image = cv2.erode(tresh, kernel, iterations=1)
</code></pre>

<p>and </p>

<pre><code>white_mask2 = cv2.morphologyEx(white_mask2, cv2.MORPH_OPEN, kernel)
</code></pre>

<p>to see if it improves something, but nothing.</p>

<p>I saw <a href="http://stackoverflow.com/questions/22656698/perspective-correction-in-opencv-using-python">this example here in SO</a>, but those guys had the same problem:
<a href="http://i.stack.imgur.com/pBChb.png" rel="nofollow"><img src="http://i.stack.imgur.com/pBChb.png" alt="enter image description here"></a> and <a href="http://i.stack.imgur.com/2WbGv.png" rel="nofollow"><img src="http://i.stack.imgur.com/2WbGv.png" alt="enter image description here"></a></p>

<p>So, I don't have idea what can I do. 
Maybe there is a way to not losing the quality of the image, or, there's another method to rotate the image without quality lost. 
I know this method: </p>

<pre><code>root_mat = cv2.getRotationMatrix2D(to_rotate_center, angle, 1.0)
result = cv2.warpAffine(to_rotate, root_mat, to_rotate.shape, flags=cv2.INTER_LINEAR)
</code></pre>

<p>But it doesn't work for me, as I have to rotate every rectangle here:</p>

<p><a href="http://i.stack.imgur.com/ThKC9.png" rel="nofollow"><img src="http://i.stack.imgur.com/ThKC9.png" alt="enter image description here"></a></p>

<p>and not the whole image. It means, the best way I found to do it, was <code>warpPerspective</code>, and it works fine, but with quality loss. I would appreciate an advice to avoid the quality lost. </p>

<p>The problem is related to the interpolation required by the warping. If you don't want things to appear smoother, you should switch from default interpolation method, which is <code>INTER_LINEAR</code> to another one, such as <code>INTER_NEAREST</code>. It would help you with the sharpness of the edges.</p>

<p>Try <code>flags=cv2.INTER_NEAREST</code> on your <code>warp</code> call, be it <a href="http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#cv2.warpAffine" rel="nofollow">warpAffine()</a> or <a href="http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#cv2.warpPerspective" rel="nofollow">warpPerspective()</a>.</p>

<p>Interpolation flags are listed <a href="http://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#ga5bb5a1fea74ea38e1a5445ca803ff121" rel="nofollow">here</a>.</p>

<pre><code>enum InterpolationFlags { 
    INTER_NEAREST = 0, 
    INTER_LINEAR = 1, 
    INTER_CUBIC = 2, 
    INTER_AREA = 3, 
    INTER_LANCZOS4 = 4, 
    INTER_MAX = 7, 
    WARP_FILL_OUTLIERS = 8, 
    WARP_INVERSE_MAP = 16 
}
</code></pre>

python image opencv image-rotation opencv3.1