暂时存放

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ForLabel {
	private static Map<String,String> map=new HashMap<String,String>();
	private static Random rand=new Random();
	private static void buildDictionary()
	{
		map.put("1", "1");
		map.put("2", "2");
		map.put("3", "3");
		map.put("4", "4");
		map.put("5", "5");
		map.put("6", "6");
		map.put("7", "7");
		map.put("8", "8");
		map.put("9", "9");
		map.put("0", "10");
		
		map.put("#", "11");
		map.put("A", "12");
		map.put("B", "13");
		map.put("C", "14");
		map.put("D", "15");
		map.put("E", "16");
		map.put("F", "17");
		map.put("G", "18");
		map.put("H", "19");
		map.put("I", "20");
		map.put("J", "21");
		map.put("K", "22");
		map.put("L", "23");
		map.put("M", "24");
		map.put("N", "25");
		map.put("O", "26");
		map.put("P", "27");
		map.put("Q", "28");
		map.put("R", "29");
		map.put("S", "30");
		map.put("T", "31");
		map.put("U", "32");
		map.put("V", "33");
		map.put("W", "34");
		map.put("X", "35");
		map.put("Y", "36");
		map.put("Z", "37");  
		map.put("+", "38");
		map.put("-", "39");
		map.put("(", "40");
		map.put("a", "41");
		map.put("b", "42");
		map.put("c", "43");
		map.put("d", "44");
		map.put("e", "45");
		map.put("f", "46");
		map.put("g", "47");
		map.put("h", "48");
		map.put("i", "49");
		map.put("j", "50");
		map.put("k", "51");
		map.put("l", "52");
		map.put("m", "53");
		map.put("n", "54");
		map.put("o", "55");
		map.put("p", "56");
		map.put("q", "57");
		map.put("r", "58");
		map.put("s", "59");
		map.put("t", "60");
		map.put("u", "61");
		map.put("v", "62");
		map.put("w", "63");
		map.put("x", "64");
		map.put("y", "65");
		map.put("z", "66");
		map.put("/", "67");
		map.put("*", "68");
		map.put(".", "69");
		map.put(")", "70");
		map.put("!", "71");
		map.put("$", "0");
		
	}
	public static <T> void swap(T[] a,int i,int j)
	{
		T temp=a[i];
		a[i]=a[j];
		a[j]=temp;
	}
	public static <T>void shuffle(T[] array)
	{
		int length=array.length;
		for (int i=length;i>0;i--)
		{
			int randInd=rand.nextInt(i);
			swap(array,randInd,i-1);
		}
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				String path="D:\\WWY\\ForMP\\imgs";
				buildDictionary();
				//buildDictionary();
				File file = new File(path);
				MyFilter6 myFilter = new MyFilter6();
				File[] images=file.listFiles(myFilter);
				shuffle(images);
				FileWriter fileWrite=new FileWriter(path+"\\"+"train.txt",true);	
				FileWriter fileWrite2=new FileWriter(path+"\\"+"vali.txt",true);
				int count=0;
				int size=images.length;
				for(File image:images) {
					String wholeImageName=image.getName();
					String imageName=wholeImageName.substring(0, wholeImageName.lastIndexOf("."));
					System.out.println(imageName);
					String[] tname=imageName.split("_");
					System.out.println(tname.length);
					/*if(tname.length<4) {
						image.delete();
						continue;
					}*/
					List list = new ArrayList<String>();
					
					
					for(int i=0;i<38;i++)
					{
						String tmp="";
						if(i<tname[3].length())
						  tmp=tname[3].substring(i, i+1);
						else
						   tmp="$";
						//if(!tmp.equals("$"))
						list.add(map.get(tmp));
					}
					String finalString="";
					for(int k=0;k<list.size();k++)
						finalString=finalString+list.get(k)+" ";
					count++;
					
					if(count<size*0.8)
						fileWrite.write(wholeImageName+" "+finalString+'\n');
					else
						fileWrite2.write(wholeImageName+" "+finalString+'\n');
					}
				fileWrite.flush();
				fileWrite.close();
				fileWrite2.flush();
				fileWrite2.close();
				System.out.println("ok");
				}
	
}

//定义一个过滤器类
class MyFilter6 implements FilenameFilter {
	@Override
	public boolean accept(File dir, String name) {
		if(name.endsWith("jpg")){
			return true;
		}else{
			return false;
		}
	}
}
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include<io.h>
using namespace cv;
using namespace std;

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\\*.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\\bac\\kongtu.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 angle =4 ;//角度
		int XforCoordinate = bacImage.rows / 2;//将坐标原点转移到几何中心
		int YforCoordinate = bacImage.cols / 2;
		
		Rotate(bacImage, destImage, angle);
		vector<string> test;
		test = splitstr(images_files[i], '_');
		cout << images_files[i]<<endl;
		imwrite("D:\\WWY\\1\\save\\"+test[3], destImage);
	}
	/*
	//读入图像,并判断图像是否读入正确
	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;
}
#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\\*.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\\bac\\kongtu.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 angle =4 ;//角度
		double XforCoordinate = bacImage.rows / 2;//将坐标原点转移到几何中心
		double YforCoordinate = bacImage.cols / 2;
		cout << "XforCoordinate=" << XforCoordinate << endl;
		cout << "YforCoordinate=" << YforCoordinate << endl;
		double x1 = x0 - XforCoordinate;
		double x2 = x0 + srcImage.rows - XforCoordinate;
		double y1 = y0 - YforCoordinate;
		double y2 = y0 + srcImage.cols - YforCoordinate;
		cout << "x1=" << x1+ XforCoordinate << endl;
		cout << "x2=" << x2+ XforCoordinate << endl;
		cout << "y1=" << y1+ YforCoordinate << endl;
		cout << "y2=" << y2 + YforCoordinate << endl;
		double b = angle * pi / 180;//角度转弧度
		double cosOfangle = cos(b);
		double sinOfangle = -sin(b);
		cout << "b=" << b << endl;
		cout << "cosOfangle=" << cosOfangle << endl;
		cout << "sinOfangle=" << sinOfangle << endl;
		double theX1 = x1 * cosOfangle + y2 * sinOfangle;
		double theX2 = x2 * cosOfangle + y1 * sinOfangle;
		double theY1 = y1 * cosOfangle - x2 * sinOfangle;
		double theY2 = y2 * cosOfangle - x1 * sinOfangle;
		cout << "theX1=" << theX1 << endl;
		cout << "theX2=" << theX2 << endl;
		cout << "theY1=" << theY1 << endl;
		cout << "theY2=" << theY2 << endl;
		double X1now = theX1 + XforCoordinate;
		double X2now = theX2 + XforCoordinate;
		double Y1now = theY1 + YforCoordinate;
		double Y2now =theY2 + YforCoordinate;
		cout << "X1now=" << X1now << endl;
		cout << "X2now=" << X2now << endl;
		cout << "Y1now=" << Y1now << endl;
		cout << "Y2now=" << Y2now << endl;
		Rotate(bacImage, destImage, angle);
		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[3], 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、付费专栏及课程。

余额充值