java opencv 旋转90度_OpenCV3 Java视角转换 图像透视变换
代码案例:packagecom.what21.opencv.demo;importjava.util.List;importorg.opencv.core.Core;importorg.opencv.core.CvType;importorg.opencv.core.Mat;importorg.opencv.core.Point;importorg.opencv.imgcodecs....
代码案例:package com.what21.opencv.demo;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;
public class Perspective {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src=Imgcodecs.imread("D:/ShareData/internet.jpg");
//读取图像到矩阵中,取灰度图像
if(src.empty()){
return ;
}
try{
int xMargin,yMargin;
int x0=src.cols()/4;
int x1=(src.cols()/4)*3;
int y0=src.cols()/4;
int y1=(src.cols()/4)*3;
Mat dst=new Mat();
List listSrcs=java.util.Arrays.asList(new Point(x0,y0),new Point(x0,y1),new Point(x1,y1),new Point(x1,y0));
Mat srcPoints=Converters.vector_Point_to_Mat(listSrcs,CvType.CV_32F);
xMargin=src.cols()/10;
yMargin=src.rows()/10;
List listDsts=java.util.Arrays.asList(new Point(x0+xMargin,y0+yMargin),listSrcs.get(1),listSrcs.get(2),new Point(x1-xMargin,y0+yMargin));
Mat dstPoints=Converters.vector_Point_to_Mat(listDsts,CvType.CV_32F);
Mat perspectiveMmat=Imgproc.getPerspectiveTransform(srcPoints, dstPoints);
Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(),Imgproc.INTER_LINEAR);
Imgcodecs.imwrite("D:/ShareData/internet.dst0.jpg",dst);
xMargin=src.cols()/8;
yMargin=src.cols()/8;
listDsts.set(0, listSrcs.get(0));
listDsts.set(1, listSrcs.get(1));
listDsts.set(2, new Point(x1-xMargin,y1-yMargin));
listDsts.set(3, new Point(x1-xMargin,y0-yMargin));
dstPoints=Converters.vector_Point_to_Mat(listDsts,CvType.CV_32F);
perspectiveMmat=Imgproc.getPerspectiveTransform(srcPoints, dstPoints);
Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(),Imgproc.INTER_LINEAR);
Imgcodecs.imwrite("D:/ShareData/internet.dst1.jpg", dst);
xMargin=src.cols()/6;
yMargin=src.cols()/6;
listDsts.set(0, new Point(x0+xMargin,y0+yMargin));
listDsts.set(1, listSrcs.get(1));
listDsts.set(2, new Point(x1-xMargin,y1-yMargin));
listDsts.set(3, listSrcs.get(3));
dstPoints=Converters.vector_Point_to_Mat(listDsts,CvType.CV_32F);
perspectiveMmat=Imgproc.getPerspectiveTransform(srcPoints, dstPoints);
Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(),Imgproc.INTER_LINEAR);
Imgcodecs.imwrite("D:/ShareData/internet.dst2.jpg", dst);
}catch(Exception e){
e.printStackTrace();
}
}
}
更多推荐
所有评论(0)