如何使用Opencv裁剪图像-常州机器视觉培训,常州上位机培训
首先,为什么我们需要裁剪?进行裁剪以从图像中删除所有不需要的对象或区域。甚至突出图像的特定特征。
以下代码片段显示了如何使用 Python 和 C++ 裁剪图像。在这篇文章中,您将详细了解这些内容。
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; |
上面的代码读取并显示图像及其尺寸。维度不仅包括二维矩阵的宽度和高度,还包括通道数(例如,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 数组切片相同的方法裁剪图像。要对数组进行切片,您需要指定第一维和第二维的开始和结束索引。
-
第一个维度始终是图像的行数或高度。
-
第二个维度是图像的列数或宽度。
二维数组的第一个维度表示数组的行(其中每一行表示图像的 y 坐标),这符合惯例。如何对 NumPy 数组进行切片?查看此示例中的语法:
cropped = img[start_row:end_row, start_col:end_col]
在 C++ 中,我们使用该Range()
函数来裁剪图像。
以下是裁剪图像的 C++ 语法:
img(Range(start_row, end_row), Range(start_col, end_col))
使用裁剪将图像分成小块
OpenCV 中裁剪的一种实际应用是将图像分割成更小的块。使用循环从图像中裁剪出一个片段。首先从图像的形状中获取所需补丁的高度和宽度。
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 中的函数。for
现在,使用两个循环进行裁剪:
-
一个用于宽度范围
-
其他为高度范围
我们使用高度和宽度分别为 76 像素和 104 像素的补丁。内部和外部循环的步幅(我们在图像中移动的像素数)等于我们正在考虑的补丁的宽度和高度。
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()
函数显示图像补丁。imwrite()
使用函数 将其保存到文件目录中。
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); |
上面覆盖有矩形补丁的最终图像将如下所示:
下图显示了保存到磁盘的单独图像补丁。
本文网址:
下一篇:没有资料
相关信息: