Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-OpenCV忽略额外轮廓_Java_Opencv - Fatal编程技术网

Java-OpenCV忽略额外轮廓

Java-OpenCV忽略额外轮廓,java,opencv,Java,Opencv,我有一个形象: 我想把它裁剪成这本书 我正在使用OpenCV尝试获取图像的控制。一旦我画出来,它看起来像这样。如何忽略图像右侧的额外轮廓?我已经尝试过使用标准偏差的异常值。现在,它获取矩形内的每个点,并将其添加到arraylist中以供以后处理。我有一个点的整体数组列表,还有2个,所以当计算统计分析时,点可以从最小到最大排序 这就是它现在的样子: 导入java.awt.Point; 导入java.io.IOException; 导入java.util.ArrayList; 导入java.ut

我有一个形象:

我想把它裁剪成这本书

我正在使用OpenCV尝试获取图像的控制。一旦我画出来,它看起来像这样。如何忽略图像右侧的额外轮廓?我已经尝试过使用标准偏差的异常值。现在,它获取矩形内的每个点,并将其添加到arraylist中以供以后处理。我有一个点的整体数组列表,还有2个,所以当计算统计分析时,点可以从最小到最大排序

这就是它现在的样子:

导入java.awt.Point;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入org.opencv.core.core;
导入org.opencv.core.Mat;
导入org.opencv.core.MatOfPoint;
导入org.opencv.core.MatOfPoint2f;
导入org.opencv.core.Rect;
导入org.opencv.imgcodecs.imgcodecs;
导入org.opencv.imgproc.imgproc;
公共类imtest{
公共静态void main(字符串args[])引发IOException{
String filename=“C:/image.png”;
System.loadLibrary(Core.NATIVE\u LIBRARY\u NAME);
Mat torect=新Mat();
Mat torect1=Imgcodecs.imread(文件名,0);
Imgproc.Canny(torect1,torect,10100);
列表等高线=新的ArrayList();
Imgproc.findContours(torect.clone()、等高线、new Mat()、Imgproc.RETR\u列表、Imgproc.CHAIN\u近似简单);
ArrayList outlie=新的ArrayList();
ArrayList ylist=新的ArrayList();
ArrayList xlist=新的ArrayList();
MatOfPoint2f approxCurve=新的MatOfPoint2f();
//对于找到的每个轮廓
对于(int i=0;i1&xoffset>1)
{
添加(新点(xoffset+x,yoffset+y));
ylist.add(yoffset+y);
xlist.add(xoffset+x);
}
}
}
}
}
}

调整canny阈值控制结果图像中的轮廓量。

调整canny阈值控制结果图像中的轮廓量

import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Rect;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class imtest {



   public static void main(String args[]) throws IOException{
       String filename="C:/image.png";
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
   Mat torect=new Mat();
   Mat torect1=Imgcodecs.imread(filename,0);
   Imgproc.Canny(torect1, torect, 10, 100); 


   List<MatOfPoint> contours = new ArrayList<MatOfPoint>();  
   Imgproc.findContours(torect.clone(), contours, new Mat(),        Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
   ArrayList<Point> outlie=new ArrayList<Point>();
   ArrayList<Integer> ylist=new ArrayList<Integer>();
   ArrayList<Integer> xlist=new ArrayList<Integer>();

          MatOfPoint2f approxCurve = new MatOfPoint2f();

          //For each contour found
          for (int i=0; i<contours.size(); i++)
          {
              //Convert contours(i) from MatOfPoint to MatOfPoint2f
              MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );
              //Processing on mMOP2f1 which is in type MatOfPoint2f
              double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
              Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

              //Convert back to MatOfPoint
              MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

              // Get bounding rect of contour
              Rect rect = Imgproc.boundingRect(points);
             int xoffset=rect.x;
             int yoffset=rect.y;
              for (int y = 0; y < rect.height; y++) {
                    for (int x = 0; x < rect.width; x++) {
                        if (yoffset>1 & xoffset>1)
                        {
                        outlie.add(new Point(xoffset+x,yoffset+y));
                        ylist.add(yoffset+y);
                        xlist.add(xoffset+x);
                        }
                    }
                    }


              }
   }
   }