Reputation: 1095
Probably this question might be duplicate.But I am finding hard to get answer as I am new to OpenCV and Canvas drawing on view for android here is my code
List<Point> pts = new ArrayList<Point>();
Converters.Mat_to_vector_Point(MatOfPoint, pts);
I am getting List correctly,where Point abstract org.opencv.core.Point. Now i am converting those points to android.graphics.Point coordinate using correct MatOfPoint.
android.graphics.Point pt1 = new android.graphics.Point((int) pts.get(0).x, (int) pts.get(0).y);
On getting this, I am trying to draw Canvas using android.graphics.Point, but coordinates are no matching up with image coordinates. Check the code given below.
@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);
}
The canvas drawn with the detected square from openCV coordinate does not match the canvas Point coordinates. Have I to manipulate the points with pixels or density of image? Any help will be appreciated. Thanks
Upvotes: 1
Views: 1676
Reputation: 136
you need to scale the point and shift point's position by adding offset
see line 412 in CameraBridgeViewBase.java
see line 171 in JavaCameraView.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
Upvotes: 3
Reputation: 13
Points which you have calculated with opencv, references coordinates on your MAT( or on your bitmap image). You have to scale these coordinates to your display object in order to fit your line on image. You can use the code below to achieve your goal. Hope it helps.
@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);
}
Upvotes: 0