Includes image batch translation, rotation, and pixel transformation

#include <opencv2/opencv.hpp>
#include "opencv2/features2d.hpp"
#include <vector>
#include <algorithm>
#include <iostream>
#include "windows.h"
#include <stdio.h>
#include <time.h>
#include <math.h>  
#include <fstream>
#include <fstream>
#include <iostream>
#include <filesystem>
#define WINDOW_NAME [Program window]			
using namespace cv;

using namespace std;
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
namespace fs = std::filesystem;

Mat Translation(Mat& src, int x, int y)
{
	cv::Mat dst;

	cv::Size dst_sz = src.size(a);// Define the translation matrix
	cv::Mat t_mat = cv::Mat::zeros(2.3, CV_32FC1);

	t_mat.at<float> (0.0) = 1;
	t_mat.at<float> (0.2) = x; // Horizontal translation
	t_mat.at<float> (1.1) = 1;
	t_mat.at<float> (1.2) = y; // Vertical translation

	// Perform affine transformation according to translation matrix
	cv::warpAffine(src, dst, t_mat, dst_sz);

	// Display translation effect
	//cv::imshow("image", src);
	//cv::imshow("result", dst);
	return dst;
}

Mat Rotate(Mat& src, double angle)
{
	cv::Mat dst;

	cv::Size src_sz = src.size(a);cv::Size dst_sz(src_sz.height, src_sz.width);
	int len = max(src.cols, src.rows);

	// Specify the rotation center
	cv::Point2f center(len / 2., len / 2.);

	// Get rotation matrix (2x3 matrix)
	cv::Mat rot_mat = cv::getRotationMatrix2D(center, angle, 1.0);

	// Perform affine transformation according to rotation matrix
	cv::warpAffine(src, dst, rot_mat, dst_sz);
	return dst;
}

Mat K_Pixels(Mat& src, double K)
{
	cv::Mat dst = K * src;
	return dst;
}

fs::path p{ "D:/ some files/Python code/AI/ Using data sets/enhanced label and filene.txt" };
ofstream output{ p };
void Save_filenameLabel(cv::String savedfilename)
{
	output << savedfilename << "" << to_string(1) << endl;
}
int main(a)
{
	cv::String path = C:/Users/LENOVO/Desktop/ color conversion /;        // Address of the image folder to be processed
	cv::String dest = "C:/Users/LENOVO/Desktop/;    // Save the address of the processed image
	cout << "Address obtained successfully" << endl;
	cv::String savedfilename;
	std::vector<cv::String> filenames;
	int len = path.length(a); cv::Mat srcImg; cv::glob(path, filenames);                 // OpencV is a very useful function to read the file name in the specified path
	for (int i = 0; i < 13; i++) {
		srcImg = cv::imread(filenames[i]);
		resize(srcImg, srcImg, Size(128.128));
		Mat dstImg;
		int Count = 1;
		/ / * * * * * * * * * * * * * * * * * * * * * * * * * for image processing part of the * * * * * * * * * * * * * * * * * * * * * * * * * * * /I'm going to shift it down to the rightfor (int j = 1; j < 21; j++)
		{
			dstImg = Translation(srcImg, j * 2, j * 2);
			savedfilename = dest + to_string(Count) + filenames[i].substr(len);

			std::cout << savedfilename << std::endl;
			cv::imwrite(savedfilename, dstImg);
			Save_filenameLabel(to_string(Count) + filenames[i].substr(len));
			cout << "The first" << j << "Zhang Finish"<< endl; Count++; } pan up to the leftfor (int j = 1; j < 21; j++)
		{
			dstImg = Translation(srcImg, - 1 * j * 2.- 1 * j * 2);
			savedfilename = dest + to_string(Count) + filenames[i].substr(len);

			std::cout << savedfilename << std::endl;
			cv::imwrite(savedfilename, dstImg);
			cout << "The first" << j << "Zhang Finish" << endl;
			Save_filenameLabel(to_string(Count) + filenames[i].substr(len)); Count++; } move up to the rightfor (int j = 1; j < 21; j++)
		{
			dstImg = Translation(srcImg, j * 2.- 1 * j * 2);
			savedfilename = dest + to_string(Count) + filenames[i].substr(len);

			std::cout << savedfilename << std::endl;
			cv::imwrite(savedfilename, dstImg);
			cout << "The first" << j << "Zhang Finish" << endl;
			Save_filenameLabel(to_string(Count) + filenames[i].substr(len)); Count++; } pan down to the leftfor (int j = 1; j < 21; j++)
		{
			dstImg = Translation(srcImg, - 1 * j * 2, j * 2);
			savedfilename = dest + to_string(Count) + filenames[i].substr(len);

			std::cout << savedfilename << std::endl;
			cv::imwrite(savedfilename, dstImg);
			cout << "The first" << j << "Zhang Finish" << endl;
			Save_filenameLabel(to_string(Count) + filenames[i].substr(len));
			Count++;
		}
		/ / rotation
		for (int j = 1; j < 21; j++)
		{
			dstImg = Rotate(srcImg, j * 8);
			savedfilename = dest + to_string(Count) + filenames[i].substr(len);

			std::cout << savedfilename << std::endl;
			cv::imwrite(savedfilename, dstImg);
			cout << "The first" << j << "Zhang Finish" << endl;
			Save_filenameLabel(to_string(Count) + filenames[i].substr(len));
			Count++;
		}
		// Pixel changes
		for (int j = 1; j < 11; j++)
		{
			dstImg = K_Pixels(srcImg, 0.5 + j * 0.1);
			savedfilename = dest + to_string(Count) + filenames[i].substr(len);

			std::cout << savedfilename << std::endl;
			cv::imwrite(savedfilename, dstImg);
			cout << "The first" << j << "Zhang Finish" << endl;
			Save_filenameLabel(to_string(Count) + filenames[i].substr(len));
			Count++;
		}
		//resize(srcImg, dstImg, Size(128, 128));
		/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
	}
	output.close(a);waitKey(0);
	return 0;
}
Copy the code