I was trying to make an application that can find rectangles in an image and highlight the corners of the same. I was able to find the corners of the image using OpenCV and no issues with it. But when i try to place some object like a small imageview to show the corners, it does not get itself at the correct position. After some brainstorming i realised that the image would be scaled and so i have to position the markers accordingly.
I took a look at this answer and used that method and tried to calculate the constraints to locate the marker but still it is not accurate (not even a bit). Just as an example i am providing an image i captured.
Here, the red circles are drawn by OpenCV (the required position) whereas the white-grey circles are those that were calculated.
Here is the code: (imgViewHeight and imgViewWidth are known constants)
protected void showCorners(List<Point> corners, Bitmap image){
List<ImageView> markers = new ArrayList<>();
markers.add((ImageView) findViewById(R.id.c1));
markers.add((ImageView) findViewById(R.id.c2));
markers.add((ImageView) findViewById(R.id.c3));
markers.add((ImageView) findViewById(R.id.c4));
ConstraintLayout cornerLayer = findViewById(R.id.cornerLayer);
for (int i=0;i<4;i++){
Point point = corners.get(i);
//TODO: Correct the algorithm to find constraints.
double constraintTop = ((imgViewHeight*point.y)/image.getHeight()); //Here are the problems
double constraintLeft = ((imgViewWidth*point.x)/image.getWidth()); //Here are the problems
Log.d("Constraints", constraintLeft+" x "+constraintTop);
ImageView marker = markers.get(i);
ConstraintSet set = new ConstraintSet();
set.clone(cornerLayer);
set.connect(marker.getId(), ConstraintSet.LEFT, cornerLayer.getId(), ConstraintSet.LEFT, (int)constraintLeft);
set.connect(marker.getId(), ConstraintSet.TOP, cornerLayer.getId(), ConstraintSet.TOP, (int)constraintTop);
set.applyTo(cornerLayer);
cornerLayer.bringChildToFront(marker);
marker.setVisibility(View.VISIBLE);
}
}
Here is the part of my layout containig the cornerLayer (it is a constraintLayout):
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cornerLayer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/title_activity_view_edit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/backgrounds/scenic" />
<ImageView
android:id="@+id/c1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="50dp"
android:layout_marginTop="50dp"
android:contentDescription="@string/a_corner"
android:src="@android:drawable/radiobutton_off_background"
app:layout_constraintStart_toStartOf="@id/cornerLayer"
app:layout_constraintTop_toTopOf="@id/cornerLayer" />
<ImageView
android:id="@+id/c2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="50dp"
android:layout_marginStart="200dp"
android:contentDescription="@string/a_corner"
android:src="@android:drawable/radiobutton_off_background"
app:layout_constraintStart_toStartOf="@id/cornerLayer"
app:layout_constraintTop_toTopOf="@id/cornerLayer" />
<ImageView
android:id="@+id/c3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="50dp"
android:layout_marginTop="400dp"
android:contentDescription="@string/a_corner"
android:src="@android:drawable/radiobutton_off_background"
app:layout_constraintStart_toStartOf="@id/cornerLayer"
app:layout_constraintTop_toTopOf="@id/cornerLayer" />
<ImageView
android:id="@+id/c4"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="200dp"
android:layout_marginTop="400dp"
android:contentDescription="@string/a_corner"
android:src="@android:drawable/radiobutton_off_background"
app:layout_constraintStart_toStartOf="@+id/cornerLayer"
app:layout_constraintTop_toTopOf="@id/cornerLayer" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have given some default constraint values just for showing neatness in android studio but it gets changed so it doesn't matter (i suppose).
Any help would be appreciated.
Thanks.

