如何使用Opencv裁剪圖像-常州機器視覺培訓(xùn),常州上位機培訓(xùn)
首先,為什么我們需要裁剪?進行裁剪以從圖像中刪除所有不需要的對象或區(qū)域。甚至突出圖像的特定特征。
以下代碼片段顯示了如何使用 Python 和 C++ 裁剪圖像。在這篇文章中,您將詳細了解這些內(nèi)容。
Python
5 |
img = cv2.imread( 'test.jpg' ) |
6 |
print (img.shape) # Print image shape |
7 |
cv2.imshow( "original" , img) |
10 |
cropped_image = img[ 80 : 280 , 150 : 330 ] |
12 |
# Display cropped image |
13 |
cv2.imshow( "cropped" , cropped_image) |
15 |
# Save the cropped image |
16 |
cv2.imwrite( "Cropped Image.jpg" , cropped_image) |
19 |
cv2.destroyAllWindows() |
C++
2 |
#include<opencv2/opencv.hpp> |
5 |
// Namespace nullifies the use of cv::function(); |
12 |
Mat img = imread( "test.jpg" ); |
13 |
cout << "Width : " << img.size().width << endl; |
14 |
cout << "Height: " << img.size().height << endl; |
15 |
cout<< "Channels: :" << img.channels() << endl; |
17 |
Mat cropped_image = img(Range(80,280), Range(150,330)); |
20 |
imshow( " Original Image" , img); |
21 |
imshow( "Cropped Image" , cropped_image); |
23 |
//Save the cropped Image |
24 |
imwrite( "Cropped Image.jpg" , cropped_image); |
26 |
// 0 means loop infinitely |
使用 OpenCV 進行裁剪
在這篇文章中將用于裁剪的圖像。
Python:
1 |
img = cv2.imread( 'test.png' ) |
3 |
# Prints Dimensions of the image |
7 |
cv2.imshow( "original" , img) |
9 |
cv2.destroyAllWindows() |
C++
1 |
Mat img = imread( "test.jpg" ); |
3 |
//Print the height and width of the image |
4 |
cout << "Width : " << img.size().width << endl; |
5 |
cout << "Height: " << img.size().height << endl; |
6 |
cout << "Channels: " << img.channels() << endl; |
上面的代碼讀取并顯示圖像及其尺寸。維度不僅包括二維矩陣的寬度和高度,還包括通道數(shù)(例如,RGB 圖像有 3 個通道——紅色、綠色和藍色)。
讓我們嘗試裁剪包含花朵的圖像部分。
Python
1 |
cropped_image = img[ 80 : 280 , 150 : 330 ] # Slicing to crop the image |
3 |
# Display the cropped image |
4 |
cv2.imshow( "cropped" , cropped_image) |
6 |
cv2.destroyAllWindows() |
C++
1 |
Mat crop = img(Range(80,280),Range(150,330)); // Slicing to crop the image |
3 |
// Display the cropped image |
4 |
imshow( "Cropped Image" , crop); |
在 Python 中,您使用與 NumPy 數(shù)組切片相同的方法裁剪圖像。要對數(shù)組進行切片,您需要指定第一維和第二維的開始和結(jié)束索引。
二維數(shù)組的第一個維度表示數(shù)組的行(其中每一行表示圖像的 y 坐標),這符合慣例。如何對 NumPy 數(shù)組進行切片?查看此示例中的語法:
cropped = img[start_row:end_row, start_col:end_col]
在 C++ 中,我們使用該Range()
函數(shù)來裁剪圖像。
以下是裁剪圖像的 C++ 語法:
img(Range(start_row, end_row), Range(start_col, end_col))
使用裁剪將圖像分成小塊
OpenCV 中裁剪的一種實際應(yīng)用是將圖像分割成更小的塊。使用循環(huán)從圖像中裁剪出一個片段。首先從圖像的形狀中獲取所需補丁的高度和寬度。
Python
1 |
img = cv2.imread( "test_cropped.jpg" ) |
2 |
image_copy = img.copy() |
C++
1 |
Mat img = imread( "test_cropped.jpg" ); |
2 |
Mat image_copy = img.clone(); |
3 |
int imgheight = img.rows; |
4 |
int imgwidth = img.cols; |
加載高度和寬度以指定需要裁剪較小補丁的范圍。為此,請使用range()
Python 中的函數(shù)。for
現(xiàn)在,使用兩個循環(huán)進行裁剪:
-
一個用于寬度范圍
-
其他為高度范圍
我們使用高度和寬度分別為 76 像素和 104 像素的補丁。內(nèi)部和外部循環(huán)的步幅(我們在圖像中移動的像素數(shù))等于我們正在考慮的補丁的寬度和高度。
Python
6 |
for y in range ( 0 , imgheight, M): |
7 |
for x in range ( 0 , imgwidth, N): |
8 |
if (imgheight - y) < M or (imgwidth - x) < N: |
14 |
# check whether the patch width or height exceeds the image width or height |
15 |
if x1 > = imgwidth and y1 > = imgheight: |
18 |
#Crop into patches of size MxN |
19 |
tiles = image_copy[y:y + M, x:x + N] |
20 |
#Save each patch into file directory |
21 |
cv2.imwrite( 'saved_patches/' + 'tile' + str (x) + '_' + str (y) + '.jpg' , tiles) |
22 |
cv2.rectangle(img, (x, y), (x1, y1), ( 0 , 255 , 0 ), 1 ) |
23 |
elif y1 > = imgheight: # when patch height exceeds the image height |
25 |
#Crop into patches of size MxN |
26 |
tiles = image_copy[y:y + M, x:x + N] |
27 |
#Save each patch into file directory |
28 |
cv2.imwrite( 'saved_patches/' + 'tile' + str (x) + '_' + str (y) + '.jpg' , tiles) |
29 |
cv2.rectangle(img, (x, y), (x1, y1), ( 0 , 255 , 0 ), 1 ) |
30 |
elif x1 > = imgwidth: # when patch width exceeds the image width |
32 |
#Crop into patches of size MxN |
33 |
tiles = image_copy[y:y + M, x:x + N] |
34 |
#Save each patch into file directory |
35 |
cv2.imwrite( 'saved_patches/' + 'tile' + str (x) + '_' + str (y) + '.jpg' , tiles) |
36 |
cv2.rectangle(img, (x, y), (x1, y1), ( 0 , 255 , 0 ), 1 ) |
38 |
#Crop into patches of size MxN |
39 |
tiles = image_copy[y:y + M, x:x + N] |
40 |
#Save each patch into file directory |
41 |
cv2.imwrite( 'saved_patches/' + 'tile' + str (x) + '_' + str (y) + '.jpg' , tiles) |
42 |
cv2.rectangle(img, (x, y), (x1, y1), ( 0 , 255 , 0 ), 1 ) |
C++
6 |
for ( int y = 0; y<imgheight; y=y+M) |
8 |
for ( int x = 0; x<imgwidth; x=x+N) |
10 |
if ((imgheight - y) < M || (imgwidth - x) < N) |
16 |
string a = to_string(x); |
17 |
string b = to_string(y); |
19 |
if (x1 >= imgwidth && y1 >= imgheight) |
26 |
// crop the patches of size MxN |
27 |
Mat tiles = image_copy(Range(y, imgheight), Range(x, imgwidth)); |
28 |
//save each patches into file directory |
29 |
imwrite( "saved_patches/tile" + a + '_' + b + ".jpg" , tiles); |
30 |
rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); |
32 |
else if (y1 >= imgheight) |
37 |
// crop the patches of size MxN |
38 |
Mat tiles = image_copy(Range(y, imgheight), Range(x, x+N)); |
39 |
//save each patches into file directory |
40 |
imwrite( "saved_patches/tile" + a + '_' + b + ".jpg" , tiles); |
41 |
rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); |
43 |
else if (x1 >= imgwidth) |
48 |
// crop the patches of size MxN |
49 |
Mat tiles = image_copy(Range(y, y+M), Range(x, imgwidth)); |
50 |
//save each patches into file directory |
51 |
imwrite( "saved_patches/tile" + a + '_' + b + ".jpg" , tiles); |
52 |
rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); |
56 |
// crop the patches of size MxN |
57 |
Mat tiles = image_copy(Range(y, y+M), Range(x, x+N)); |
58 |
//save each patches into file directory |
59 |
imwrite( "saved_patches/tile" + a + '_' + b + ".jpg" , tiles); |
60 |
rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); |
接下來,使用該imshow()
函數(shù)顯示圖像補丁。imwrite()
使用函數(shù) 將其保存到文件目錄中。
Python
1 |
#Save full image into file directory |
2 |
cv2.imshow( "Patched Image" ,img) |
3 |
cv2.imwrite( "patched.jpg" ,img) |
6 |
cv2.destroyAllWindows() |
C++
1 |
imshow( "Patched Image" , img); |
2 |
imwrite( "patched.jpg" ,img); |
上面覆蓋有矩形補丁的最終圖像將如下所示:
下圖顯示了保存到磁盤的單獨圖像補丁。
本文網(wǎng)址:
下一篇:沒有資料
相關(guān)信息: