这个问题可能是重复的。但是我发现很难得到答案,因为我是OpenCV和Canvas的新手,可以在Android上绘图,这是我的代码
List<Point> pts = new ArrayList<Point>();
Converters.Mat_to_vector_Point(MatOfPoint, pts);
我得到列表正确,其中点抽象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 绘制画布,但坐标与图像坐标不匹配。检查下面给出的代码。
@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 坐标检测到的正方形绘制的画布与画布点坐标不匹配。我必须用像素或图像密度来操纵点吗?任何帮助将不胜感激。谢谢
您需要通过添加偏移来缩放点和移动点的位置
请参阅CameraBridgeViewBase中的第412行.java
参见JavaCameraView中的第171行.java
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
使用 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);
}