旋转图片造点数据

由于学业不精,所以代码没有那么强的包容性
本代码逻辑为,小图数据贴到大图,然后旋转,然后切出新图
注意:
1.小图的贴图初始坐标需要设定在大图左上方
在这里插入图片描述
2.代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include<io.h>
#include <cmath>
using namespace cv;
using namespace std;

#define pi 3.14159265
typedef std::vector<std::string>  StringList;
/*
分割字符串
*/
StringList splitstr(const std::string& str, char tag)
{
	StringList  li;
	std::string subStr;

	//遍历字符串,同时将i位置的字符放入到子串中,当遇到tag(需要切割的字符时)完成一次切割
	//遍历结束之后即可得到切割后的字符串数组
	for (size_t i = 0; i < str.length(); i++)
	{
		if (tag == str[i]) //完成一次切割
		{
			if (!subStr.empty())
			{
				li.push_back(subStr);
				subStr.clear();
			}
		}
		else //将i位置的字符放入子串
		{
			subStr.push_back(str[i]);
		}
	}

	if (!subStr.empty()) //剩余的子串作为最后的子字符串
	{
		li.push_back(subStr);
	}

	return li;
}
// 图像旋转
///@ angle 要旋转的角度
void Rotate(const Mat &srcImage, Mat &destImage, double angle)
{
	Point2f center(srcImage.cols / 2, srcImage.rows / 2);//中心
	Mat M = getRotationMatrix2D(center, angle, 1);//计算旋转的仿射变换矩阵 
	warpAffine(srcImage, destImage, M, Size(srcImage.cols, srcImage.rows), INTER_LINEAR, BORDER_REPLICATE);//仿射变换  
	circle(destImage, center, 2, Scalar(255, 0, 0));
}

int main()
{
	String imagePath = "D:\\WWY\\1\\1108\\wwy\\*.jpg";
	
	std::vector<String>images_files;
	cv::glob(imagePath, images_files);
	for (int i = 0; i < images_files.size(); i++) {
		cv::Mat srcImage = imread(images_files[i]);
		cv::Mat bacImage = imread("D:\\WWY\\1\\1108\\bC\\bac.jpg");//背景图
		int x0 = 200;
		int y0 = 180;
		Mat imageROI = bacImage(Range(x0, x0 + srcImage.rows), Range(y0, y0 + srcImage.cols));
		srcImage.copyTo(imageROI);
		if (!srcImage.data) {
			cout << "no image";
			return -1;
		}		
		Mat destImage;
		double Forangle =-4 ;//角度
		double angle = fabs(Forangle);
		double XforCoordinate = bacImage.rows / 2;//将坐标原点转移到几何中心
		double YforCoordinate = bacImage.cols / 2;
		double x1 = x0 - XforCoordinate;//调整坐标系数,以几何中心为原点
		double x2 = x0 + srcImage.rows - XforCoordinate;
		double y1 = y0 - YforCoordinate;
		double y2 = y0 + srcImage.cols - YforCoordinate;
		//计算旋转后坐标
		double b = angle * pi / 180;//角度转弧度
		double cosOfangle = cos(b);
		double sinOfangle = -sin(b);
		double theX1, theX2, theY1, theY2;
		if (Forangle < 0) {
			 theX1 = x1 * cosOfangle - y1 * sinOfangle;
			 theX2 = x2 * cosOfangle - y2 * sinOfangle;
			 theY1 = y1 * cosOfangle + x2 * sinOfangle;
			 theY2 = y2 * cosOfangle + x1 * sinOfangle;
		}else {
		     theX1 = x1 * cosOfangle + y2 * sinOfangle;
			 theX2 = x2 * cosOfangle + y1 * sinOfangle;
			 theY1 = y1 * cosOfangle - x2 * sinOfangle;
			 theY2 = y2 * cosOfangle - x1 * sinOfangle;
		}
		double X1now = theX1 + XforCoordinate;//还原坐标
		double X2now = theX2 + XforCoordinate;
		double Y1now = theY1 + YforCoordinate;
		double Y2now =theY2 + YforCoordinate;
		Rotate(bacImage, destImage, Forangle);
		vector<string> test;
		test = splitstr(images_files[i], '_');
		cout << images_files[i]<<endl;
		Mat AnewData = destImage(Range(X1now, X2now), Range(Y1now, Y2now));
		imwrite("D:\\WWY\\1\\save\\"+test[2], AnewData);
	}
	/*
	//读入图像,并判断图像是否读入正确
	cv::Mat srcImage = imread("D:\\WWY\\1\\img_2221_10862_0.jpg");
	cv::Mat bacImage = imread("D:\\WWY\\1\\bac\\kongtu.jpg");
	Mat imageROI = bacImage(Range(550 , 550 + srcImage.rows), Range(180 , 180 + srcImage.cols));
	srcImage.copyTo(imageROI);
	if (!srcImage.data)
		return -1;
	imshow("srcImage", srcImage);
	//将图片按比例缩放至宽为250像素的大小
	Mat destImage;
	double angle =3;//角度
	Rotate(bacImage, destImage, angle);
	imshow("dst", destImage);
	imwrite("D:\\WWY\\1\\save\\Q234R.jpg", destImage);
	*/
	waitKey(0);
	return 0;
}

3.新坐标的数学逻辑
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.0 不贴图了

#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include<io.h>
#include <cmath>
using namespace cv;
using namespace std;

#define pi 3.14159265
typedef std::vector<std::string>  StringList;
/*
分割字符串
*/
StringList splitstr(const std::string& str, char tag)
{
	StringList  li;
	std::string subStr;

	//遍历字符串,同时将i位置的字符放入到子串中,当遇到tag(需要切割的字符时)完成一次切割
	//遍历结束之后即可得到切割后的字符串数组
	for (size_t i = 0; i < str.length(); i++)
	{
		if (tag == str[i]) //完成一次切割
		{
			if (!subStr.empty())
			{
				li.push_back(subStr);
				subStr.clear();
			}
		}
		else //将i位置的字符放入子串
		{
			subStr.push_back(str[i]);
		}
	}

	if (!subStr.empty()) //剩余的子串作为最后的子字符串
	{
		li.push_back(subStr);
	}

	return li;
}
// 图像旋转
///@ angle 要旋转的角度
void Rotate(const Mat &srcImage, Mat &destImage, double angle)
{
	Point2f center(srcImage.cols / 2, srcImage.rows / 2);//中心
	Mat M = getRotationMatrix2D(center, angle, 1);//计算旋转的仿射变换矩阵 
	double XforCoordinate = srcImage.rows / 2;//将坐标原点转移到几何中心
	double YforCoordinate = srcImage.cols / 2;
	double x1 = 0 - XforCoordinate;//调整坐标系数,以几何中心为原点
	double x2 = 0 + srcImage.rows - XforCoordinate;
	double y1 = 0 - YforCoordinate;
	double y2 = 0 + srcImage.cols - YforCoordinate;
	//计算旋转后坐标

	double b = angle * pi / 180;//角度转弧度
	double cosOfangle = cos(b);
	double sinOfangle = -sin(b);
	double theX1, theX2, theY1, theY2;
	if (angle < 0) {
		theX1 = x1 * cosOfangle - y1 * sinOfangle;
		theX2 = x2 * cosOfangle - y2 * sinOfangle;
		theY1 = y1 * cosOfangle + x2 * sinOfangle;
		theY2 = y2 * cosOfangle + x1 * sinOfangle;
	}
	else {
		theX1 = x1 * cosOfangle - y2 * sinOfangle;
		theX2 = x2 * cosOfangle + y1 * sinOfangle;
		theY1 = y1 * cosOfangle - x2 * sinOfangle;
		theY2 = y2 * cosOfangle - x1 * sinOfangle;
	}
	double X1now = theX1 + XforCoordinate;//还原坐标
	double X2now = theX2 + XforCoordinate;
	double Y1now = theY1 + YforCoordinate;
	double Y2now = theY2 + YforCoordinate;
	int top = X1now;
	int bottom = X1now;
	int left = Y1now;
	int right =Y1now;
	cout << " X1now" << X1now<<endl;
	cout << " X2now" << X2now << endl;
	cout << " Y1now" << Y1now << endl;
	cout << " Y2now" << Y2now << endl;

	/*int top = (int)(0.05*srcImage.rows);
	int bottom = (int)(0.05*srcImage.rows);
	int left = (int)(0.05*srcImage.cols);
	int right = (int)(0.05*srcImage.cols);*/

	RNG rng(12345);//用于构造随机值
	Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
	Mat dst;
	copyMakeBorder(srcImage, dst, top, bottom, left, right, BORDER_REPLICATE, color);
	warpAffine(dst, destImage, M, Size(dst.cols, dst.rows), INTER_LINEAR, BORDER_REPLICATE);//仿射变换  
	circle(destImage, center, 2, Scalar(255, 0, 0));
}

int main()
{
	String imagePath = "D:\\WWY\\1\\1108\\1108\\*.jpg";
	
	std::vector<String>images_files;
	cv::glob(imagePath, images_files);
	
	
	//namedWindow("srcImage");
	//resizeWindow("srcImage", 640, 480);
	//imshow("srcImage", srcImage);
	for (int i = 0; i < images_files.size(); i++) {
		cv::Mat srcImage = imread(images_files[i]);
		if (!srcImage.data) {
			cout << "no image";
			return -1;
		}
		Mat destImage;
		double Forangle = 3;//角度
		Rotate(srcImage, destImage, Forangle);
		vector<string> test;
		test = splitstr(images_files[i], '_');
		cout << images_files[i] << endl;
		imwrite("D:\\WWY\\1\\save\\" + test[2], destImage);
	}
	//imshow("destImage", destImage);
	/*for (int i = 0; i < images_files.size(); i++) {
		cv::Mat srcImage = imread(images_files[i]);
		cv::Mat bacImage = imread("D:\\WWY\\1\\1108\\bC\\bac.jpg");//背景图
		int x0 = 200;
		int y0 = 180;
		Mat imageROI = bacImage(Range(x0, x0 + srcImage.rows), Range(y0, y0 + srcImage.cols));
		srcImage.copyTo(imageROI);
		if (!srcImage.data) {
			cout << "no image";
			return -1;
		}		
		Mat destImage;
		double Forangle =-4 ;//角度
		double angle = fabs(Forangle);
		double XforCoordinate = bacImage.rows / 2;//将坐标原点转移到几何中心
		double YforCoordinate = bacImage.cols / 2;
		double x1 = x0 - XforCoordinate;//调整坐标系数,以几何中心为原点
		double x2 = x0 + srcImage.rows - XforCoordinate;
		double y1 = y0 - YforCoordinate;
		double y2 = y0 + srcImage.cols - YforCoordinate;
		//计算旋转后坐标
		double b = angle * pi / 180;//角度转弧度
		double cosOfangle = cos(b);
		double sinOfangle = -sin(b);
		double theX1, theX2, theY1, theY2;
		if (Forangle < 0) {
			 theX1 = x1 * cosOfangle - y1 * sinOfangle;
			 theX2 = x2 * cosOfangle - y2 * sinOfangle;
			 theY1 = y1 * cosOfangle + x2 * sinOfangle;
			 theY2 = y2 * cosOfangle + x1 * sinOfangle;
		}else {
		     theX1 = x1 * cosOfangle + y2 * sinOfangle;
			 theX2 = x2 * cosOfangle + y1 * sinOfangle;
			 theY1 = y1 * cosOfangle - x2 * sinOfangle;
			 theY2 = y2 * cosOfangle - x1 * sinOfangle;
		}
		double X1now = theX1 + XforCoordinate;//还原坐标
		double X2now = theX2 + XforCoordinate;
		double Y1now = theY1 + YforCoordinate;
		double Y2now =theY2 + YforCoordinate;
		Rotate(bacImage, destImage, Forangle);
		vector<string> test;
		test = splitstr(images_files[i], '_');
		cout << images_files[i]<<endl;
		Mat AnewData = destImage(Range(X1now, X2now), Range(Y1now, Y2now));
		imwrite("D:\\WWY\\1\\save\\"+test[2], AnewData);
	}*/

	/*
	//读入图像,并判断图像是否读入正确
	cv::Mat srcImage = imread("D:\\WWY\\1\\img_2221_10862_0.jpg");
	cv::Mat bacImage = imread("D:\\WWY\\1\\bac\\kongtu.jpg");
	Mat imageROI = bacImage(Range(550 , 550 + srcImage.rows), Range(180 , 180 + srcImage.cols));
	srcImage.copyTo(imageROI);
	if (!srcImage.data)
		return -1;
	imshow("srcImage", srcImage);
	//将图片按比例缩放至宽为250像素的大小
	Mat destImage;
	double angle =3;//角度
	Rotate(bacImage, destImage, angle);
	imshow("dst", destImage);
	imwrite("D:\\WWY\\1\\save\\Q234R.jpg", destImage);
	*/
	waitKey(0);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值