本篇内容:

* 图片读取

* 图片高宽

* 图片ROI

* 图片缩放

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

[OpenCV] Samples 18: Load image and check its attributes的更多相关文章

  1. [OpenCV] Samples 10: imagelist_creator

    yaml写法的简单例子.将 $ ./ 1 2 3 4 5 命令的参数(代表图片地址)写入yaml中. 写yaml文件. 参考:[OpenCV] Samples 06: [ML] logistic re ...

  2. [OpenCV] Samples 16: Decompose and Analyse RGB channels

    物体的颜色特征决定了灰度处理不是万能,对RGB分别处理具有相当的意义. #include <iostream> #include <stdio.h> #include &quo ...

  3. [OpenCV] Samples 06: [ML] logistic regression

    logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...

  4. [OpenCV] Samples 06: logistic regression

    logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...

  5. [OpenCV] Samples 13: opencv_version

    cv::CommandLineParser的使用. I suppose CommandLineParser::has("something") should be true whe ...

  6. [OpenCV] Samples 12: laplace

    先模糊再laplace,也可以替换为sobel等. 变换效果后录成视频,挺好玩. #include "opencv2/videoio/videoio.hpp" #include & ...

  7. [OpenCV] Samples 09: image

    根据需求,转化为不同的颜色格式,split后处理各自通道. plImage <==> Mat 格式转换 Mat --> plImage 简单写法: IplImage copy = m ...

  8. [OpenCV] Samples 03: cout_mat

    操作Mat元素时:I.at<double>(1,1) = CV_PI; /* * * cvout_sample just demonstrates the serial out capab ...

  9. [OpenCV] Samples 02: [ML] kmeans

    注意Mat作为kmeans的参数的含义. 扩展:高维向量的聚类. #include "opencv2/highgui.hpp" #include "opencv2/cor ...

随机推荐

  1. eclipse如何导入jar包 BUILD PATH

    http://blog.csdn.net/believejava/article/details/41750987

  2. JSP_tomcat_mysql_注冊验证用户;

    本文出自:http://blog.csdn.net/svitter 资源下载: github: git clone https://github.com/Svtter/JSP-tomcat-mysql ...

  3. Unity Shader-后处理:高斯模糊

    一.简介   上一篇文章学习了模糊的原理以及基本的模糊实现,对于清晰和模糊这个定义感觉还是比较说明问题,这里再贴出一下:“清晰的图片,各个像素之间会有明显的过渡,而如果各个像素之间的差距不是很大,那么 ...

  4. java获取文件列表,并按照目录的深度及文件名的拼音的升序排列

    java实现在线浏览zip文件及文件下载 首先用java读出目录或是zip下的所有文件 1KG_20140718_HD/Readme-说明.htm:3.00KB1KG_20140718_HD/一键GH ...

  5. 如何让FireFox/chrome新打开的标签页在后台打开,而不是立即跳转过去

    firefox: 地址栏输入about:config 找到下面三项,全部设为true browser.tabs.loadInBackground browser.tabs.loadDivertedIn ...

  6. tensorflow之数据读取探究(1)

    Tensorflow中之前主要用的数据读取方式主要有: 建立placeholder,然后使用feed_dict将数据feed进placeholder进行使用.使用这种方法十分灵活,可以一下子将所有数据 ...

  7. Js实现页面关键字高亮显示

    <!DOCTYPE HTML> <html lang="en"> <meta http-equiv="Content-Type" ...

  8. [C++] const与重载

    下面的两个函数构成重载吗? void M(int a){} //(1) void M(const int a){} //(2) 下面的呢? void M(int& a){} //(3) voi ...

  9. ios mac 对照片进行JPEG压缩

    ios mac 对照片进行JPEG压缩 1. 在iOS上可以使用 API UIImageJPEGRepresentation 对照片数据进行JPEG压缩: 我们知道iOS其实是MAC OS 的移植,那 ...

  10. Jmeter进行接口测试

    原文地址:https://www.cnblogs.com/nancyzhu/p/8035042.html web接口测试工具: 手工测试的话可以用postman ,自动化测试多是用到 Jmeter(开 ...