jashliao 用 VC++ 實現 fanfuhan OpenCV 教學014 ~ opencv-014-使用resize進行圖像插值[Image Interpolation ~ resize()] 圖像放大/縮小(Zoom In/Zoom Out)
jashliao 用 VC++ 實現 fanfuhan OpenCV 教學014 ~ opencv-014-使用resize進行圖像插值[Image Interpolation ~ resize()] 圖像放大/縮小(Zoom In/Zoom Out)
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/03/27/opencv-014/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
https://github.com/jash-git/jashliao-implements-FANFUHAN-OPENCV-with-VC
★前言:

★主題:
OPENCV提供使用者可以等比例改變圖像大小的函數resize(),其函數原型與參數介紹如下所示:
resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )
interpolation 說明
INTER_NEAREST 最近鄰插值
INTER_LINEAR 雙線性插值(預設)
INTER_AREA 使用像素區域關係進行重採樣。它可能是圖像抽取的首選方法,因為它會產生無雲紋理(波紋)的結果。 但是當圖像縮放時,它類似於INTER_NEAREST方法。
INTER_CUBIC 4×4像素鄰域的雙三次插值
INTER_LANCZOS4 8×8像素鄰域的Lanczos插值
★C++
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
/*
* 图像插值
*/
int main() {
Mat src = imread("../images/test.png");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);
int h = src.rows;
int w = src.cols;
float fx = 0.0, fy = 0.0;
Mat dst = Mat::zeros(src.size(), src.type());
Size S(w * 2, h * 2);
resize(src, dst, S, fx, fy, INTER_NEAREST);
imshow("INTER_NEAREST", dst);
resize(src, dst, S, fx, fy, INTER_LINEAR);
imshow("INTER_LINEAR", dst);
resize(src, dst, S, fx, fy, INTER_CUBIC);
imshow("INTER_CUBIC", dst);
resize(src, dst, S, fx, fy, INTER_LANCZOS4);
imshow("INTER_LANCZOS4", dst);
waitKey(0);
return 0;
}
★Python
import cv2 as cv
src = cv.imread("D:/vcprojects/images/test.png")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
h, w = src.shape[:2]
print(h, w)
dst = cv.resize(src, (w*2, h*2), fx=0.75, fy=0.75, interpolation=cv.INTER_NEAREST)
cv.imshow("INTER_NEAREST", dst)
dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_LINEAR)
cv.imshow("INTER_LINEAR", dst)
dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_CUBIC)
cv.imshow("INTER_CUBIC", dst)
dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_LANCZOS4)
cv.imshow("INTER_LANCZOS4", dst)
cv.warpAffine()
cv.waitKey(0)
cv.destroyAllWindows()
★結果圖:

★延伸說明/重點回顧:
經過實際測試感覺,INTER_LINEAR,看起來最舒服