OpenCV入门(C++/Python)-使用OpenCV裁剪图像(四)
裁剪是為了從圖像中刪除所有不需要的物體或區域。甚至突出顯示圖像的特定功能。
使用OpenCV裁剪沒有特定的功能,NumPy數組切片是工作。讀取的每個圖像都存儲在2D數組中(對于每個顏色通道)。只需指定要裁剪區域的高度和寬度(以像素為單位),就可以完成
使用OpenCV裁剪圖像
- 1.使用OpenCV裁剪
- 2.使用裁剪功能對圖像進行劃分
1.使用OpenCV裁剪
以下代碼片段展示了如何使用Python和C++裁剪圖像。在例子的進一步,您將詳細了解這些。
Python
C++
// Include Libraries #include<opencv2/opencv.hpp> #include<iostream>// Namespace nullifies the use of cv::function(); using namespace std; using namespace cv;int main() {// Read imageMat img = imread("test.jpg");cout << "Width : " << img.size().width << endl;cout << "Height: " << img.size().height << endl;cout<<"Channels: :"<< img.channels() << endl;// Crop imageMat cropped_image = img(Range(400,1200), Range(350,700));//display imageimshow(" Original Image", img);imshow("Cropped Image", cropped_image);//Save the cropped Imageimwrite("Cropped Image.jpg", cropped_image);// 0 means loop infinitelywaitKey(0);destroyAllWindows();return 0; }上面的代碼讀取并顯示圖像及其尺寸。尺寸不僅包括二維矩陣的寬度和高度,還包括通道的數量(例如,RGB圖像有3個通道——紅色、綠色和藍色)。
讓我們嘗試裁剪圖像中包含美女的部分。
Python
cropped_image = img[400:1200, 350:700] # Slicing to crop the image# Display the cropped image cv2.imshow("cropped", cropped_image) cv2.waitKey(0) cv2.destroyAllWindows()C++
Mat crop = img(Range(400,1200),Range(350,700)); // Slicing to crop the image// Display the cropped image imshow("Cropped Image", crop);waitKey(0); destroyAllWindows(); return 0;
在Python中,您可以使用與NumPy數組切片相同的方法裁剪圖像。要切片數組,您需要指定第一維和第二維的開始和結束索引。
- 第一個維度總是行數或圖像的高度。
- 第二個維度是列數或圖像的寬度。
如何剪切圖像的NumPy數組?查看此示例中的語法:
cropped = img[start_row:end_row, start_col:end_col]在C++中,我們使用Range()函數裁剪圖像。
- Python同理一樣,它也應用切片。
- 在這里,圖像也按照上述相同的約定作為二維矩陣讀取。
以下是裁剪圖像的C++語法:
img(Range(start_row, end_row), Range(start_col, end_col))2.使用裁剪功能對圖像進行劃分
在OpenCV中裁剪的一個實際應用可以是將圖像劃分為大小相同圖像塊。使用循環從圖像中裁剪片段。首先從圖像的形狀中獲取所需圖像塊的高度和寬度
Python
img = cv2.imread("test_cropped.jpg") image_copy = img.copy() imgheight=img.shape[0] imgwidth=img.shape[1]C++
Mat img = imread("test_cropped.jpg"); Mat image_copy = img.clone(); int imgheight = img.rows; int imgwidth = img.cols;加載高度和寬度,以指定需要裁剪較小圖像塊的范圍。為此,使用Python中的range()函數。現在,使用兩個循環裁剪:
- 寬度范圍
- 高度范圍
已知原圖像瘩高度寬度為(1350,1080),我們使用的圖像塊的高度和寬度分別為(270,216)。內外循環的步幅(我們在圖像中移動的像素數)也就是劃分下來,有25個圖像塊。(拼圖一樣)
Python
M = 216 N = 270 x1 = 0 y1 = 0for y in range(0, imgheight, M):for x in range(0, imgwidth, N):if (imgheight - y) < M or (imgwidth - x) < N:breaky1 = y + Mx1 = x + N# check whether the patch width or height exceeds the image width or heightif x1 >= imgwidth and y1 >= imgheight:x1 = imgwidth - 1y1 = imgheight - 1# Crop into patches of size MxNtiles = image_copy[y:y + M, x:x + N]# Save each patch into file directorycv2.imwrite(str(x) + '_' + str(y) + '.jpg', tiles)cv2.rectangle(img, (x, y), (x1, y1), (0, 255, 0), 1)elif y1 >= imgheight: # when patch height exceeds the image heighty1 = imgheight - 1# Crop into patches of size MxNtiles = image_copy[y:y + M, x:x + N]# Save each patch into file directorycv2.imwrite(str(x) + '_' + str(y) + '.jpg', tiles)cv2.rectangle(img, (x, y), (x1, y1), (0, 255, 0), 1)elif x1 >= imgwidth: # when patch width exceeds the image widthx1 = imgwidth - 1# Crop into patches of size MxNtiles = image_copy[y:y + M, x:x + N]# Save each patch into file directorycv2.imwrite(str(x) + '_' + str(y) + '.jpg', tiles)cv2.rectangle(img, (x, y), (x1, y1), (0, 255, 0), 1)else:# Crop into patches of size MxNtiles = image_copy[y:y + M, x:x + N]# Save each patch into file directorycv2.imwrite(str(x) + '_' + str(y) + '.jpg', tiles)cv2.rectangle(img, (x, y), (x1, y1), (0, 255, 0), 1)C++
int M = 216; int N = 270;int x1 = 0; int y1 = 0; for (int y = 0; y<imgheight; y=y+M) {for (int x = 0; x<imgwidth; x=x+N){if ((imgheight - y) < M || (imgwidth - x) < N){break;}y1 = y + M;x1 = x + N;string a = to_string(x);string b = to_string(y);if (x1 >= imgwidth && y1 >= imgheight){x = imgwidth - 1;y = imgheight - 1;x1 = imgwidth - 1;y1 = imgheight - 1;// crop the patches of size MxNMat tiles = image_copy(Range(y, imgheight), Range(x, imgwidth));//save each patches into file directoryimwrite("saved_patches/tile" + a + '_' + b + ".jpg", tiles); rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); }else if (y1 >= imgheight){y = imgheight - 1;y1 = imgheight - 1;// crop the patches of size MxNMat tiles = image_copy(Range(y, imgheight), Range(x, x+N));//save each patches into file directoryimwrite("saved_patches/tile" + a + '_' + b + ".jpg", tiles); rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); }else if (x1 >= imgwidth){x = imgwidth - 1; x1 = imgwidth - 1;// crop the patches of size MxNMat tiles = image_copy(Range(y, y+M), Range(x, imgwidth));//save each patches into file directoryimwrite("saved_patches/tile" + a + '_' + b + ".jpg", tiles); rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); }else{// crop the patches of size MxNMat tiles = image_copy(Range(y, y+M), Range(x, x+N));//save each patches into file directoryimwrite("saved_patches/tile" + a + '_' + b + ".jpg", tiles); rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); }} }接下來,使用imshow()函數顯示圖像塊拼圖。使用imwrite()函數將其保存到文件目錄中。
Python
#Save full image into file directory cv2.imshow("Patched Image",img) cv2.imwrite("patched.jpg",img)cv2.waitKey() cv2.destroyAllWindows()C++
imshow("Patched Image", img); imwrite("patched.jpg",img); waitKey(); destroyAllWindows();Python
C++
總結
以上是生活随笔為你收集整理的OpenCV入门(C++/Python)-使用OpenCV裁剪图像(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hbase和es在搜索场景的应用
- 下一篇: 读书笔记《Effective C++》条