1.引入头文件
#include<opencv2/opencv.hpp> #include<iostream> // Namespace nullifies the use of cv::function(); using namespace std; using namespace cv;
2.读取并显示图像及其尺寸
上面的代码读取并显示图像及其尺寸。维度不仅包括二维矩阵的宽度和高度,还包括通道数(例如,RGB 图像有 3 个通道——红色、绿色和蓝色)。
Mat img = imread("test.jpg"); //Print the height and width of the image cout << "Width : " << img.size().width << endl; cout << "Height: " << img.size().height << endl; cout << "Channels: " << img.channels() << endl; // Display image imshow("Image", img); waitKey(0); destroyAllWindows();
3.开始裁剪
Mat crop = img(Range(80,280),Range(150,330)); // Slicing to crop the image // Display the cropped image imshow("Cropped Image", crop); waitKey(0); destroyAllWindows(); return 0;
4.使用裁剪将图像分成小块
OpenCV 中裁剪的一种实际应用是将图像分割成更小的块。使用循环从图像中裁剪出一个片段。首先从图像的形状中获取所需补丁的高度和宽度。
加载高度和宽度以指定需要裁剪较小补丁的范围。为此,请使用range()Python 中的函数。for现在,使用两个循环进行裁剪:
1一个用于宽度范围
2其他为高度范围
我们使用高度和宽度分别为 76 像素和 104 像素的补丁。内部和外部循环的步幅(我们在图像中移动的像素数)等于我们正在考虑的补丁的宽度和高度。
int M = 76; int N = 104; 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 MxN Mat tiles = image_copy(Range(y, imgheight), Range(x, imgwidth)); //save each patches into file directory imwrite("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 MxN Mat tiles = image_copy(Range(y, imgheight), Range(x, x+N)); //save each patches into file directory imwrite("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 MxN Mat tiles = image_copy(Range(y, y+M), Range(x, imgwidth)); //save each patches into file directory imwrite("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 MxN Mat tiles = image_copy(Range(y, y+M), Range(x, x+N)); //save each patches into file directory imwrite("saved_patches/tile" + a + '_' + b + ".jpg", tiles); rectangle(img, Point(x,y), Point(x1,y1), Scalar(0,255,0), 1); } }
5.显示图像
接下来,使用该imshow()函数显示图像补丁。imwrite()使用函数 将其保存到文件目录中。
imshow("Patched Image", img); imwrite("patched.jpg",img); waitKey(); destroyAllWindows();
6.终极代码
// Include Libraries #include<opencv2/opencv.hpp> #include<iostream> // Namespace nullifies the use of cv::function(); using namespace std; using namespace cv; int main() { // Read image Mat img = imread("test.jpg"); cout << "Width : " << img.size().width << endl; cout << "Height: " << img.size().height << endl; cout<<"Channels: :"<< img.channels() << endl; // Crop image Mat cropped_image = img(Range(80,280), Range(150,330)); //display image imshow(" Original Image", img); imshow("Cropped Image", cropped_image); //Save the cropped Image imwrite("Cropped Image.jpg", cropped_image); // 0 means loop infinitely waitKey(0); destroyAllWindows(); return 0; }