转自:https://www.cnblogs.com/dyufei/p/8205121.html

一. 主要函数介绍

1) 图像大小变换 cvResize ()

原型:

voidcvResize(const CvArr* src,CvArr* dst,intinterpolation=CV_INTER_LINEAR
);

说明:

src 表示输入图像。
dst表示输出图像。
intinterpolation插值方法,有以下四种:

CV_INTER_NN - 最近邻插值,
CV_INTER_LINEAR - 双线性插值 (缺省值)
CV_INTER_AREA - 使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN 方法..
CV_INTER_CUBIC - 立方插值.

2)图像读取 imread()

原型:

python:
cv2.imread(filename[, flags]) → retval
c++:
Mat imread(const string& filename, int flags=1 )

说明:

filename 表示图像的路径和名称(不在工作路径要提供绝对路径,否则读不到也不会报错)
params 表示 的加载方式
python:

cv2.IMREAD_COLOR:读入一副彩色图像。图像的透明度会被忽略, 这是默认参数。
cv2.IMREAD_GRAYSCALE:以灰度模式读入图像

c++:

CV_LOAD_IMAGE_COLOR 彩色
CV_LOAD_IMAGE_GRAYSCALE 灰度

3)图像创建 imwrite()

原型:

python:
cv2.imwrite(filename, image[, params])
c++:
bool imwrite(const string& filename, InputArray image, const vector<int>& params=vector<int>())

说明:

filename 表示写图像的路径和名称(不在工作路径要提供绝对路径)
image 是要保存的图像数据
params 表示 图像保存方式python可以不用提供,但C++必须根据根式设置正确,否则保存不了图片。

注意: C++ 中 imwrite(函数的) params参数 :

参数与保存的图像类型相关,如果参数未指定文件保存不成功,具体根据保存的图像类型具体设置

1)JPEG,参数为CV_IMWRITE_JPEG_QUALITY,它的值是从0到100,值越小压缩的越多,默认值是95.
2)PNG,参数为CV_IMWRITE_PNG_COMPRESSION,它的值是从0到9,值越大表示图片尺寸越小,压缩时间越长。默认值是3。
3)PPM,PGM或者PBM,参数为CV_IMWRITE_PXM_BINARY,它的值是0或者1。默认值是1。

二、实例

python版(python3.5 opencv3.4):

import numpy as np
import cv2 def resizeImage(image,width=None,height=None,inter=cv2.INTER_AREA):
newsize = (width,height)
#获取图像尺寸
(h,w) = image.shape[:2]
if width is None and height is None:
return image
#高度算缩放比例
if width is None:
n = height/float(h)
newsize = (int(n*w),height)
else :
n = width/float(w)
newsize = (width,int(h*n)) # 缩放图像
newimage = cv2.resize(image, newsize, interpolation=inter)
return newimage imageOriginal = cv2.imread("test.jpg")
cv2.imshow("Original", imageOriginal)
#获取图像尺寸
w = width=imageOriginal.shape[1]
h = width=imageOriginal.shape[2]
print ("Image size:",w,h)
#放大2倍
newimage = resizeImage(imageOriginal,w*2,h*2,cv2.INTER_LINEAR)
cv2.imshow("New", newimage)
#保存缩放后的图像
cv2.imwrite('newimage.jpg',newimage)
#缩小5倍
newimage2 = resizeImage(imageOriginal,int(w/5),int(h/5),cv2.INTER_LINEAR)
cv2.imwrite('newimage2.jpg',newimage2)

C++ 版(imageResize.cpp)

#include <iostream>
#include "opencv2/opencv.hpp" using namespace std;
using namespace cv; void imageResize(Mat image, Mat* dst, int width, int height, int inter = CV_INTER_AREA )
{ int w = image.cols;
int h = image.rows;
int newW = width;
int newH = height;
if(width == 0 && height ==0){
return;
}
if(width == 0){
float re = h/(float)height;
newW = (int) w * re;
} else {
float re = w/(float)width;
newH = (int) h * re;;
} resize(image, *dst, Size(newW, newH),inter); } int main()
{
const char* filename = "test.jpg";
Mat image,dst;
//image = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
image = imread(filename, CV_LOAD_IMAGE_COLOR);
if (image.empty()) {
std::cout<<"Faild open file.";
}
//imshow("image", image);
//image.cols为图像的宽度 image.cols为图像的高度
int w = image.cols;
int h = image.rows;
std::cout<<"Image size:"<<w <<" * "<<h<<std::endl;
imageResize(image,&dst,w * 2, h * 2); std::cout<<"new Image size:"<<dst.cols <<" * "<<dst.rows<<std::endl;
vector<int> compression_params;
//JPEG,参数为CV_IMWRITE_JPEG_QUALITY,值是从0到100,值越小压缩的越多
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);
//imshow("dstImage", dst);
imwrite("dstImage.jpg",dst,compression_params); return 0;
}

编译:

 sudo g++ imageResize.cpp  -o resize  `pkg-config --cflags --libs opencv

OpenCV - opencv3 图像处理 之 图像缩放( python与c++实现 )的更多相关文章

  1. openCV(三)---图像缩放

    UIImage *img1 = [UIImage imageNamed:@"1448941176867"]; //将UIImage转换为IplImage格式 IplImage *p ...

  2. opencv学习笔记3——图像缩放,翻转和阈值分割

    #图像的缩放操作 #cv.resize(src,dsize,dst=None,,fx=None,fy=None,interpolation=None) #src->原图像,dsize->目 ...

  3. opencv3 图像处理(一)图像缩放( python与c++ 实现)

    opencv3 图像处理 之 图像缩放( python与c++实现 ) 一. 主要函数介绍 1) 图像大小变换 Resize () 原型: void Resize(const CvArr* src,C ...

  4. 【python图像处理】图像的缩放、旋转与翻转

    [python图像处理]图像的缩放.旋转与翻转 图像的几何变换,如缩放.旋转和翻转等,在图像处理中扮演着重要的角色,python中的Image类分别提供了这些操作的接口函数,下面进行逐一介绍. 1.图 ...

  5. 邻近双线性插值图像缩放的Python实现

    最近在查找有关图像缩放之类的算法,因工作中需要用到诸如此类的图像处理算法就在网上了解了一下相关算法,以及其原理,并用Python实现,且亲自验证过,在次与大家分享. 声明:本文代码示例针对的是plan ...

  6. opencv学习笔记——图像缩放函数resize

    opencv提供了一种图像缩放函数 功能:实现对输入图像缩放到指定大小 函数原型: void cv::resize ( InputArray src, OutputArray dst, Size ds ...

  7. OpenCV计算机视觉学习(11)——图像空间几何变换(图像缩放,图像旋转,图像翻转,图像平移,仿射变换,镜像变换)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 图像 ...

  8. Python图像处理丨图像腐蚀与图像膨胀

    摘要:本篇文章主要讲解Python调用OpenCV实现图像腐蚀和图像膨胀的算法. 本文分享自华为云社区<[Python图像处理] 八.图像腐蚀与图像膨胀>,作者: eastmount . ...

  9. < python PIL - 批量图像处理 - RGB图像生成灰度图像 >

    < python PIL - 批量图像处理 - RGB图像生成灰度图像 > 直接用python自带的PIL图像库,将一个文件夹下所有jpg/png的RGB图像转换成灰度/黑白图像 from ...

随机推荐

  1. List contents of directories in a tree-like format

    Python programming practice. Usage: List contents of directories in a tree-like format. #!/usr/bin/p ...

  2. CuteEditor.Editor+a+a+c+a+a.a() System.RuntimeType.get_Assembly() 问题解决方法

    问题: Server Error in '/' Application. Attempt by method 'CuteEditor.Editor+a+a+c+a+a.a()' to access m ...

  3. ZOJ 3961 Let's Chat 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题意 给出两个人的发消息的记录,然后 如果有两人在连续M天 ...

  4. Linux centos7 防火墙设置

    1.查看防火墙状态 systemctl list-unit-files|grep firewalld.service 或 systemctl status firewalld.service 2.开启 ...

  5. Linux文件系统管理 fdisk分区命令

    概述 我们在安装操作系统的过程中已经对系统硬盘进行了分区,但是如果我新添加了一块硬盘,想要正常使用时,在Linux中有专门的分区命令 fdisk 和 parted.其中 fdisk 命令较为常用,但不 ...

  6. STM32探秘 之FSMC

    源:STM32探秘 之FSMC STM32 FSMC总线深入研究

  7. 转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系

    文章名称:Exome Sequencing of Cell-Free DNA from Metastatic Cancer Patients IdentifiesClinically Actionab ...

  8. CentOS 7卸载mariadb安装mysql

    CentOS 7已经将默认集成mariadb而不是mysql,这对于多数还是依赖于mysql的应用来说,需要手动的进行更新. 可能会遇到这样错误,换成MySQL就好了. error 2002 (hy0 ...

  9. RHCE学习笔记 管理1 (第三~五章)

    第三章 红帽企业linux 获取帮助 (略) man .pinfo. 第四章 编辑文件 1.输出重定向到文件和程序 >file    定向文件(覆盖) >>file   定向文件(附 ...

  10. 4950: [Wf2017]Mission Improbable

    4950: [Wf2017]Mission Improbable Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 608  Solved: 222[Sub ...