C++切图并保存固定名称

/*#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <io.h>
#include <string.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void getFiles(string path, vector<string>& files)
{
	//文件句柄
	long   hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			//如果是目录,迭代之
			//如果不是,加入列表
			if ((fileinfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\").append(fileinfo.name), files);
			}
			else
			{
				files.push_back(p.assign(path).append("\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}
void main()
{
	char * filePath = "D:\img";
	vector<string> files;

	////获取该路径下的所有文件
	getFiles(filePath, files);

	char str[30];
	//Mat img;
	int size = files.size();
	//int k = 0;
	
	for (int i = 0; i < size; i++)
	{
		//string Img_Name = "D:\proc_img" + to_string(k) + ".jpg";
		//cout << files[i].c_str() << endl;
		cout << files[i].c_str() << endl;
		//sprintf_s(fileName1, files[i].c_str()); //保存图片的路径
		//img = imread(files[i], 1);
		//imwrite(Img_Name, img);
		//k++;
		system("pause");
	}
}
*/

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/features2d.hpp>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;

//void alignImages(Mat &im1, Mat &im2, Mat &finalMatches, Mat &im1Reg, Mat &h);
int main()
{
	std::string pattern_jpg = "D:/左上原图/*.jpg";

	std::vector<cv::String> image_files;
	cv::glob(pattern_jpg, image_files);//glob可以将一个文件夹内所有图片装到一个文件vec里
	if (image_files.size() == 0)
		{
			std::cout << "No image files[jpg]" << std::endl;
			return 0;
		}
	int k = 500;
	for (unsigned int frame = 0; frame < image_files.size(); ++frame)
		{//image_file.size()代表文件中总共的图片个数

			//std::string Img_Name1 = "D:\EB_1\" + to_string(k) + ".jpg";
			std::string Img_Name2 = "D:\EB_2\" + to_string(k) + ".jpg";
			//std::string Img_Name3 = "D:\EB_3\" + to_string(k) + ".jpg";
			Mat image = cv::imread(image_files[frame]);
			
			//Rect area1(0, 0, 1270, 1500);
			Rect area2(1050, 0, 2050, 1300);
			//Rect area3(1300, 1200, 2600, 1200);

			//Mat img_region1 = image(area1);
			Mat img_region2 = image(area2);
			//Mat img_region2 = image(area2);

			//string refFilename("D:/proc_img/refer.jpg");
			//cout << "Reading reference image : " << refFilename << endl;

			//cout << image_files.size() << endl;
			//cv::imwrite(Img_Name1, img_region1);
			cv::imwrite(Img_Name2, img_region2);
			//cv::imwrite(Img_Name2, img_region2);
			cout << k << endl;
			++k;
			//imshow("1", image);
			waitKey(300);
		}
	return 0;
}

/**
*
* @param im1 对齐图像
* @param im2 模板图像
* @finalMatches 匹配图像
* @param im1Reg 输出图像
* @param h
*/
void alignImages(Mat &im1, Mat &im2, Mat &finalMatches, Mat &im1Reg, Mat &h)
{
	// Convert images to grayscale
	Mat im1Gray, im2Gray;
	cvtColor(im1, im1Gray, COLOR_BGR2GRAY);
	cvtColor(im2, im2Gray, COLOR_BGR2GRAY);

	// Variables to store keypoints and descriptors
	vector<KeyPoint> keypoints1, keypoints2;
	Mat descriptors1, descriptors2;

	Ptr<ORB> orb = ORB::create(1000);
	orb->setFastThreshold(20);

	orb->detectAndCompute(im1Gray, Mat(), keypoints1, descriptors1);//计算角点和描述子
	orb->detectAndCompute(im2Gray, Mat(), keypoints2, descriptors2);

	// Match features. 
	vector<DMatch> matches, matchesGMS;

	Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
	matcher->match(descriptors1, descriptors2, matches, Mat());
	cout << "matches: " << matches.size() << endl;

	matchGMS(im1.size(), im2.size(), keypoints1, keypoints2, matches, matchesGMS);//GMS匹配
	cout << "matchesGMS: " << matchesGMS.size() << endl;

	drawMatches(im1, keypoints1, im2, keypoints2, matchesGMS, finalMatches);


	vector<Point2f> points1, points2;
	for (size_t i = 0; i < matchesGMS.size(); i++)
	{
		//queryIdx是对齐图像的描述子和特征点的下标。
		points1.push_back(keypoints1[matchesGMS[i].queryIdx].pt);
		//queryIdx是是样本图像的描述子和特征点的下标。
		points2.push_back(keypoints2[matchesGMS[i].trainIdx].pt);
	}

	// Find homography 
	h = findHomography(points1, points2, RANSAC);

	// Use homography to warp image 
	warpPerspective(im1, im1Reg, h, im2.size());
}

/*int main()
{
	// Read image to be aligned 
	string imFilename("D:/proc_img/1.jpg");
	cout << "Reading image to align : " << imFilename << endl;
	Mat im = imread(imFilename);

	// Read reference image   
	string refFilename("D:/proc_img/refer.jpg");
	cout << "Reading reference image : " << refFilename << endl;
	Mat imReference = imread(refFilename);

	// Registered image will be resotred in imReg.
	// The estimated homography will be stored in h.
	Mat im_Aligned, h, final_Match;

	double detecttime = (double)getTickCount();
	// Align images
	cout << "Aligning images ..." << endl;
	alignImages(im, imReference, final_Match, im_Aligned, h);


	//计算检测时间
	detecttime = ((double)getTickCount() - detecttime) / getTickFrequency();
	printf("-----cost time-------:%7.3fs
", detecttime);

	// Write aligned image to disk.
	string str1 =
		string outFilename("aligned.jpg");
	cout << "Saving aligned image : " << outFilename << endl;
	//imwrite("D:/proc_img/String(outFilename)", im_Aligned);
	imwrite("matches.jpg", final_Match);

	// Print estimated homography
	cout << "Estimated homography : 
" << h << endl;
	system("pause");
	return 0;
}
*/
原文地址:https://www.cnblogs.com/Henry-ZHAO/p/12725184.html