[OpenCV] Samples 18: Load image and check its attributes
本篇内容:
* 图片读取
* 图片高宽
* 图片ROI
* 图片缩放
- Ref: http://blog.csdn.net/u012005313/article/details/51943442
-
官网2.3.2参考:Geometric Image Transformations
interpolation - 插值方法。共有5种:
1)INTER_NEAREST - 最近邻插值法
2)INTER_LINEAR - 双线性插值法(默认)
3)INTER_AREA - 基于局部像素的重采样(resampling using pixel area relation)。对于图像抽取(image decimation)来说,这可能是一个更好的方法。但如果是放大图像时,它和最近邻法的效果类似。
4)INTER_CUBIC - 基于4x4像素邻域的3次插值法
5)INTER_LANCZOS4 - 基于8x8像素邻域的Lanczos插值
通过matplotlib.pyplot显示图片,有新意。
import cv2 import matplotlib.pyplot as plt img = cv2.imread('flower.jpg') # 插值:interpolation # None本应该是放图像大小的位置的,后面设置了缩放比例, #所有就不要了 res1 = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC) #直接规定缩放大小,这个时候就不需要缩放因子 height,width = img.shape[:2] res2 = cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC) plt.subplot(131) plt.imshow(img) plt.subplot(132) plt.imshow(res1) plt.subplot(133) plt.imshow(res2)
功能的代码实例:
for name in filteredFiles:
# 1.读取图片
img = cv2.imread(join(dirPath, name))
print("-----------------------")
print("Debug:", name, img.shape)
# 2.(高,长,channel)
rows = img.shape[0]
cols = img.shape[1]
start_row = 0
start_col = 0
if (rows > cols):
# vertical
start_row = (rows - cols) // 2
else:
# horizon
start_col = (cols - rows) // 2
min_edge=min(rows, cols)
# 3.截取局部区域 ROI
imgROI=img[start_row:start_row+min_edge, start_col:start_col+min_edge]
print("Debug:", imgROI.shape)
# cv2.imshow("show", imgROI)
# cv2.waitKey(0)
# 4.缩放
# ref: http://blog.csdn.net/u012005313/article/details/51943442
# ref: http://blog.csdn.net/on2way/article/details/46801063
# 如果想要收缩图像,那么使用重采样差值法效果最好;
# 如果想要放大图像,那么最好使用3次差值法或者线性差值法(文档推荐的).
img_zo = cv2.resize(imgROI, (final_size, final_size), interpolation=cv2.INTER_AREA)
print("Debug:", img_zo.shape)
# cv2.imshow("show", img_zo)
# cv2.waitKey(0)
ret_name = "square_"+name