Java 使用裁剪图像检查多边形视点是否为矩形,是否在android中使用OpenCV库?

Java 使用裁剪图像检查多边形视点是否为矩形,是否在android中使用OpenCV库?,java,android,opencv3.0,opencv4android,Java,Android,Opencv3.0,Opencv4android,我使用OpenCV库从图像中检测较大的矩形形状。在检测到矩形后,我裁剪它们。当我裁剪图像时,我只需要裁剪矩形。 如果您看到图像1,请完美检测自动矩形。 第二个图像用户调整边缘。我想验证用户是否可以裁剪矩形边?如果此图像2类型的裁剪检测到,则我拒绝它们。我只想要矩形? 当我裁剪图像时,我得到了一些要点 if (polygonView.isValidShape(polygonView.getPoints())) { showProgressDialog(getResour

我使用OpenCV库从图像中检测较大的矩形形状。在检测到矩形后,我裁剪它们。当我裁剪图像时,我只需要裁剪矩形。 如果您看到图像1,请完美检测自动矩形。
第二个图像用户调整边缘。我想验证用户是否可以裁剪矩形边?如果此图像2类型的裁剪检测到,则我拒绝它们。我只想要矩形?

当我裁剪图像时,我得到了一些要点

 if (polygonView.isValidShape(polygonView.getPoints())) {
            showProgressDialog(getResources().getString(R.string.scanning));
            AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    SparseArray<PointF> pts = polygonView.getPoints();
                    float x1, x2, x3, x4, y1, y2, y3, y4;
                    x1 = Objects.requireNonNull(pts.get(0)).x * rapport;
                    x2 = Objects.requireNonNull(pts.get(1)).x * rapport;
                    x3 = Objects.requireNonNull(pts.get(2)).x * rapport;
                    x4 = Objects.requireNonNull(pts.get(3)).x * rapport;

                    y1 = Objects.requireNonNull(pts.get(0)).y * rapport;
                    y2 = Objects.requireNonNull(pts.get(1)).y * rapport;
                    y3 = Objects.requireNonNull(pts.get(2)).y * rapport;
                    y4 = Objects.requireNonNull(pts.get(3)).y * rapport;
                    scan(src, x1, y1, x2, y2, x3, y3, x4, y4);
                    

                    scanner.onScanFinish(Uri.parse(folder.getPath() + "/scanned.jpg"));
                    dismissDialog();
                }
            });
        }



// code after validation, cropping of the document to make a rectangle store them 



 private void scan(Mat src, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
    double w1 = sqrt( pow(x4 - x3 , 2) + pow(x4 - x3, 2));
    double w2 = sqrt( pow(x2 - x1 , 2) + pow(x2-x1, 2));
    double h1 = sqrt( pow(y2 - y4 , 2) + pow(y2 - y4, 2));
    double h2 = sqrt( pow(y1 - y3 , 2) + pow(y1-y3, 2));
    int maxWidth = (int) ((w1 < w2) ? w1 : w2);
    int maxHeight = (int) ((h1 < h2) ? h1 : h2);
    Mat dst = Mat.zeros(maxHeight, maxWidth, CV_8UC3);







   /* Double area= (double) maxHeight * (double) maxWidth;

    if (Math.abs(area) > (scaled.size().height/5 * scaled.size().width/5)) {
        System.out.println("yes"+Math.abs(area)+"max width"+maxWidth+"max height"+maxHeight);
    }else
    {
        System.out.println("yes"+Math.abs(area)+"max width"+maxWidth+"max height"+maxHeight);
    }

   if (isRectangle(w1, w2, h1, h2)) {
        System.out.println("Yes" + w1 + "," + w2 + "," + h1 + "," + h2);
    }else {
        System.out.println("No" + w1 + "," + w2 + "," + h1 + "," + h2);
    }


    Imgproc.rectangle(src,new Point(rect.x));*/


    // corners of destination image with the sequence [tl, tr, bl, br]

    List<Point> dst_pts = new ArrayList<>();
    List<Point> img_pts = new ArrayList<>();
    dst_pts.add(new Point(0, 0));
    dst_pts.add(new Point(maxWidth - 1, 0));
    dst_pts.add(new Point(0, maxHeight - 1));
    dst_pts.add(new Point(maxWidth - 1, maxHeight - 1));

    img_pts.add(new Point(x1,y1));
    img_pts.add(new Point(x2,y2));
    img_pts.add(new Point(x3,y3));
    img_pts.add(new Point(x4,y4));


    MatOfPoint2f mdst = new MatOfPoint2f();
    mdst.fromList(dst_pts);
    MatOfPoint2f mimg = new MatOfPoint2f();
    mimg.fromList(img_pts);
    // get transformation matrix
    Mat transmtx = Imgproc.getPerspectiveTransform(mimg, mdst);
    // apply perspective transformation
    Imgproc.warpPerspective(src, dst, transmtx, dst.size());

    Imgcodecs.imwrite(folder.getAbsolutePath()+"/scanned.jpg", dst);
}
if(polygonView.isValidShape(polygonView.getPoints()){
showProgressDialog(getResources().getString(R.string.scanning));
AsyncTask.execute(新的Runnable(){
@凌驾
公开募捐{
SparseArray pts=polygonView.getPoints();
浮球x1、x2、x3、x4、y1、y2、y3、y4;
x1=Objects.requirennoull(pts.get(0)).x*融洽关系;
x2=Objects.requirennoull(pts.get(1)).x*融洽关系;
x3=Objects.requirennoull(pts.get(2)).x*融洽关系;
x4=Objects.requirennoull(pts.get(3)).x*融洽关系;
y1=Objects.requirennoull(pts.get(0)).y*融洽关系;
y2=Objects.requirennoull(pts.get(1)).y*融洽关系;
y3=Objects.requirennoull(pts.get(2)).y*融洽关系;
y4=Objects.requirennoull(pts.get(3)).y*融洽关系;
扫描(src,x1,y1,x2,y2,x3,y3,x4,y4);
scanner.onscanfish(Uri.parse(folder.getPath()+“/scanned.jpg”);
dismissDialog();
}
});
}
//代码验证后,对文档进行裁剪,使其成为矩形存储
专用无效扫描(垫src、浮球x1、浮球y1、浮球x2、浮球y2、浮球x3、浮球y3、浮球x4、浮球y4){
双w1=sqrt(功率(x4-x3,2)+功率(x4-x3,2));
双w2=sqrt(功率(x2-x1,2)+功率(x2-x1,2));
双h1=sqrt(功率(y2-y4,2)+功率(y2-y4,2));
双h2=sqrt(功率(y1-y3,2)+功率(y1-y3,2));
int最大宽度=(int)((w1(比例大小().高度/5*比例大小().宽度/5)){
System.out.println(“是”+数学abs(面积)+“最大宽度”+最大高度”+最大高度”);
}否则
{
System.out.println(“是”+数学abs(面积)+“最大宽度”+最大高度”+最大高度”);
}
如果(isRectangle(w1、w2、h1、h2)){
系统输出打印项次(“是”+w1+、“+w2+”、“+h1+”、“+h2”);
}否则{
系统输出打印项次(“否”+w1+、“+w2+”、“+h1+”、“+h2”);
}
矩形(src,新点(rect.x))*/
//具有序列[tl、tr、bl、br]的目标图像的角点
List dst_pts=new ArrayList();
List img_pts=new ArrayList();
dst_点添加(新点(0,0));
dst_pts.add(新点(最大宽度-1,0));
dst_点添加(新点(0,最大高度-1));
dst_点添加(新点(最大宽度-1,最大高度-1));
增加(新点(x1,y1));
img_pts.add(新点(x2,y2));
img_零件添加(新点(x3,y3));
img_零件添加(新点(x4,y4));
MatOfPoint2f mdst=新的MatOfPoint2f();
mdst.fromList(dst_pts);
MatOfPoint2f mimg=新的MatOfPoint2f();
mimg.fromList(img_pts);
//求变换矩阵
Mat transmtx=Imgproc.getPerspectiveTransform(mimg,mdst);
//应用透视变换
Imgproc.warpPerspective(src、dst、transmtx、dst.size());
Imgcodecs.imwrite(folder.getAbsolutePath()+“/scanned.jpg”,dst);
}