zoukankan      html  css  js  c++  java
  • 旋转测试

    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include "opencv2/opencv.hpp"
    
    using namespace std;
    using namespace cv;
    
    bool rotation(Mat &in, Mat &out, const Size rect_size,
                                const Point2f center, const double angle) {
      if (1) {
        imshow("in", in);
        waitKey(0);
        //destroyWindow("in");
      }
    
      Mat in_large;
      in_large.create(int(in.rows * 1.5), int(in.cols * 1.5), in.type());
    
      float x = in_large.cols / 2 - center.x > 0 ? in_large.cols / 2 - center.x : 0;
      float y = in_large.rows / 2 - center.y > 0 ? in_large.rows / 2 - center.y : 0;//相对于in_large in的tl点坐标
    
      float width = x + in.cols < in_large.cols ? in.cols : in_large.cols - x;
      float height = y + in.rows < in_large.rows ? in.rows : in_large.rows - y;
    
      /*assert(width == in.cols);
      assert(height == in.rows);*/
    
      if (width != in.cols || height != in.rows) return false;
    
      Mat imageRoi = in_large(Rect_<float>(x, y, width, height));
      addWeighted(imageRoi, 0, in, 1, 0, imageRoi);
    
      Point2f center_diff(in.cols / 2.f, in.rows / 2.f);
      Point2f new_center(in_large.cols / 2.f, in_large.rows / 2.f);
    
      Mat rot_mat = getRotationMatrix2D(new_center, angle, 1);
    
      imshow("in_copy", in_large);
      waitKey(0);
    
      Mat mat_rotated;
      warpAffine(in_large, mat_rotated, rot_mat, Size(in_large.cols, in_large.rows),
                 CV_INTER_CUBIC);
    
      imshow("mat_rotated", mat_rotated);
      waitKey(0);
    
      Mat img_crop;
      getRectSubPix(mat_rotated, Size(rect_size.width, rect_size.height),
                    new_center, img_crop);
    
      out = img_crop;
    
      if (0) {
        imshow("out", out);
        waitKey(0);
        //destroyWindow("out");
      }
    
      imshow("img_crop", img_crop);
      waitKey(0);
    
      return true;
    }
    
    
    
    int main()
    {
    	Mat test = imread("./t.png"); 
    	//imshow("test", test);
    	Mat out;
    	Point2f center_diff(test.cols / 2.f, test.rows / 2.f);
    	rotation(test, out, test.size(), center_diff, -30.f);
    
    	return 0;
    }
    

      

  • 相关阅读:
    CAST和CONVERT
    #pragma 预处理指令详解
    Android系统移植主要事项
    Java动态绑定机制的内幕
    Java接口和抽象类用法总结
    Android工程的编译过程
    点击按钮,并且实现增加一个按钮的效果 (附效果图)
    iOS-设置导航栏"返回"按钮 (附效果图)
    常用代码整理(重要)
    NSTimer 的暂停与恢复运行。
  • 原文地址:https://www.cnblogs.com/fdd566/p/6519448.html
Copyright © 2011-2022 走看看