python – 在open cv中调整图像大小

我对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)

在此之后,我得到的结果有点像this Image.只有部分图像可见,我无法看到图像的其余部分.请帮我.谢谢!

最佳答案 在我看来,你正在混合宽度和高度.

对于常规图像,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]
点赞