[英]Java - OpenCV ignoring extra contours
我有一張圖片:
我想裁剪它所以這本書本身就是這樣。
我正在使用OpenCV來嘗試並獲得圖像的控制。 一旦我繪制它們,它就像這樣。 如何忽略圖像右側的額外輪廓? 我已經嘗試使用標准偏差的異常值。 現在,它需要在rectanlge中的每個點,並將其添加到arraylist以供稍后處理。 我有一個整體的arraylist for points,還有2個,所以在計算統計分析時,積分可以從最小到最大排序。
這就是現在的樣子:
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);
}
}
}
}
}
}
調整canny的閾值控制了所得圖像中的輪廓量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.