本篇内容:

* 图片读取

* 图片高宽

* 图片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. Spark:几种给Dataset增加列的方式、Dataset删除列、Dataset替换null列

    几种给Dataset增加列的方式 首先创建一个DF对象: scala> spark.version res0: String = .cloudera1 scala> val , , 2.0 ...

  2. iOS AVAudioSession 配置(录音完声音变小问题)

    有这么一个场景,首先我们录音,录音完再播放发现音量变小了: 百思不得其解,查看API发现AVAudioSession里面有这么一个选项, 如果你的app涉及到了音视频通话以及播放其他语音,那么当遇到声 ...

  3. spark run using IDE / Maven

    来自:http://stackoverflow.com/questions/26892389/org-apache-spark-sparkexception-job-aborted-due-to-st ...

  4. Python操作redis系列之 列表(list) (五)

    # -*- coding: utf- -*- import redis r =redis.Redis(host=,password=") 1. Lpush 命令将一个或多个值插入到列表头部. ...

  5. 修改oracle为归档模式

    1.查看是否为归档模式 SQL> archive log list; Database log mode No Archive Mode Automatic archival Disabled ...

  6. HTML中input type="text"和type="password" 显示的长度不一样

    在CSS里边加上input {width:100px;}能把所有input标签的控件宽度改为相同! 加上这个属性 style="width:180px;"

  7. elasticsearch日志删除命令

    通过curl发送DELETE命令给elasticsearch服务器,进行日志删除操作.命令示例如下: curl -XDELETE *' curl -XDELETE 'http://192.168.10 ...

  8. golang ---tcmalloc浅析

    总体结构 在tcmalloc内存管理的体系之中,一共有三个层次:ThreadCache.CentralCache.PageHeap,如上图所示.分配内存和释放内存的时候都是按从前到后的顺序,在各个层次 ...

  9. 译:1. RabbitMQ Java Client 之 "Hello World"

    这些教程介绍了使用RabbitMQ创建消息传递应用程序的基础知识.您需要安装RabbitMQ服务器才能完成教程 1. 打造第一个Hello World 程序 RabbitMQ是一个消息代理:它接受和转 ...

  10. Atiitt 使用java语言编写sql函数或存储过程

    Atiitt 使用java语言编写sql函数或存储过程 1.1. java编写sql函数或存储过程的机制1 1.2. Java编写sp的优点1 1.3. 支持java源码,class文件,blog f ...