本篇内容:

* 图片读取

* 图片高宽

* 图片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. CocosCreator动作系统

    废话没有,直接上代码,有注释可以看的, cc.Class({ extends: cc.Component, properties: { player:{ default:null, type:cc.N ...

  2. spring cloud:Edgware.RELEASE版本hystrix超时新坑

    升级到Edgware.RELEASE发现,zuul中不管如何设置hystrix的超时时间均不起作用,仍然是默认的1000ms.  降回低版本后正常,但是低版本的fallback方法中,又拿不到详细异常 ...

  3. delphi获取文件的创建/修改时间、按时间删除指定文件下的文件

    uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrl ...

  4. Date与Quartz的cron之间的相互转换

    1.概述 最近在写一个Quartz相关的模块,需要实现Date与Quartz的cron之间的相互转换,在网上查了一下竟然没有找到,但是找到一份这样的博客:http://hw1287789687.ite ...

  5. java command line error opening registry key 'Software\JavaSoft\Java Runtime Environment' java.dll

    C:\Users\huxxxxchan>javaError: opening registry key 'Software\JavaSoft\Java Runtime Environment'E ...

  6. 2017 33 款iOS开源库

    IGListKit https://github.com/Instagram/IGListKit 由 Instagram 开发人员制作,IGListKit 是用于构建快速灵活列表的数据驱动型的 UIC ...

  7. excel表格 xls、xlsx 读取

    public static void main(String[] args) throws Exception { // getdslContext(); String file = "F: ...

  8. 【转】iPhone X

    iPhone X 在 CIIA 第一期报告中,我剖析了 iPhone 从诞生以来就存在的,以及后来产生的一些设计问题.昨天在苹果店里玩了一下 iPhone X,发现它不但继承了以往的 iPhone 的 ...

  9. 搞定所有的跨域请求问题 jsonp CORS

    网上各种跨域教程,各种实践,各种问答,除了简单的 jsonp 以外,很多说 CORS 的都是行不通的,老是缺那么一两个关键的配置.本文只想解决问题,所有的代码经过亲自实践.   本文解决跨域中的 ge ...

  10. MySQL保存历史执行语句

    MySQL想查看历史执行语句,不能像linux那样通过命令查看 只能将每条执行语句保存到LOG里面,然后通过LOG文件再查找 修改my.cnf配置文件 [mysqld] 字段部分,定义LOG名字为ex ...