示例将图像尺寸加倍
调整图像大小有两种方法。可以指定新的大小:
手动;
height, width = src.shape[:2]
dst = cv2.resize(src, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)
通过比例因子。
dst = cv2.resize(src, None, fx = 2, fy = 2, interpolation = cv2.INTER_CUBIC)
,其中fx是沿水平轴的缩放比例, fy是沿垂直轴的缩放比例。
要缩小图像,通常使用INTER_AREA插值效果最佳,而要放大图像,通常使用INTER_CUBIC(速度慢)或INTER_LINEAR(速度更快,但看起来仍然可以)最好。
示例缩小图像以适合最大高度/宽度(保持宽高比)
import cv2
img = cv2.imread('YOUR_PATH_TO_IMG')
height, width = img.shape[:2]
max_height = 300
max_width = 300
# only shrink if img is bigger than required
if max_height < height or max_width < width:
# get scaling factor
scaling_factor = max_height / float(height)
if max_width/float(width) < scaling_factor:
scaling_factor = max_width / float(width)
# resize image
img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
cv2.imshow("Shrinked image", img)
key = cv2.waitKey()
在cv2中使用代码
import cv2 as cv
im = cv.imread(path)
height, width = im.shape[:2]
thumbnail = cv.resize(im, (round(width / 10), round(height / 10)), interpolation=cv.INTER_AREA)
cv.imshow('exampleshq', thumbnail)
cv.waitKey(0)
cv.destroyAllWindows()
0
我想使用OpenCV2.0和Python2.6显示调整大小的图像。我在http://opencv.willowgarage.com/documentation/python/cookbook.html上使用并采用了该示例,但是不幸的是,该代码是针对OpenCV2.1的,并且似乎不适用于2.0。这是我的代码:
由于我不能使用
我用了
相反,这在其他应用程序中没有问题。但是,cv.iplimage没有属性行,列或大小。谁能给我一个提示,如何解决这个问题?谢谢。