色婷婷AV无码久久精品,久久天天躁狠狠躁夜夜97,羞羞麻豆国产精品1区2区3区,啪影院免费线观看视频,思思久久er99精品亚洲

常州機器視覺培訓(xùn)

常州上位機軟件開發(fā)

常州工業(yè)機器人編程設(shè)計培訓(xùn)

常州PLC培訓(xùn)

常州PLC

常州PLC編程培訓(xùn)

常州電工培訓(xùn)

常州和訊plc培訓(xùn)中心歡迎您!
當前位置:網(wǎng)站首頁 > 新聞中心 新聞中心
如何使用Opencv裁剪圖像-常州機器視覺培訓(xùn),常州上位機培訓(xùn)
日期:2024-4-12 16:39:02人氣:  標簽:常州機器視覺培訓(xùn) 常州上位機培訓(xùn)

首先,為什么我們需要裁剪?進行裁剪以從圖像中刪除所有不需要的對象或區(qū)域。甚至突出圖像的特定特征。



以下代碼片段顯示了如何使用 Python 和 C++ 裁剪圖像。在這篇文章中,您將詳細了解這些內(nèi)容。

Python

1 # Import packages
2 import cv2
3 import numpy as np
4
5 img = cv2.imread('test.jpg')
6 print(img.shape) # Print image shape
7 cv2.imshow("original", img)
8
9 # Cropping an image
10 cropped_image = img[80:280150:330]
11
12 # Display cropped image
13 cv2.imshow("cropped", cropped_image)
14
15 # Save the cropped image
16 cv2.imwrite("Cropped Image.jpg", cropped_image)
17
18 cv2.waitKey(0)
19 cv2.destroyAllWindows()

C++

1 // Include Libraries
2 #include<opencv2/opencv.hpp>
3 #include<iostream>
4
5 // Namespace nullifies the use of cv::function();
6 using namespace std;
7 using namespace cv;
8
9 int main()
10 {
11   // Read image
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;
16   // Crop image
17   Mat cropped_image = img(Range(80,280), Range(150,330));
18
19   //display image
20   imshow(" Original Image", img);
21   imshow("Cropped Image", cropped_image);
22
23   //Save the cropped Image
24   imwrite("Cropped Image.jpg", cropped_image);
25
26   // 0 means loop infinitely
27   waitKey(0);
28   destroyAllWindows();
29   return 0;
30 }

使用 OpenCV 進行裁剪

image.png

在這篇文章中將用于裁剪的圖像。


Python:

1 img=cv2.imread('test.png')
2
3 # Prints Dimensions of the image
4 print(img.shape)
5
6 # Display the image
7 cv2.imshow("original", img)
8 cv2.waitKey(0)
9 cv2.destroyAllWindows()

C++

1 Mat img = imread("test.jpg");
2
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;
7
8 // Display image
9 imshow("Image", img);
10 waitKey(0);
11 destroyAllWindows();

上面的代碼讀取并顯示圖像及其尺寸。維度不僅包括二維矩陣的寬度和高度,還包括通道數(shù)(例如,RGB 圖像有 3 個通道——紅色、綠色和藍色)。

讓我們嘗試裁剪包含花朵的圖像部分。

Python

1 cropped_image = img[80:280150:330# Slicing to crop the image
2
3 # Display the cropped image
4 cv2.imshow("cropped", cropped_image)
5 cv2.waitKey(0)
6 cv2.destroyAllWindows()

C++

1 Mat crop = img(Range(80,280),Range(150,330)); // Slicing to crop the image
2
3 // Display the cropped image
4 imshow("Cropped Image", crop);
5
6 waitKey(0);
7 destroyAllWindows();
8 return 0;


image.png


在 Python 中,您使用與 NumPy 數(shù)組切片相同的方法裁剪圖像。要對數(shù)組進行切片,您需要指定第一維和第二維的開始和結(jié)束索引。 

  • 第一個維度始終是圖像的行數(shù)或高度。

  • 第二個維度是圖像的列數(shù)或?qū)挾取?nbsp;

二維數(shù)組的第一個維度表示數(shù)組的行(其中每一行表示圖像的 y 坐標),這符合慣例。如何對 NumPy 數(shù)組進行切片?查看此示例中的語法:

cropped = img[start_row:end_row, start_col:end_col]

在 C++ 中,我們使用該Range()函數(shù)來裁剪圖像。 

  • 與 Python 一樣,它也適用于切片。 

  • 在這里,圖像也被讀取為 2D 矩陣,遵循上述相同的約定。 

以下是裁剪圖像的 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()
3 imgheight=img.shape[0]
4 imgwidth=img.shape[1]

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)進行裁剪:

  1. 一個用于寬度范圍

  2. 其他為高度范圍 

我們使用高度和寬度分別為 76 像素和 104 像素的補丁。內(nèi)部和外部循環(huán)的步幅(我們在圖像中移動的像素數(shù))等于我們正在考慮的補丁的寬度和高度。

Python

1 = 76
2 = 104
3 x1 = 0
4 y1 = 0
5
6 for in range(0, imgheight, M):
7     for in range(0, imgwidth, N):
8         if (imgheight - y) < M or (imgwidth - x) < N:
9             break
10
11         y1 = + M
12         x1 = + N
13
14         # check whether the patch width or height exceeds the image width or height
15         if x1 >= imgwidth and y1 >= imgheight:
16             x1 = imgwidth - 1
17             y1 = imgheight - 1
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), (02550), 1)
23         elif y1 >= imgheight: # when patch height exceeds the image height
24             y1 = imgheight - 1
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), (02550), 1)
30         elif x1 >= imgwidth: # when patch width exceeds the image width
31             x1 = imgwidth - 1
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), (02550), 1)
37         else:
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), (02550), 1)

C++

1 int M = 76;
2 int N = 104;
3
4 int x1 = 0;
5 int y1 = 0;
6 for (int y = 0; y<imgheight; y=y+M)
7 {
8     for (int x = 0; x<imgwidth; x=x+N)
9     {
10         if ((imgheight - y) < M || (imgwidth - x) < N)
11         {
12             break;
13         }
14         y1 = y + M;
15         x1 = x + N;
16         string a = to_string(x);
17         string b = to_string(y);
18
19         if (x1 >= imgwidth && y1 >= imgheight)
20         {
21             x = imgwidth - 1;
22             y = imgheight - 1;
23             x1 = imgwidth - 1;
24             y1 = imgheight - 1;
25
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);   
31         }
32         else if (y1 >= imgheight)
33         {
34             y = imgheight - 1;
35             y1 = imgheight - 1;
36
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);   
42         }
43         else if (x1 >= imgwidth)
44         {
45             x = imgwidth - 1;  
46             x1 = imgwidth - 1;
47
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);   
53         }
54         else
55         {
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);   
61         }
62     }
63 }

接下來,使用該imshow()函數(shù)顯示圖像補丁。imwrite()使用函數(shù) 將其保存到文件目錄中。

Python

1 #Save full image into file directory
2 cv2.imshow("Patched Image",img)
3 cv2.imwrite("patched.jpg",img)
4
5 cv2.waitKey()
6 cv2.destroyAllWindows()

C++

1 imshow("Patched Image", img);
2 imwrite("patched.jpg",img);
3 waitKey();
4 destroyAllWindows();



上面覆蓋有矩形補丁的最終圖像將如下所示: 

image.png


下圖顯示了保存到磁盤的單獨圖像補丁。

image.png

本文網(wǎng)址:
下一篇:沒有資料

相關(guān)信息:
版權(quán)所有 CopyRight 2006-2017 江蘇和訊自動化設(shè)備有限公司 常州自動化培訓(xùn)中心 電話:0519-85602926 地址:常州市新北區(qū)府琛商務(wù)廣場2號樓1409室
蘇ICP備14016686號-2 技術(shù)支持:常州山水網(wǎng)絡(luò)
本站關(guān)鍵詞:常州PLC培訓(xùn) 常州PLC編程培訓(xùn) 常州PLC編程 常州PLC培訓(xùn)班 網(wǎng)站地圖 網(wǎng)站標簽
在線與我們?nèi)〉寐?lián)系
色婷婷AV无码久久精品,久久天天躁狠狠躁夜夜97,羞羞麻豆国产精品1区2区3区,啪影院免费线观看视频,思思久久er99精品亚洲