java版利用opencv根据RotateRect裁剪图像区域算法

 
  
public Mat guiyihuaMatByRoi(Mat cpsrcMat,RotatedRect rotatedRect){
        /*Point[] rectPoints=new Point[4];
        rotatedRect.points(rectPoints);//存储矩形4点坐标
        for(int i=0;irotatedRect.size.width?rotatedRect.size.height:rotatedRect.size.width;
        double minrectbian=rotatedRect.size.height=0){
            newpX=oldx*cos+oldy*sin;
            newpY=oldy/cos+(cpsrcMat.width()-(oldx+oldy*tan))*sin;
        }
        //Imgproc.circle(rotated,new Point(newpX,newpY),5,new Scalar(255,0,0),2);

        int startrow=(int)(newpY-minrectbian/2)-pading;
        if(startrow<0)startrow=0;

        int endrow=(int)(newpY+minrectbian/2)+pading;
        if(endrow>=rotated.height())endrow=rotated.height();

        int startcls=(int)(newpX-maxrectbian/2)-pading;
        if(startcls<0)startcls=0;

        int endcls= (int)(newpX+maxrectbian/2)+pading;
        if(endcls>=rotated.width())endcls=rotated.width();
        rotated=rotated.submat(startrow,endrow,startcls,endcls);
        //Imgproc.circle(rotated,new Point(startcls,startrow),5,new Scalar(0,255,0),2);
        //Imgproc.circle(rotated,new Point(endcls,startrow),5,new Scalar(0,255,0),2);
        //Imgproc.circle(rotated,new Point(startcls,endrow),5,new Scalar(0,255,0),2);
        //Imgproc.circle(rotated,new Point(endcls,endrow),5,new Scalar(0,255,0),2);
        return  rotated;
    }
    //旋转图像内容不变,尺寸相应变大
    public Mat rotateImage1(Mat img, double degree){
        double angle = degree  * Math.PI/ 180.; // 弧度
        double a = Math.sin(angle), b = Math.cos(angle);
        int width = img.width();
        int height = img.height();
        int width_rotate = (int)(height * Math.abs(a) + width * Math.abs(b));
        int height_rotate = (int)(width *  Math.abs(a) + height *  Math.abs(b));
        //旋转数组map
        // [ m0  m1  m2 ] ===>  [ A11  A12   b1 ]
        // [ m3  m4  m5 ] ===>  [ A21  A22   b2 ]
        Mat map_matrix = new Mat(2, 3, CvType.CV_32F);
        // 旋转中心
        Point center = new Point(width / 2, height / 2);
        map_matrix = Imgproc.getRotationMatrix2D(center, degree, 1.0);
        map_matrix.put(0,2,map_matrix.get(0,2)[0]+ (width_rotate - width) / 2);
        map_matrix.put(1,2,map_matrix.get(1,2)[0]+ (height_rotate - height) / 2);
        Mat rotated=new Mat();
        Imgproc.warpAffine(img, rotated,map_matrix, new Size(width_rotate,height_rotate), Imgproc.INTER_LINEAR|Imgproc.WARP_FILL_OUTLIERS,0,new Scalar(255,255,255));
        return rotated;
    }

你可能感兴趣的:(opencv)