Android 打开CV检测纸返回奇怪/错误的坐标
我正在使用以下类检测/接收找到的文档/纸张:Android 打开CV检测纸返回奇怪/错误的坐标,android,opencv,canvas,detection,android-camerax,Android,Opencv,Canvas,Detection,Android Camerax,我正在使用以下类检测/接收找到的文档/纸张: public class DetekPs { /** * Object that encapsulates the contour and 4 points that makes the larger * rectangle on the image */ public static class Quadrilateral { public MatOfPoint contour; public Point[] points
public class DetekPs {
/**
* Object that encapsulates the contour and 4 points that makes the larger
* rectangle on the image
*/
public static class Quadrilateral {
public MatOfPoint contour;
public Point[] points;
public Quadrilateral(MatOfPoint contour, Point[] points) {
this.contour = contour;
this.points = points;
}
}
public static Quadrilateral findDocument( Mat inputRgba ) {
ArrayList<MatOfPoint> contours = findContours(inputRgba);
Quadrilateral quad = getQuadrilateral(contours);
return quad;
}
private static ArrayList<MatOfPoint> findContours(Mat src) {
double ratio = src.size().height / 500;
int height = Double.valueOf(src.size().height / ratio).intValue();
int width = Double.valueOf(src.size().width / ratio).intValue();
Size size = new Size(width,height);
Mat resizedImage = new Mat(size, CvType.CV_8UC4);
Mat grayImage = new Mat(size, CvType.CV_8UC4);
Mat cannedImage = new Mat(size, CvType.CV_8UC1);
Imgproc.resize(src,resizedImage,size);
Imgproc.cvtColor(resizedImage, grayImage, Imgproc.COLOR_RGBA2GRAY, 4);
Imgproc.GaussianBlur(grayImage, grayImage, new Size(5, 5), 0);
Imgproc.Canny(grayImage, cannedImage, 75, 200);
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(cannedImage, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
hierarchy.release();
Collections.sort(contours, new Comparator<MatOfPoint>() {
@Override
public int compare(MatOfPoint lhs, MatOfPoint rhs) {
return Double.valueOf(Imgproc.contourArea(rhs)).compareTo(Imgproc.contourArea(lhs));
}
});
resizedImage.release();
grayImage.release();
cannedImage.release();
return contours;
}
private static Quadrilateral getQuadrilateral(ArrayList<MatOfPoint> contours) {
for ( MatOfPoint c: contours ) {
MatOfPoint2f c2f = new MatOfPoint2f(c.toArray());
double peri = Imgproc.arcLength(c2f, true);
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(c2f, approx, 0.02 * peri, true);
Point[] points = approx.toArray();
// select biggest 4 angles polygon
if (points.length == 4) {
Point[] foundPoints = sortPoints(points);
return new Quadrilateral(c, foundPoints);
}
}
return null;
}
private static Point[] sortPoints(Point[] src) {
ArrayList<Point> srcPoints = new ArrayList<>(Arrays.asList(src));
Point[] result = { null , null , null , null };
Comparator<Point> sumComparator = new Comparator<Point>() {
@Override
public int compare(Point lhs, Point rhs) {
return Double.valueOf(lhs.y + lhs.x).compareTo(rhs.y + rhs.x);
}
};
Comparator<Point> diffComparator = new Comparator<Point>() {
@Override
public int compare(Point lhs, Point rhs) {
return Double.valueOf(lhs.y - lhs.x).compareTo(rhs.y - rhs.x);
}
};
// top-left corner = minimal sum
result[0] = Collections.min(srcPoints, sumComparator);
// bottom-right corner = maximal sum
result[2] = Collections.max(srcPoints, sumComparator);
// top-right corner = minimal diference
result[1] = Collections.min(srcPoints, diffComparator);
// bottom-left corner = maximal diference
result[3] = Collections.max(srcPoints, diffComparator);
return result;
}
}
绘制要点:
@Override
protected void onDraw(Canvas canvas) {
Log.e("i","he ra hu" );
Paint paint2 = new Paint();
paint2.setColor(Color.RED);
paint2.setStrokeWidth(3);
for(int idx=0;idx<po.length;idx++){
canvas.drawCircle((float)po[idx].x,(float)po[idx].y, 6, paint2);
Log.e("x",""+xx);
Log.e("y",""+yy);
}
}
这会导致绘制点时如下所示:
x: 23.0
y: 122.0
x: 249.0
y: 110.0
x: 249.0
y: 110.0
x: 0.0
y: 182.0
正如你所看到的,接收点和绘制点与我的图像相差很远。
问题在于原因是什么。我画错了吗?我得到了错误的分数吗?或者是关于我的图像/位图的大小/宽度的问题,因此我需要设置正确的纵横比/分辨率?您是否检测到输入大小调整版本上的点?@eldesgraciado否图像大小保持不变。我在imageview上显示相同的显示,然后基本上将相同的图像传递给opencv分析仪
x: 23.0
y: 122.0
x: 249.0
y: 110.0
x: 249.0
y: 110.0
x: 0.0
y: 182.0