使用cv2.warpPerspective图像质量下降 [英] Image loses quality with cv2.warpPerspective
问题描述
我正在使用OpenCV 3.1和Python.
I am working with OpenCV 3.1 and with Python.
当我尝试用文字校正图像(校正其倾斜度)时出现问题.我正在使用cv2.warpPerspective
使其成为可能,但是图像质量下降很多.您可以在此处看到图像的原始部分:
My problem comes when I try to deskew (fix the tilt of) an image with text. I am using cv2.warpPerspective
to make it possible, but the image loses a lot of quality. You can see here the original part of the image:
,然后是旋转的"图像:
and then, here, the "rotated" image:
就像平滑.
我正在使用形态转换,例如:
I was using morphological transformation like:
kernel = np.ones((2, 2), np.uint8)
blur_image = cv2.erode(tresh, kernel, iterations=1)
和
white_mask2 = cv2.morphologyEx(white_mask2, cv2.MORPH_OPEN, kernel)
看看它是否可以改善某些东西,但无济于事.
to see if it improves something, but nothing.
我在SO中看到了此示例,但是那些家伙有同样的问题: 和
I saw this example here in SO, but those guys had the same problem: and
所以,我不知道该怎么办. 也许有一种不丢失图像质量的方法,或者还有另一种旋转图像而不丢失质量的方法. 我知道这种方法:
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:
root_mat = cv2.getRotationMatrix2D(to_rotate_center, angle, 1.0)
result = cv2.warpAffine(to_rotate, root_mat, to_rotate.shape, flags=cv2.INTER_LINEAR)
但这对我不起作用,因为我必须在这里旋转每个矩形:
But it doesn't work for me, as I have to rotate every rectangle here:
,而不是整个图像.这意味着,我发现做到这一点的最佳方法是warpPerspective
,它工作正常,但质量下降.我希望您能避免质量下降的建议.
and not the whole image. It means, the best way I found to do it, was warpPerspective
, and it works fine, but with quality loss. I would appreciate an advice to avoid the quality lost.
推荐答案
问题与翘曲所需的插值有关.如果您不希望事情看起来更平滑,则应从默认的插值方法(c2)切换到另一种插值方法,例如INTER_NEAREST
.它将帮助您提高边缘的清晰度.
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 INTER_LINEAR
to another one, such as INTER_NEAREST
. It would help you with the sharpness of the edges.
在warp
调用中尝试flags=cv2.INTER_NEAREST
,它是 warpPerspective( ).
Try flags=cv2.INTER_NEAREST
on your warp
call, be it warpAffine() or warpPerspective().
在此处中列出.... >
Interpolation flags are listed here.
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
}
这篇关于使用cv2.warpPerspective图像质量下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!