Java - OpenCV忽略了额外的轮廓

时间:2016-04-17 19:50:30

标签: java opencv

我有一张图片:

enter image description here

我想裁剪它,所以这本书本身就是。

我正在使用OpenCV来尝试并获得图像的控制。一旦我绘制它们,它就像这样。如何忽略图像右侧的额外轮廓?我已经尝试使用标准偏差的异常值。现在,它需要在rectanlge中的每个点,并将其添加到arraylist以供稍后处理。我有一个关于积分的整体arraylist,还有2个在计算统计分析时,积分可以从最小到最大排序。

这就是现在的样子:

enter image description here

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);
                        }
                    }
                    }


              }
   }
   }

1 个答案:

答案 0 :(得分:0)

调整精确阈值可控制生成图像中的轮廓量。