#include #include #include using namespace std; using namespace cv; /** * Rotate an image */ void rotate(cv::Mat& src, double angle, cv::Mat& dst, Point center) { int len = std::max(src.cols, src.rows); cv::Point2f pt(center.x, center.y); cv::Mat r = cv::getRotationMatrix2D(pt, angle, 1.0); cv::warpAffine(src, dst, r, cv::Size(len, len)); } void draw_square( Point a, Point b, Point c, Point d, cv::Mat &matimg) { cv::Scalar line_color; line_color = CV_RGB( 255, 0, 0 ); cv::line(matimg, a, b, line_color, 1, 8, 0); cv::line(matimg, b, c, line_color, 1, 8, 0); cv::line(matimg, c, d, line_color, 1, 8, 0); cv::line(matimg, d, a, line_color, 1, 8, 0); return; } Point get_vertex(int x, int y, double rotation, Size textSize, Point center) { int x1, y1; double a,b,d,e,g,h; double cz = 0.0, sz = 0.0; cz = cos(M_PI * rotation / 180.0); sz = sin(M_PI * rotation / 180.0); a = cz; b = sz; d = 0 - sz; e = cz; g = 0 - textSize.width/2*cz + textSize.height/2*sz + center.x; h = 0 - textSize.width/2*sz - textSize.height/2*cz + center.y; x1 = a*x + d*y + g; y1 = b*x + e*y + h; return Point(x1,y1); } int main() { Mat img(320,320,CV_8UC3,Scalar(255,255,255)); // Create and generate template Mat textImg = Mat::zeros(img.rows, img.cols, img.type()); double fontscale = 2.0; Size textSize = getTextSize("F", FONT_HERSHEY_SIMPLEX, fontscale, 6, 0); ofstream gt("./seq11/gt.txt"); Point center(250, 250); Point ld(center.x-textSize.width/2, center.y+textSize.height/2); Rect roi_region(center.x-textSize.width/2,center.y-textSize.height/2,textSize.width,textSize.height); Mat template_img = img(roi_region); putText(textImg, "F", ld, FONT_HERSHEY_SIMPLEX, fontscale ,Scalar(255,255,255),6); rotate(textImg, 0, textImg, center); img= img-textImg; imwrite("template.jpg",template_img); template_img.release(); cv::Point a; // w = 0, h = 0 cv::Point b; // w = 0, h = max cv::Point c; // w = max, h = max cv::Point d; // w = max, h = 0 char filesavename[100]; /* a----b | | d----c */ for(int i = 0; i < 360; i+=10) { img.setTo(Scalar(255,255,255)); textImg.setTo(Scalar(0,0,0)); putText(textImg, "F", ld, FONT_HERSHEY_SIMPLEX, fontscale ,Scalar(255,255,255),6); rotate(textImg, 360-i, textImg, center); img= img-textImg; a = get_vertex(0,0,i,textSize,center); b = get_vertex(textSize.width,0,i,textSize,center); c = get_vertex(textSize.width,textSize.height,i,textSize,center); d = get_vertex(0,textSize.height,i,textSize,center); sprintf(filesavename,"./seq11/%d.jpg",i); imwrite(filesavename,img); sprintf(filesavename,"./seq11/GT_img/%d.jpg",i); draw_square(a,b,c,d,img); imwrite(filesavename,img); gt<