使用OpenCV調(diào)整圖像大小。要調(diào)整圖像的大小,可以根據(jù)每個軸(高度和寬度)進行縮放,考慮指定的縮放因素,或者只需設(shè)置所需的高度和寬度。
調(diào)整圖像大小時:
- 如果想在調(diào)整后的圖像中保持相同的寬高比,請務(wù)必記住圖像的原始寬高比(即寬度和高度)。
- 縮小圖像的大小需要重新采樣像素。
- 增加圖像的大小需要重建圖像。這意味著需要插值新像素。
各種插值技術(shù)發(fā)揮作用來完成這些操作。OpenCV中有幾種方法可供選擇,選擇通常取決于特定的應(yīng)用程序。
通過調(diào)整自定義高度和寬度的大小來使圖像越來越小。隨著您的進一步發(fā)展,我們將討論使用不同的比例因子和插值方法調(diào)整大小。
1.圖像尺寸信息
Python
# let's start with the Imports
import cv2
import numpy as np
# Read the image using imread function
image = cv2.imread('image.jpg')
cv2.imshow('Original Image', image)
# let's downscale the image using new width and height
down_width = 300
down_height = 200
down_points = (down_width, down_height)
resized_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR)
# let's upscale the image using new width and height
up_width = 600
up_height = 400
up_points = (up_width, up_height)
resized_up = cv2.resize(image, up_points, interpolation= cv2.INTER_LINEAR)
# Display images
cv2.imshow('Resized Down by defining height and width', resized_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining height and width', resized_up)
cv2.waitKey()
#press any key to close the windows
cv2.destroyAllWindows()
C++
// let's start with including libraries
#include<opencv2/opencv.hpp>
#include<iostream>
// Namespace to nullify use of cv::function(); syntax
using namespace std;
using namespace cv;
int main()
{
// Read the image using imread function
Mat image = imread("image.jpg");
imshow("Original Image", image);
// let's downscale the image using new width and height
int down_width = 300;
int down_height = 200;
Mat resized_down;
//resize down
resize(image, resized_down, Size(down_width, down_height), INTER_LINEAR);
// let's upscale the image using new width and height
int up_width = 600;
int up_height = 400;
Mat resized_up;
//resize up
resize(image, resized_up, Size(up_width, up_height), INTER_LINEAR);
// Display Images and press any key to continue
imshow("Resized Down by defining height and width", resized_down);
waitKey();
imshow("Resized Up image by defining height and width", resized_up);
waitKey();
destroyAllWindows();
return 0;
}
在開始調(diào)整圖像的大小之前,先了解其原始尺寸。要獲取圖像的大?。?/mark>
- 在Python中使用shape方法
- C++中的rows和cols參數(shù)
Python中的image.shape返回三個值:高度、寬度和通道數(shù)。
在C++中:
- image.rows:圖像的高度
- image.columns:圖像的寬度
也可以使用size()函數(shù)獲得上述結(jié)果。
- image.size().width 返回寬度
- image.size().height 返回高度
Python
# Get original height and width
h,w,c = image.shape
print("Original Height and Width:", h,"x", w)
C++
// Get height and width
cout << "Original Height and Width :" << image.rows << "x" << image.cols << endl;
這里需要注意的一件重要事情是,OpenCV以 h e i g h t ? w i d t h ? c h a n n e l s height*width*channels height?width?channels格式輸出圖像的形狀,而其他一些圖像處理庫則以寬度、高度的形式輸出。對此有合乎邏輯的看法。
當使用OpenCV讀取圖像時,它們表示為NumPy數(shù)組。一般來說,總是用 r o w s ? c o l u m n s rows*columns rows?columns(表示其高度的行和表示其寬度的列)來引用數(shù)組的形狀。因此,即使使用OpenCV讀取圖像以獲得其形狀,相同的NumPy數(shù)組規(guī)則也會發(fā)揮作用。你得到形狀的形狀是 h e i g h t ? w i d t h ? c h a n n e l s height*width*channels height?width?channels。
OpenCV resize()函數(shù)語法
OpenCV resize()函數(shù)語法需要兩個輸入?yún)?shù):
- 源圖像。
- 調(diào)整大小圖像的所需大小,d大小。
我們將在以下各節(jié)中討論各種輸入?yún)?shù)選項。
resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
- src:這是必需的輸入圖像,它可以是具有輸入圖像路徑的字符串(例如:“test_image.png”)。
- dsize:它是輸出圖像的理想尺寸,它可以是新的高度和寬度。
- fx:沿水平軸的縮放因子。
- fy:沿垂直軸的縮放因子。
- interpolation:它為我們提供了調(diào)整圖像大小的不同方法的選擇。
2.使用自定義寬度和高度調(diào)整圖像大小
在第一個示例中,讓我們通過指定一個新的寬度和高度來調(diào)整圖像的大小,這將縮小圖像的縮放。在以下代碼中:
- 我們將所需的寬度設(shè)置為300,所需的高度設(shè)置為200。
- 這兩個值組合成一個二維向量,這是resize()函數(shù)所要求的。
- 我們還指定了插值方法,恰好是默認值。
Python
# Set rows and columns
# lets downsize the image using new width and height
down_width = 300
down_height = 200
down_points = (down_width, down_height)
resized_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR)
C++
// Set rows and columns
// lets downsize the image using new width and height
int down_width = 300;
int down_height = 200;
Mat resize_down;
// resize down
resize(image, resize_down, Size(down_width, down_height), INTER_LINEAR);
接下來,我們創(chuàng)建另一個變量來增加圖像的大小。
Python
# Set rows and columns
up_width = 600
up_height = 400
up_points = (up_width, up_height)
# resize the image
resized_up = cv2.resize(image, up_points, interpolation = cv2.INTER_LINEAR)
C++
// Set rows and columns
int up_width = 600;
int up_height = 400;
Mat resized_up;
//resize up
resize(image, resized_up, Size(up_width, up_height), INTER_LINEAR);
在上面的Python代碼中,我們正在使用resize()函數(shù)定義新的寬度和高度來升級圖像。過程和步驟與之前的片段相似。
在C++代碼中:
- 我們定義了用于升級的寬度和高度的新整數(shù)。
- 給出輸出圖像的矩陣。
- 然后使用resize()函數(shù),與之前的代碼相同。
現(xiàn)在,讓我們使用OpenCV的imshow()函數(shù)顯示所有圖像。
Python
# Display images
cv2.imshow('Resized Down by defining height and width', resized_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining height and width', resized_up)
cv2.waitKey()
cv2.destroyAllWindows()
C++
// Display Images and press any key to continue
imshow("Resized Down by defining height and width", resized_down);
waitKey();
imshow("Resized Up image by defining height and width", resized_up);
waitKey();
destroyAllWindows();
3.使用縮放因子調(diào)整圖像的大小
現(xiàn)在我們用縮放因子調(diào)整圖像的大小。但在更進一步之前,你需要了解什么是縮放因素。
縮放因子通常是尺度縮放或乘以某些數(shù)量的數(shù)字,在圖像中尺寸是圖像的寬度和高度??s放因子有助于保持寬高比完好無損,并保持顯示質(zhì)量。因此,在您升級或縮小縮放圖像時,圖像不會顯得失真。
Python
# Scaling Up the image 1.2 times by specifying both scaling factors
scale_up_x = 1.2
scale_up_y = 1.2
# Scaling Down the image 0.6 times specifying a single scale factor.
scale_down = 0.6
scaled_f_down = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_LINEAR)
scaled_f_up = cv2.resize(image, None, fx= scale_up_x, fy= scale_up_y, interpolation= cv2.INTER_LINEAR)
C++
// Scaling Up the image 1.2 times by specifying both scaling factors
double scale_up_x = 1.2;
double scale_up_y = 1.2;
// Scaling Down the image 0.6 times specifying a single scale factor.
double scale_down = 0.6;
Mat scaled_f_up, scaled_f_down;
//resize
resize(image,scaled_f_down, Size(), scale_down, scale_down, INTER_LINEAR);
resize(image, scaled_f_up, Size(), scale_up_x, scale_up_y, INTER_LINEAR);
在上面的Python代碼中:
- 我們沿著水平和垂直軸定義了新的縮放因素。
- 定義縮放因子,就不需要對增加新的圖像寬度和高度。因此,值為None。
在上面的C++代碼中:
- 我們定義了新的縮放因子以及新圖像的矩陣。
- 由于我們不需要新的寬度和高度點,我們保持Size()為空,并使用resize()函數(shù)
現(xiàn)在,讓我們展示圖像,以便可視化和更好地理解。
Python
# Display images and press any key to check next image
cv2.imshow('Resized Down by defining scaling factor', scaled_f_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining scaling factor', scaled_f_up)
cv2.waitKey()
C++
// Display images and Press any key to continue check next image
imshow("Resized Down by defining scaling factor", scaled_f_down);
waitKey();
imshow("Resized Up by defining scaling factor", scaled_f_up);
waitKey();
左側(cè)圖像是縮小版本,右側(cè)是縮放版本。
4.使用不同的插值方法調(diào)整大小
不同的插值方法用于調(diào)整不同的尺寸大小。
- INTER_AREA:INTER_AREA使用像素區(qū)域關(guān)系進行重新采樣。這最適合縮小圖像的大?。s?。?。當用于放大圖像時,它使用INTER_NEAREST方法。
- INTER_CUBIC:這使用雙立方插值來調(diào)整圖像的大小。在調(diào)整新像素的大小和插值時,此方法作用于圖像的4×4相鄰像素。然后,需要16像素的平均權(quán)重來創(chuàng)建新的插值像素
- INTER_LINEAR:此方法與INTER_CUBIC插值有點相似。但與INTER_CUBIC不同,這使用2×2相鄰像素來獲得插值像素的加權(quán)平均值。
- INTER_NEAREST:INTER_NEAREST方法使用最近的鄰居概念進行插值。這是最簡單的方法之一,僅使用圖像中的一個相鄰像素進行插值。
如果您不完全理解插值方法,請不要擔(dān)心。我們將在一個單獨的例子中解釋它們。
Python
# Scaling Down the image 0.6 times using different Interpolation Method
res_inter_nearest = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_NEAREST)
res_inter_linear = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_LINEAR)
res_inter_area = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_AREA)
C++
# Scaling Down the image 0.6 using different Interpolation Method
Mat res_inter_linear, res_inter_nearest, res_inter_area;
resize(image, res_inter_linear, Size(), scale_down, scale_down, INTER_LINEAR);
resize(image, res_inter_nearest, Size(), scale_down, scale_down, INTER_NEAREST);
resize(image, res_inter_area, Size(), scale_down, scale_down, INTER_AREA);
在上面的Python片段中,我們正在使用不同的插值方法調(diào)整圖像的大小。同樣,在C++片段中,我們首先為輸出圖像定義新矩陣,然后使用不同的插值方法調(diào)整它們的大小?,F(xiàn)在讓我們顯示圖像。
Python
# Concatenate images in horizontal axis for comparison
vertical= np.concatenate((res_inter_nearest, res_inter_linear, res_inter_area), axis = 0)
# Display the image Press any key to continue
cv2.imshow('Inter Nearest :: Inter Linear :: Inter Area', vertical)
C++文章來源:http://www.zghlxwxcb.cn/news/detail-469377.html
Mat a,b,c;
vconcat(res_inter_linear, res_inter_nearest, a);
vconcat(res_inter_area, res_inter_area, b);
vconcat(a, b, c);
// Display the image Press any key to continue
imshow("Inter Linear :: Inter Nearest :: Inter Area :: Inter Area", c);
INTER_LINEAR在左邊,INTER_NEAREST在中間,INTER_AREA在右邊文章來源地址http://www.zghlxwxcb.cn/news/detail-469377.html
到了這里,關(guān)于OpenCV入門(C++/Python)- 使用OpenCV調(diào)整尺寸大?。ㄈ┑奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!