协调从org.opencv.core.Point到android.graphics.Point的转换

时间:2015-01-01 11:53:54

标签: android opencv canvas graphics

可能这个问题可能是重复的。但是我发现很难得到答案,因为我是OpenCV的新手,Canvas在Android上查看这里是我的代码

List<Point> pts = new ArrayList<Point>();
Converters.Mat_to_vector_Point(MatOfPoint, pts);

我正确获取List,其中Point abstract org.opencv.core.Point。 现在我正在使用正确的MatOfPoint将这些点转换为android.graphics.Point坐标。

android.graphics.Point pt1 = new android.graphics.Point((int) pts.get(0).x, (int) pts.get(0).y);

在得到这个,我试图使用android.graphics.Point绘制Canvas,但坐标与图像坐标不匹配。检查下面给出的代码。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeWidth(3);

    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(Color.parseColor("#55000000"));
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(5);
    canvas.drawPaint(paint);


    canvas.drawLine(point1.x,point1.y, point2.x, point2.y, paint);
}

使用openCV坐标中检测到的方块绘制的画布与画布点坐标不匹配。我是否可以使用像素或图像密度来操纵点?任何帮助将不胜感激。 感谢

2 个答案:

答案 0 :(得分:0)

使用opencv计算的点,引用MAT(或位图图像)上的坐标。 您必须将这些坐标缩放到显示对象,以使您的线条适合图像。 您可以使用以下代码来实现目标。 希望它有所帮助。

 @Override
    protected void onDraw(Canvas canvas){

        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(3);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.parseColor("#55000000"));
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5);
        canvas.drawPaint(paint);
        //iv is the ImageView which you can draw canvas over it
        // imgbitmap is bitmap image 



          double scaledWidth=iv.getWidth();
           double scaledHeight=iv.getHeight();

           double xScaleFactor= scaledWidth/imgbitmap.getWidth();
           double yScaleFactor= scaledHeight/imgbitmap.getHeight();                                

 android.graphics.Point canvas_point1 = new android.graphics.Point((int)((point1.x*xScaleFactor)),(int)((point1.y*yScaleFactor)));
 android.graphics.Point canvas_point2 = new android.graphics.Point((int)((point2.x*xScaleFactor)),(int)((point2.y*yScaleFactor)));
           canvas.drawLine(canvas_point1.x,canvas_point1.y, canvas_point2.x, canvas_point2.y, paint);
        }

答案 1 :(得分:0)

您需要通过添加偏移

来缩放点和移动点的位置

参见CameraBridgeViewBase.java

中的第412行

请参阅JavaCameraView.java

中的第171行
scale=Math.min(mOpenCvCameraView.getWidth()/Matwidth,mOpenCvCameraView.getHeight())/Matheight)

xoffset=(mOpenCvCameraView.getWidth()-scale*Matwidth)/2
yoffset=(mOpenCvCameraView.getHeight()-scale*Matheight)/2
final point1's X coordinate is: point1.x*scale+xoffset
final point1's Y coordinate is: point1.y*scale+yoffset

final point2's X coordinate is: point2.x*scale+xoffset
final point2's Y coordinate is: point2.y*scale+yoffset