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 {
String path="D:\\WWY\\ForMP\\imgs";
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);
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="$";
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;
for (size_t i = 0; i < str.length(); i++)
{
if (tag == str[i])
{
if (!subStr.empty())
{
li.push_back(subStr);
subStr.clear();
}
}
else
{
subStr.push_back(str[i]);
}
}
if (!subStr.empty())
{
li.push_back(subStr);
}
return li;
}
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);
}
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;
for (size_t i = 0; i < str.length(); i++)
{
if (tag == str[i])
{
if (!subStr.empty())
{
li.push_back(subStr);
subStr.clear();
}
}
else
{
subStr.push_back(str[i]);
}
}
if (!subStr.empty())
{
li.push_back(subStr);
}
return li;
}
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);
}
waitKey(0);
return 0;
}