在OpenCV中调整图像大小

4

我对opencv和图像处理比较新,我在pyimagesearch.com上找到了这段代码并尝试使用。我将图像的高度调整为500像素以执行边缘检测和查找轮廓。

r = 500.0 / image.shape[1]
dim = (500,int(image.shape[0] * r))
image = cv2.resize(image,dim,interpolation = cv2.INTER_AREA)
ratio  = image.shape[0] /500.0

我将处理后的图像再次乘以比率(以便针对原始图像进行更改)。

warped = four_point_transform(orig,screenCnt.reshape(4,2)*ratio)
r = 500.0 / warped.shape[1]
dim = (500,int(warped.shape[0] * r))

warped = cv2.resize(warped,dim,interpolation = cv2.INTER_AREA)
cv2.imshow("Perspective Transform",warped)

在此之后,我得到的结果有些像这样图片。只有部分图像是可见的,而我无法看到其余的图像。请帮助我。谢谢!
2个回答

2

我觉得你可能混淆了宽度和高度。

对于普通图像,image.shape[]将按照高度、宽度和通道的顺序给出图像的形状。

因此,代码应该如下所示:

newHeight = 500.0
oldHeight = image.shape[0]
oldWidth = image.shape[1]
r = newHeight / oldHeight
newWidth = int(oldWidth * r)
dim = (newHeight, newWidth)
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
aspectRatio = image.shape[1] / image.shape[0]

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接