Transferred from: https://www.cnblogs.com/dyufei/p/8205121.html
I. Main function INTRODUCTION 1) Image size Transform Cvresize () prototype:
voidcvResize(const CvArr* src,CvArr* dst,intinterpolation=CV_INTER_LINEAR);
Description
SRC indicates the input image.
DST represents the output image.
Intinterpolation interpolation method, there are the following four kinds:
Cv_inter_nn-Nearest neighbor interpolation,
Cv_inter_linear-bilinear interpolation (default)
Cv_inter_area-resampling using pixel relationships. This method avoids ripples when the image shrinks. When the image is zoomed in, it resembles the Cv_inter_nn method:
Cv_inter_cubic-cubic interpolation.
2) Image reading Imread () prototype:
python: cv2.imread(filename[, flags]) → retvalc++: Mat imread(const string& filename, int flags=1 )
Description
filename indicates the path and name of the image (no absolute path is provided in the work path, otherwise it will not be read or error)
How the params represents loading
Python:
Cv2. Imread_color: Reads a pair of color images. The transparency of the image is ignored, which is the default parameter.
Cv2. Imread_grayscale: Reading into images in grayscale mode
C++:
Cv_load_image_color Color
Cv_load_image_grayscale Grayscale
3) Image creation Imwrite () prototype:
params]) c++: bool imwrite(const string& filename, InputArray image, const vector<int>& params=vector<int>())
Description
filename indicates the path and name of the write image (no absolute path is provided in the work path)
Image is the data to be saved
The params means that the image is saved in Python, but the C + + must be set correctly based on the radicals, otherwise the picture will not be saved.
Note: The params parameter of imwrite (function) in C + +:
parameter is related to the saved image type, if the parameter does not specify that the file is not saved successfully, depending on the saved image type
1) JPEG, the parameter is cv_imwrite_jpeg_quality, its value is from 0 to 100, the smaller the value the more compression, the default value is 95.
2) PNG, the parameter is cv_imwrite_png_compression, its value is from 0 to 9, the larger the value, the smaller the picture size, the longer the compression time. The default value is 3.
3) PPM,PGM or PBM, the parameter is Cv_imwrite_pxm_binary, and its value is 0 or 1. The default value is 1.
Second, the example Python version (python3.5 opencv3.4):
Import NumPyAs NPImport Cv2Def Resizeimage(Image,width=None, height=None, Inter=Cv2. Inter_area): newsize= (width,height)#获取图像尺寸 (H,W)= image.shape[:2]If widthIsNoneand heightIsNone:return image#高度算缩放比例If widthIsNone:n= height/Float (h) newsize= (Int (n*W), height)Else:n= width/Float (w) newsize= (width,Int (h*n))# Zoom Image NewImage= Cv2.resize (image, NewSize, interpolation=inter)Return newimageimageoriginal= 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 times times smaller newimage2 = Resizeimage (imageoriginal,Int (w/5),Int (h/5), Cv2. Inter_linear) cv2.imwrite (' newimage2.jpg ', newimage2)
C + + version (ImageResize.cpp)
#Include<iostream>#Include"OPENCV2/OPENCV.HPP"UsingNamespaceStdUsingnamespace CV;void Imageresize(Mat image, mat* DST,IntWidthIntHeightIntInter = 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() {Constchar* 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 the width of the image image.cols the height of the imageint w = image.cols;int h = image.rows;STD::cout<<"Image size:" <<w <<"*" <STD::Endl Imageresize (IMAGE,&DST,W *2, H *2);std::cout<< "new Image size:" <<dst.cols <<std::ENDL; vector<int> compression_params; Span class= "CO" >//jpeg, with the parameter cv_imwrite_jpeg_quality, the value is from 0 to 100, the smaller the value the more compressed Compression_ Params.push_back (cv_imwrite_jpeg_quality); Compression_params.push_back (100); //imshow ("Dstimage", DST); Imwrite (" dstimage.jpg ", dst,compression_params); return 0;}
Compile:
sudo g++ imageResize.cpp -o resize --cflags --libs opencv
[Turn]opencv3 image processing image scaling (Python and C + + implementation)