专业的编程技术博客社区

网站首页 > 博客文章 正文

Python-OpenCV 3. 图像缩放与裁剪

baijin 2024-08-31 16:15:20 博客文章 8 ℃ 0 评论

图像的扩缩裁剪

cv2.resize 缩放:

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst 

参数:

参数描述src【必需】原图像dsize【必需】输出图像所需大小fx【可选】沿水平轴的比例因子fy【可选】沿垂直轴的比例因子interpolation【可选】插值方式

插值方式:

  • cv.INTER_NEAREST 最近邻插值
  • cv.INTER_LINEAR 双线性插值
  • cv.INTER_CUBIC 双线性插值
  • cv.INTER_AREA 使用像素区域关系重新采样。它可能是图像抽取的首选方法,因为它可以提供无莫尔条纹的结果。但是当图像被缩放时,它类似于INTER_NEAREST方法。

通常的,缩小使用cv.INTER_AREA,放缩使用cv.INTER_CUBIC(较慢)和cv.INTER_LINEAR(较快效果也不错)。默认情况下,所有的放缩都使用cv.INTER_LINEAR。

缩放示例:

#-*- coding: utf-8 -*-
import cv2
fn = "test.jpg"
if __name__ == '__main__':
 img = cv2.imread(fn)
 scale_percent = 20
 width = int(img.shape[1] * scale_percent / 100)
 height = int(img.shape[0] * scale_percent / 100)
 dim = (width, height)
 resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
 print('Resized Dimensions : ', resized.shape)
 cv2.imshow("Resized image", resized)
 cv2.waitKey(0)
 cv2.destroyAllWindows()

裁剪

#-*- coding: utf-8 -*-
import cv2
fn = "test.jpg"
if __name__ == '__main__':
 img = cv2.imread(fn)
 scale_percent = 50
 width = int(img.shape[1] * scale_percent / 100)
 height = int(img.shape[0] * scale_percent / 100)
 dim = (width, height)
 resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
 print('Resized Dimensions : ', resized.shape)
 cv2.imshow("Resized image", resized)
 patch_tree = img[150:400, 130:400]
 cv2.imshow("Patched image", patch_tree)
 cv2.waitKey(0)
 cv2.destroyAllWindows()

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表