How to accurately translate image coordinates from Python/OpenCV to Android ImageView?

25 Views Asked by At

I am working on an indoor navigation application that involves two floor plan images: one is the original floor plan, and the other includes nodes and paths (used for navigation). Using Python and the OpenCV library, I have successfully extracted the XY coordinates of all nodes from the navigation floor plan. However, I am facing challenges when translating these coordinates to position a map pin on the ImageView in Android Studio.

When I attempted to position an ImageView as a map pin on a FloorPlan ImageView using these coordinate, the map pin does not align with the nodes as expected in the Android ImageView. The scaling and translation seem to be incorrect.

Here is my imageView:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/floorPlan"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:src="@drawable/floor5" />

    <!-- ImageView for the map pin marker -->
    <ImageView
        android:id="@+id/mapPin"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:visibility="gone"
        android:src="@drawable/map_pin" />

</FrameLayout>

Here is what i tried for scaling:

private void updateMapPinPosition(int xCoordinate, int yCoordinate) {
    // Convert 30dp to pixels based on screen density
    int pinSizeInPixels = (int) (30 * getResources().getDisplayMetrics().density);

    // Get the ImageView and its dimensions
    ImageView mapPin = findViewById(R.id.mapPin);
    Drawable floorPlanDrawable = mapImageView.getDrawable();

    if (floorPlanDrawable != null) {
        // Get the dimensions of the drawable (the floor plan image)
        int drawableWidth = floorPlanDrawable.getIntrinsicWidth();
        int drawableHeight = floorPlanDrawable.getIntrinsicHeight();

        // Get the dimensions of the ImageView (the view displaying the floor plan)
        int imageViewWidth = mapImageView.getWidth();
        int imageViewHeight = mapImageView.getHeight();

        // Calculate the scale based on the drawable dimensions and view dimensions
        float xScale = (float) imageViewWidth / drawableWidth;
        float yScale = (float) imageViewHeight / drawableHeight;

        // Scale the coordinates from the server to fit the ImageView
        int scaledX = (int) (xCoordinate * xScale);
        int scaledY = (int) (yCoordinate * yScale);

        // Adjust the position of the map pin based on the pin size
        scaledX -= pinSizeInPixels / 2;  // Center the pin horizontally
        scaledY -= pinSizeInPixels;       // Anchor the pin to the bottom

        // Use absolute coordinates to place the pin
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                pinSizeInPixels,  // Width in pixels
                pinSizeInPixels   // Height in pixels
        );

        params.leftMargin = scaledX;  // X position
        params.topMargin = scaledY;   // Y position

        mapPin.setLayoutParams(params);  // Apply the layout parameters to the map pin
        mapPin.setVisibility(View.VISIBLE);  // Make the map pin visible
    }
}
0

There are 0 best solutions below